Error Identifier: notIdentical.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, string $s): void
{
if ($i !== $s) { // error: Strict comparison using !== between int and string will always evaluate to true.
// ...
}
}
Why is it reported? #
The strict not-identical operator (!==) compares both value and type. When the two operands can never be of the same type, the comparison will always evaluate to true, making the condition meaningless. This usually indicates a logic error – the code is checking something that can never be false.
This error can be turned off for the last condition in an if/elseif chain via the reportAlwaysTrueInLastCondition option.
How to fix it #
Compare values of compatible types, or remove the unnecessary condition.
-function doFoo(int $i, string $s): void
+function doFoo(int $i, int $j): void
{
- if ($i !== $s) {
+ if ($i !== $j) {
// ...
}
}
If comparing different representations of the same logical value, convert one operand first.
function doFoo(int $i, string $s): void
{
- if ($i !== $s) {
+ if ($i !== (int) $s) {
// ...
}
}
How to ignore this error #
You can use the identifier notIdentical.alwaysTrue to ignore this error using a comment:
// @phpstan-ignore notIdentical.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: notIdentical.alwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\StrictComparisonOfDifferentTypesRule [1]