Error Identifier: notEqual.alwaysFalse
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 $value): void
{
if ($value != $value) {
// unreachable
}
}
Why is it reported? #
The loose comparison using != between two expressions will always evaluate to false based on the types PHPStan has inferred. This means the condition can never be true, so any code inside the branch is dead code.
The != operator performs a loose (type-juggling) comparison. PHPStan reports this when it can determine from the types that the comparison will always yield false, regardless of the actual values at runtime.
How to fix it #
Review the logic of the comparison. The condition is redundant and the code inside the branch is unreachable.
If the comparison is intentional but the types are wrong, fix the types so the comparison becomes meaningful:
<?php declare(strict_types = 1);
-function check(int $value): void
+function check(int $value, int $other): void
{
- if ($value != $value) {
+ if ($value != $other) {
// ...
}
}
If the branch is no longer needed, remove it:
<?php declare(strict_types = 1);
function check(int $value): void
{
- if ($value != $value) {
- // unreachable
- }
}
How to ignore this error #
You can use the identifier notEqual.alwaysFalse to ignore this error using a comment:
// @phpstan-ignore notEqual.alwaysFalse
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.alwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\ConstantLooseComparisonRule [1]