Error Identifier: booleanAnd.leftAlwaysTrue
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;
if ($one && $i) {
// ...
}
}
Why is it reported? #
The left side of the && (boolean AND) expression always evaluates to true. When the left operand is always truthy, it has no effect on the result of the expression – the outcome depends entirely on the right operand. This indicates a redundant check, a logic error, or a variable that should hold a different value.
In the example above, $one is always 1, which is truthy in PHP, so the left side of && is always true and the condition is equivalent to just if ($i).
How to fix it #
Remove the redundant left operand:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $one = 1;
- if ($one && $i) {
+ if ($i) {
// ...
}
}
Or fix the logic to use a variable whose value is not always truthy:
<?php declare(strict_types = 1);
-function doFoo(int $i): void
+function doFoo(int $i, bool $flag): void
{
- $one = 1;
- if ($one && $i) {
+ if ($flag && $i) {
// ...
}
}
How to ignore this error #
You can use the identifier booleanAnd.leftAlwaysTrue to ignore this error using a comment:
// @phpstan-ignore booleanAnd.leftAlwaysTrue
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.leftAlwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanAndConstantConditionRule [1]