Error Identifier: logicalAnd.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 and $i) { // ERROR: Left side of and is always true.
echo 'entered';
}
}
Why is it reported? #
The left side of the and operator always evaluates to true. In this example, $one is always 1, which is truthy in PHP, so the left operand of the and expression is always true. This usually indicates redundant logic, a copy-paste mistake, or a wrong variable being checked. The condition is equivalent to just checking the right side alone.
How to fix it #
Remove the redundant left side if it is not needed:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $one = 1;
- if ($one and $i) {
+ if ($i) {
echo 'entered';
}
}
Or fix the logic to use the correct variable:
<?php declare(strict_types = 1);
-function doFoo(int $i): void
+function doFoo(int $i, int $j): void
{
- $one = 1;
- if ($one and $i) {
+ if ($j and $i) {
echo 'entered';
}
}
How to ignore this error #
You can use the identifier logicalAnd.leftAlwaysTrue to ignore this error using a comment:
// @phpstan-ignore logicalAnd.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: logicalAnd.leftAlwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanAndConstantConditionRule [1]