Error Identifier: logicalAnd.rightAlwaysTrue
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 $x): void
{
if ($x > 0 and $x >= 0) { // ERROR: Right side of and is always true.
echo 'positive';
}
}
Why is it reported? #
PHPStan determined that the right side of the and expression always evaluates to true. This means the right operand does not contribute additional filtering to the condition – the result depends solely on the left side. This usually indicates redundant logic, a copy-paste mistake, or a condition that has been made unnecessary by earlier narrowing.
The and keyword is the low-precedence version of &&. This identifier specifically covers the and keyword; for &&, see booleanAnd.rightAlwaysTrue.
In the example above, when $x > 0 is true, $x >= 0 is necessarily also true, making the right side redundant.
How to fix it #
Remove the redundant right side if it is not needed:
<?php declare(strict_types = 1);
function doFoo(int $x): void
{
- if ($x > 0 and $x >= 0) {
+ if ($x > 0) {
echo 'positive';
}
}
Or fix the logic if the condition should test something different:
<?php declare(strict_types = 1);
function doFoo(int $x): void
{
- if ($x > 0 and $x >= 0) {
+ if ($x > 0 and $x < 100) {
echo 'positive';
}
}
How to ignore this error #
You can use the identifier logicalAnd.rightAlwaysTrue to ignore this error using a comment:
// @phpstan-ignore logicalAnd.rightAlwaysTrue
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.rightAlwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanAndConstantConditionRule [1]