Error Identifier: equal.notAllowed
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 check(int $a, int $b): bool
{
return $a == $b;
}
Why is it reported? #
This error is reported by phpstan/phpstan-strict-rules.
Loose comparison via == is not allowed. The == operator performs type juggling before comparing values, which can lead to unexpected results. For example, 0 == 'foo' evaluates to true in PHP versions before 8.0, and '' == null is true in all versions.
Using strict comparison (===) avoids these pitfalls by comparing both value and type.
How to fix it #
Replace the loose comparison operator with its strict equivalent:
<?php declare(strict_types = 1);
function check(int $a, int $b): bool
{
- return $a == $b;
+ return $a === $b;
}
How to ignore this error #
You can use the identifier equal.notAllowed to ignore this error using a comment:
// @phpstan-ignore equal.notAllowed
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: equal.notAllowed
Rules that report this error #
- PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule [1] phpstan/phpstan-strict-rules