skip to Main Content

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

  1. $data is non-empty arrray
  2. $data is empty ([])
  3. $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.

2

Answers


  1. here’s how you can adjust your code to address the concerns and ensure proper handling of $data:

    public function addPromotions(Array $promotions) {
        $data = [];
        foreach ($promotions as $promotion) {
            $data[] = ['PromoCode' => $promotion->claim_code, 'PromotionName' => $promotion->name];
        }
        if ($data) {
            $this->post('/current/api/AddPromotions', $data);
        } else {
        
        }
    }
    
    Login or Signup to reply.
  2. An empty array evaluates to false

    If $data evaluates to false the empty array created by $data ?? [] will evaluate to false as well. Creating the empty array is therefore redundant and can be removed.

    1. if checks for the true-ish value of a statement.
    2. The expression $data ?? [] checks if $data exists and if not creates an empty array to be evaluated for the if condition
    3. An empty array is treated as false in a condition expression (var_dump((bool) array()); // false)
    4. So again, when $data evaluates to false, the empty array is also false and won’t change the outcome of the if condition.

    This means that you might just as well write if( isset($data) ). If you initialize $data as an empty array you can also use !empty($data) to be more explicitly checking if something is in the array.

    Also see:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search