Error Identifier: booleanAnd.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) {
// never reached
}
}
Why is it reported? #
The left side of the && (boolean AND) expression always evaluates to false. Because && uses short-circuit evaluation, when the left operand is always falsy, the right operand is never evaluated, and the entire expression is always false. This means the condition body will never be executed, indicating a logic error or redundant code.
In the example above, $zero is always 0, which is falsy in PHP, so $zero && $i is always false.
How to fix it #
Remove the redundant condition if the code inside should never execute:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $zero = 0;
- if ($zero && $i) {
- // never reached
- }
}
Or fix the logic to check the correct variable:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $zero = 0;
- if ($zero && $i) {
+ if ($i && $i > 0) {
// now depends on actual input
}
}
How to ignore this error #
You can use the identifier booleanAnd.leftAlwaysFalse to ignore this error using a comment:
// @phpstan-ignore booleanAnd.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: booleanAnd.leftAlwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanAndConstantConditionRule [1]