16 comments
Comment actions Permalink

There is PHP | Probable bugs | Non-strict object equality: 

Reports the usages of the comparison operator (==) for comparing object variables. 
Object variables are compared as follows:

  • When using the comparison operator (==), two object instances are considered equal if they have the same attributes and values (values are compared with ==), and are instances of the same class.
  • When using the identity operator (===), object variables are considered identical if and only if they refer to the same instance of the same class.

 

0
Comment actions Permalink

Yes, I know about that one, but why not one for non-object comparisons? We have one for array functions, like in_array(), but not for 

false == 0

I am of the practice of not using == or != anymore, and I would like them all flagged. I could have sworn that this existed.

 

0
Comment actions Permalink

Can you share a real-life example where this would be helpful?

0
Comment actions Permalink

I'm not sure what you mean. I want to be warned anytime that I use "==" or "==", instead of "===" or "!==". PHP does coercion just like JavaScript does:

https://www.php.net/manual/en/language.operators.comparison.php

var_dump(1 == TRUE);  // TRUE - same as (bool)1 == TRUE
var_dump(0 == FALSE); // TRUE - same as (bool)0 == FALSE
var_dump(100 < TRUE); // FALSE - same as (bool)100 < TRUE
var_dump(-10 < FALSE);// FALSE - same as (bool)-10 < FALSE
var_dump(min(-100, -10, NULL, 10, 100)); // NULL - (bool)NULL < (bool)-100 is FALSE < TRUE

More info:

http://phpsadness.com/sad/47

 

My practice now is to never use equal/not equal, but to only use indentical/not identical and I want myself (and my team) to be warned went this is not done. I would like the same done for array functions (in_array()), which I thought was done already, but that was actually a Plugin

0
Comment actions Permalink

Thanks for the clarification. The reason I'm asking this is that we're trying to make our inspections "smart", so that they wouldn't bother you with false positives. For example: https://youtrack.jetbrains.com/issue/WI-41951 and https://youtrack.jetbrains.com/issue/WI-68581.

Anyway, I've submitted this as a new feature request at https://youtrack.jetbrains.com/issue/WI-69082. Please vote for it or comment in order to get notified about its updates & increase its priority.

0
Comment actions Permalink

I disagree with both of those issue requests:

ignore if compared arguments are of the same object type

That to me is the whole point. I don't know if they are the same type and I don't want to have to go find out later when I'm the reader of that code.

=== ensures they are the same type, or it will fail, which forces me as the writer to check this or my tests will fail.

0
Comment actions Permalink

FYI, I think we make way to many exceptions. I think the rules should be strict (one way or the other), and leave the exception to person who wants it to figure out. Otherwise, I'm having to program to the vastness of opinions.

0
Comment actions Permalink
Thanks for sharing your opinion! I've added this to both reports as an internal comment.
0
Comment actions Permalink

You can actually configure an inspection that would do that for you. This is done via Find | Replace Structurally:

Create a pattern to search for == and replace it with ===:

Then save it as Inspection using a menu at the top left:

As a result, all these usages will highlighted in editor & also be shown when running Code > Inspect Code:

0
Comment actions Permalink

Well, now, that's cool! I have never even tried to use the Structural Search / Structural Replace as I was confused about its purpose (and I'm rather good at RegEx). Thanks!

0
Comment actions Permalink

Great, thanks for the feedback!

0
Comment actions Permalink

Well, I have a problem. Initially, this worked great, I even see that it added my Inspections to the Profile I was using:

 

However, once I closed and reopened PhpStorm, although the Profile still contains my new Inspections, they are no longer working and are not available in the UI.

 

0
Comment actions Permalink

When I try to add it again, I get a message that it already exists.

 

0
Comment actions Permalink

I'm filing this as a bug.

0
Comment actions Permalink

I just tried it again, I created another Inspection this way, watched it add it to the Profile, now there are 3 of these, but only 1 shows up in the UI, and when I close and reopen, that 1 is gone as well (but still in the XML file).

0

Please sign in to leave a comment.