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
{
$t = true;
if ($i > 0 || $t) {
echo 'right always true';
}
}
Why is it reported? #
The right side of a || expression always evaluates to true. This means the entire expression is always true regardless of what the left side evaluates to, which usually indicates a logic error or a redundant condition.
In the example above, $t is always true, so the right side of || is always true when it is evaluated (i.e., when $i > 0 is false). This makes the whole condition always true.
How to fix it #
Simplify the condition if it is redundant:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $t = true;
- if ($i > 0 || $t) {
- echo 'right always true';
- }
+ echo 'right always true';
}
Or fix the right side so it can evaluate to either true or false:
<?php declare(strict_types = 1);
-function doFoo(int $i): void
+function doFoo(int $i, bool $flag): void
{
- $t = true;
- if ($i > 0 || $t) {
+ if ($i > 0 || $flag) {
// ...
}
}
How to ignore this error #
You can use the identifier booleanOr.rightAlwaysTrue to ignore this error using a comment:
// @phpstan-ignore booleanOr.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: booleanOr.rightAlwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanOrConstantConditionRule [1]