Below are the comments given by Coverity scan. Here there is a chance that $data become undefined, but the code "$data ?? []" will solve that issue. If $data is null output of this code will be []. I dont understand what is issue here.
//assignment: Assigning: $data = undefined.
public function addPromotions( Array $promotions )
{
foreach($promotions as $promotion)
//implied_types: $data[] implies that the type of $data must be array.
{ $data[] = ['PromoCode' => $promotion->claim_code, 'PromotionName' => $promotion->name]; }
//possible_types: At condition $data, the type of $data must be one of undefined and array.
//dead_error_condition: The condition $data must be true.
//CID 15805 (#1 of 1): Logically dead code (DEADCODE)
//dead_error_line: Execution cannot reach this statement: var <storage from new>;.**
if( $data ?? [] )
{ $this->post('/current/api/AddPromotions', $data); }
}
Below is actual code
public function addPromotions( Array $promotions )
{
foreach($promotions as $promotion)
{ $data[] = ['PromoCode' => $promotion->claim_code, 'PromotionName' => $promotion->name]; }
if( $data ?? [] )
{ $this->post('/current/api/AddPromotions', $data); }
}
In some cases ( empty $promotions array ), we get below error WARNING Undefined variable $data To tackle this kind of error, I used "$data ?? []". Because, if $data is undefined, it evaluates to null when using this operator and returns []. There are alternative code for this. But I didn't get the issue with my usage. There are 3 cases for $data
- $data is non-empty arrray
- $data is empty ([])
- $data is undefined
Here 1st two cases, 2nd operand (of $data ?? []) wont consider. But in the 3rd case, 2nd operand will be considered. In that case how this will become a dead code. IMO, a dead code is something which never executed in any case.
here's how you can adjust your code to address the concerns and ensure proper handling of $data: