Error Identifier: logicalAnd.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(): void
{
$zero = 0;
if ($zero and rand(0, 1)) {
// unreachable
}
}
Why is it reported? #
The left side of the and expression always evaluates to false. Since and short-circuits, the right side is never evaluated and the entire expression is always false. The branch body is dead code. This typically indicates a logic error or a variable that was not assigned the intended value.
How to fix it #
Fix the left-hand condition so it can be true:
function doFoo(): void
{
- $zero = 0;
- if ($zero and rand(0, 1)) {
+ $value = rand(0, 1);
+ if ($value and rand(0, 1)) {
// ...
}
}
Or remove the dead branch entirely:
function doFoo(): void
{
$zero = 0;
- if ($zero and rand(0, 1)) {
- // unreachable
- }
}
How to ignore this error #
You can use the identifier logicalAnd.leftAlwaysFalse to ignore this error using a comment:
// @phpstan-ignore logicalAnd.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: logicalAnd.leftAlwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanAndConstantConditionRule [1]