Phpinsights: Possible false positive Strict comparison using === between bool and 0

Created on 11 Jun 2020  路  2Comments  路  Source: nunomaduro/phpinsights

| Q | A
| ---------------- | -----
| Bug report? | yes
| Feature request? | no
| Library version | 0.6.0

Updated to Larastan 0.6.0 this morning and tried to resolve all the new stuff that larastan found on my code base and I stumbled upon the following:

 ------ -------------------------------------------------------------------------------
  Line   src/Relations/Customers/Observers/CustomerObserver.php
 ------ -------------------------------------------------------------------------------
  51     Strict comparison using === between bool and 0 will always evaluate to false.
 ------ -------------------------------------------------------------------------------

The code in question:

if ($this->customer->is_consumer === 0) {

In which $this->customer is an instance of a Customer model and is_consumer is a boolean property. However, the model does not do a $casts to boolean, so the data that is actaully returned is in fact a 0. and not a boolean value.

Now, granted, I probably should add a $casts and update the if condition to === false, because that is the better code, but I was just wondering: how does Larastan assume that we are talking about a boolean comparison and that is_consumer is in fact a boolean? Surely it is not checking the migrations? Do you check if there is some sort of 'boolean'-ish property name, like is_, or has_ and then simply assume that it is a boolean?

I'm glad that Larastan pointed this out, don't get me wrong, as I agree with what it is saying. But Larastan assumes here that is_consumer is a boolean, while in fact it is just an integer value. And therefore the === 0 is a valid comparison, and therefore it is a false positive.

Most helpful comment

My bad!

All 2 comments

@axit-joost Larastan? Are you sure that you are on the right repo buddy?

My bad!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

viezel picture viezel  路  4Comments

erwinw picture erwinw  路  6Comments

Knudian picture Knudian  路  3Comments

brunocfalcao picture brunocfalcao  路  5Comments

nunomaduro picture nunomaduro  路  5Comments