Error Identifier: notEqual.invalid
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
function doFoo(stdClass $obj, int $number): void
{
$result = $obj != $number; // error: Comparison operation "!=" between stdClass and int results in an error.
}
Why is it reported? #
PHP does not support loose comparison (!=) between certain type combinations. When comparing an object with a scalar type like int using !=, PHP cannot perform a meaningful comparison and will produce an error. The operation has no well-defined behavior for these types.
How to fix it #
Compare values of the same or compatible types, or extract a comparable value from the object first.
function doFoo(stdClass $obj, int $number): void
{
- $result = $obj != $number;
+ $result = $obj->value !== $number;
}
How to ignore this error #
You can use the identifier notEqual.invalid to ignore this error using a comment:
// @phpstan-ignore notEqual.invalid
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: notEqual.invalid
Rules that report this error #
- PHPStan\Rules\Operators\InvalidComparisonOperationRule [1]