Error Identifier: booleanAnd.alwaysTrue
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
{
$one = 1;
$result = $one && $i;
}
Why is it reported? #
The result of the && (boolean AND) expression always evaluates to true. This happens when both sides of the operator are always truthy, making the condition constant. In this example, $one is always 1 (truthy) and the result of the entire && expression is always true when used outside a first-level statement. This usually indicates a logic error, a redundant check, or dead code.
How to fix it #
Remove the redundant operand if the condition is always true:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $one = 1;
- $result = $one && $i;
+ $result = (bool) $i;
}
Or fix the logic to use the correct variable so the result is not always the same:
<?php declare(strict_types = 1);
-function doFoo(int $i): void
+function doFoo(int $i, bool $flag): void
{
- $one = 1;
- $result = $one && $i;
+ $result = $flag && $i;
}
How to ignore this error #
You can use the identifier booleanAnd.alwaysTrue to ignore this error using a comment:
// @phpstan-ignore booleanAnd.alwaysTrue
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.alwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanAndConstantConditionRule [1]