Error Identifier: logicalOr.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 $i): void
{
$one = 1;
if ($one or $i) {
// ...
}
}
Why is it reported? #
The left side of the or operator is always true. Since $one is 1 (a truthy value), the condition will always evaluate to true regardless of the value of $i. The right side of or is never evaluated because PHP uses short-circuit evaluation.
This usually indicates a logic error, leftover debugging code, or a condition that has become redundant after refactoring.
How to fix it #
Fix the condition to test the intended value:
- if ($one or $i) {
+ if ($i > 0) {
// ...
}
Or remove the always-true left side if only the right side matters:
- if ($one or $i) {
+ if ($i) {
// ...
}
How to ignore this error #
You can use the identifier logicalOr.leftAlwaysTrue to ignore this error using a comment:
// @phpstan-ignore logicalOr.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: logicalOr.leftAlwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanOrConstantConditionRule [1]