Error Identifier: identical.alwaysTrue
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
{
$value = 1;
if ($value === 1) {
// always entered
}
}
Why is it reported? #
The strict comparison using === always evaluates to true because both sides are known to have the same value at that point in the code. In the example above, $value is always 1, so comparing it with === to 1 is always true. This usually indicates a redundant check or a logic error.
How to fix it #
Remove the redundant comparison:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
$value = 1;
- if ($value === 1) {
- // always entered
- }
+ // Execute the code unconditionally
}
Or fix the logic to compare the correct variable:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $value = 1;
- if ($value === 1) {
+ if ($i === 1) {
// now depends on the actual input
}
}
How to ignore this error #
You can use the identifier identical.alwaysTrue to ignore this error using a comment:
// @phpstan-ignore identical.alwaysTrue
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.alwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\StrictComparisonOfDifferentTypesRule [1]