Error Identifier: logicalOr.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 $i, stdClass $std): void
{
if ($i or $std) {
// ...
}
}
Why is it reported? #
The right side of the or operator always evaluates to true. In the example, $std is of type stdClass, which is an object and is always truthy in a boolean context. This means the right side of the or expression can never be false, making the condition partially redundant.
How to fix it #
If the condition is truly meant to always be true at that point, simplify it:
function doFoo(int $i, stdClass $std): void
{
- if ($i or $std) {
- // ...
- }
+ // ...
}
Or fix the logic to use the intended variable or comparison:
-function doFoo(int $i, stdClass $std): void
+function doFoo(int $i, ?stdClass $std): void
{
if ($i or $std) {
// ...
}
}
How to ignore this error #
You can use the identifier logicalOr.rightAlwaysTrue to ignore this error using a comment:
// @phpstan-ignore logicalOr.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: logicalOr.rightAlwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanOrConstantConditionRule [1]