Search code examples
phperror-handlingphpstorm

Add custom error highlighting in PhpStorm when single equals in php if condition


Yes, using a single equals in a php if condition is sometimes desired, but how can I set a custom error in PhpStorm on the below code to prevent me from doing so?

mark as error when:

if (a = b) {
    //crap I just reassigned a when I wanted to check if a == b
}

Solution

  • There is an inspection for that already -- just use it.

    Settings/Preferences | Editor | Inspections | PHP | Probable bugs -> Assignment in condition

    enter image description here

    It works just fine:

    enter image description here

    Severity can be changed from "Warning" to "Error" if needed.


    NOTE:
    Obviously, it will now highlight ALL such assignments .. not just this particular place. But you can create custom scope and turn it on or off for files in those scopes.

    For particular place it can also be suppressed via special comment:

    /** @noinspection PhpAssignmentInConditionInspection */
    if ($a = $b) {
        echo 'hello!';
    }