Error Identifier: booleanOr.alwaysTrue
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
{
$result = $i >= 0 || $i < 0;
}
Why is it reported? #
The result of the || (boolean OR) expression always evaluates to true. This happens when at least one side of the operator is guaranteed to be truthy regardless of the input. In the example above, every integer is either greater than or equal to zero, or less than zero, so the entire expression is always true. This usually indicates a logic error, a redundant check, or dead code.
How to fix it #
Fix the logic to produce a meaningful condition:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $result = $i >= 0 || $i < 0;
+ $result = $i >= 0;
}
Or remove the redundant expression entirely:
<?php declare(strict_types = 1);
-function doFoo(int $i): void
+function doFoo(int $i): true
{
- $result = $i >= 0 || $i < 0;
+ return true;
}
How to ignore this error #
You can use the identifier booleanOr.alwaysTrue to ignore this error using a comment:
// @phpstan-ignore booleanOr.alwaysTrue
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.alwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanOrConstantConditionRule [1]