Error Identifier: booleanOr.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 || $x >= 0) {
// ...
}
}
Why is it reported? #
The right side of a || (or or) 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, when $x < 0 is false, then $x >= 0 must be true, so the right side is always true when it is reached.
How to fix it #
Review the logic of the condition. The right side being always true usually means the condition is redundant and can be simplified:
<?php declare(strict_types = 1);
function doFoo(int $x): void
{
- if ($x < 0 || $x >= 0) {
+ if (true) {
// ...
}
}
Or the condition may contain a logic error and one of the sides needs to be adjusted:
<?php declare(strict_types = 1);
function doFoo(int $x): void
{
- if ($x < 0 || $x >= 0) {
+ if ($x < 0 || $x > 10) {
// ...
}
}
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]