Error Identifier: booleanOr.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 $x): void
{
if ($x >= 0 || $x > 5) {
// ...
}
}
Why is it reported? #
The left side of a || (or or) expression always evaluates to true. Because || uses short-circuit evaluation, when the left side is always true, the right side is never evaluated, making the entire expression pointless.
In the example above, if $x >= 0 is always true in the given scope, the condition $x > 5 on the right side is unreachable.
How to fix it #
Review the logic of the condition. The left side being always true usually means one of the following:
The condition is redundant and can be simplified:
<?php declare(strict_types = 1);
function doFoo(int $x): void
{
- if ($x >= 0 || $x > 5) {
+ if ($x >= 0) {
// ...
}
}
Or the condition contains a logic error and should use && instead:
<?php declare(strict_types = 1);
function doFoo(int $x): void
{
- if ($x >= 0 || $x > 5) {
+ if ($x >= 0 && $x > 5) {
// ...
}
}
How to ignore this error #
You can use the identifier booleanOr.leftAlwaysTrue to ignore this error using a comment:
// @phpstan-ignore booleanOr.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: booleanOr.leftAlwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanOrConstantConditionRule [1]