Error Identifier: booleanOr.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 $i): void
{
$zero = 0;
if ($zero || $i > 0) {
// ...
}
}
Why is it reported? #
The left side of the || (boolean OR) expression always evaluates to false. While the overall expression might still be true depending on the right side, having a left operand that is always falsy indicates dead code or a logic error. The left operand serves no purpose because the result of the || expression is entirely determined by the right side.
In the example above, $zero is always 0, which is falsy in PHP, so the left side of || is always false.
How to fix it #
Remove the redundant left operand:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $zero = 0;
- if ($zero || $i > 0) {
+ if ($i > 0) {
// ...
}
}
Or fix the logic to use the correct variable:
<?php declare(strict_types = 1);
-function doFoo(int $i): void
+function doFoo(int $i, bool $flag): void
{
- $zero = 0;
- if ($zero || $i > 0) {
+ if ($flag || $i > 0) {
// ...
}
}
How to ignore this error #
You can use the identifier booleanOr.leftAlwaysFalse to ignore this error using a comment:
// @phpstan-ignore booleanOr.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: booleanOr.leftAlwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanOrConstantConditionRule [1]