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
{
$t = true;
if ($t || $i > 0) {
echo 'left always true';
}
}
Why is it reported? #
The left side of a || expression always evaluates to true. Because || uses short-circuit evaluation, when the left side is always true, the right side is never evaluated, making the entire expression always true.
In the example above, $t is always true, so $i > 0 on the right side is never evaluated.
How to fix it #
Simplify the condition by removing the redundant parts:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $t = true;
- if ($t || $i > 0) {
- echo 'left always true';
- }
+ echo 'left always true';
}
Or fix the left side if it should not always be true:
<?php declare(strict_types = 1);
-function doFoo(int $i): void
+function doFoo(int $i, bool $flag): void
{
- $t = true;
- if ($t || $i > 0) {
+ if ($flag || $i > 0) {
echo 'something';
}
}
How to ignore this error #
You can use the identifier booleanOr.leftAlwaysTrue to ignore this error using a comment:
// @phpstan-ignore booleanOr.leftAlwaysTrue
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: booleanOr.leftAlwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanOrConstantConditionRule [1]