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
{
$f = false;
if ($i > 0 || $f) {
echo 'right always false';
}
}
Why is it reported? #
The right side of the || operator always evaluates to false. This means the right operand never contributes 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.
In the example above, $f is always false, so the right side of || is redundant and the entire condition is equivalent to just $i > 0.
How to fix it #
Remove the redundant right-side condition:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $f = false;
- if ($i > 0 || $f) {
+ if ($i > 0) {
echo 'right always false';
}
}
Or fix the right side to use a meaningful condition:
<?php declare(strict_types = 1);
-function doFoo(int $i): void
+function doFoo(int $i, bool $flag): void
{
- $f = false;
- if ($i > 0 || $f) {
+ if ($i > 0 || $flag) {
// ...
}
}
How to ignore this error #
You can use the identifier booleanOr.rightAlwaysFalse to ignore this error using a comment:
// @phpstan-ignore booleanOr.rightAlwaysFalse
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: booleanOr.rightAlwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanOrConstantConditionRule [1]