Error Identifier: equal.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 doFoo(string $s): void
{
if ($s == new \stdClass()) {
// never reached
}
}
Why is it reported? #
The loose comparison using == always evaluates to false based on the types of the operands. Even with PHP’s type juggling rules, these two values can never be considered equal. This indicates dead code or a logic error – the branch will never execute.
How to fix it #
Fix the comparison to compare values that can actually be equal:
function doFoo(string $s): void
{
- if ($s == new \stdClass()) {
+ if ($s == 'expected') {
// ...
}
}
Or use strict comparison (===) if the intent is to compare identical types:
function doFoo(string $s): void
{
- if ($s == new \stdClass()) {
+ if ($s === 'expected') {
// ...
}
}
How to ignore this error #
You can use the identifier equal.alwaysFalse to ignore this error using a comment:
// @phpstan-ignore equal.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: equal.alwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\ConstantLooseComparisonRule [1]