Error Identifier: logicalOr.leftAlwaysFalse
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 $x): void
{
if ($x > 10) {
return;
}
// At this point $x is <= 10
$result = ($x > 10) or ($x === 5); // ERROR: Left side of or is always false.
}
Why is it reported? #
PHPStan determined that the left side of the or expression is always false. This means the left operand does not contribute to the result – the entire expression depends solely on the right side. This usually indicates a logic error, a redundant check, or a condition that has already been narrowed by earlier control flow.
The or keyword is the low-precedence version of ||. This identifier specifically covers the or keyword; for ||, see booleanOr.leftAlwaysFalse.
How to fix it #
Remove the redundant left side or fix the condition to test what was actually intended:
<?php declare(strict_types = 1);
function doFoo(int $x): void
{
if ($x > 10) {
return;
}
- $result = ($x > 10) or ($x === 5);
+ $result = ($x === 5);
}
Or fix the condition if the logic is wrong:
<?php declare(strict_types = 1);
function doFoo(int $x): void
{
if ($x > 10) {
return;
}
- $result = ($x > 10) or ($x === 5);
+ $result = ($x >= 8) or ($x === 5);
}
How to ignore this error #
You can use the identifier logicalOr.leftAlwaysFalse to ignore this error using a comment:
// @phpstan-ignore logicalOr.leftAlwaysFalse
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: logicalOr.leftAlwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanOrConstantConditionRule [1]