Error Identifier: identical.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(int $i): void
{
if ($i === 'hello') {
// ...
}
}
Why is it reported? #
A strict comparison using === between two values of incompatible types will always evaluate to false. The === operator checks both value and type equality, so comparing an int with a string can never be true.
In the example above, $i is an int and 'hello' is a string. These types can never be identical, so the condition will never be satisfied and the code inside the if block is dead code.
How to fix it #
Fix the comparison to use the correct type:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- if ($i === 'hello') {
+ if ($i === 42) {
// ...
}
}
Or fix the parameter type if the comparison is correct:
<?php declare(strict_types = 1);
-function doFoo(int $i): void
+function doFoo(string $i): void
{
if ($i === 'hello') {
// ...
}
}
How to ignore this error #
You can use the identifier identical.alwaysFalse to ignore this error using a comment:
// @phpstan-ignore identical.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: identical.alwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\StrictComparisonOfDifferentTypesRule [1]