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) {
return;
}
// At this point $x is 0
if (($x > 0) or ($x < 0)) { // ERROR: Result of or is always false.
echo 'nonzero';
}
}
Why is it reported? #
PHPStan determined that the result of the or expression is always false. Both comparisons evaluate to false for the narrowed type of $x, making the entire expression always false. This usually indicates dead code, a logic error, or a condition that has already been checked earlier in the control flow.
The or keyword is the low-precedence version of ||. This identifier specifically covers the or keyword; for ||, see booleanOr.alwaysFalse.
How to fix it #
Remove the redundant condition since the variable has already been narrowed:
function doFoo(int $x): void
{
if ($x !== 0) {
return;
}
- if (($x > 0) or ($x < 0)) {
- echo 'nonzero';
- }
+ echo 'zero';
}
Or fix the logic to test what was actually intended:
function doFoo(int $x): void
{
- if ($x !== 0) {
+ if ($x > 10) {
return;
}
- if (($x > 0) or ($x < 0)) {
+ if (($x > 5) or ($x < 0)) {
echo 'nonzero';
}
}
How to ignore this error #
You can use the identifier logicalOr.alwaysFalse to ignore this error using a comment:
// @phpstan-ignore logicalOr.alwaysFalse
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.alwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanOrConstantConditionRule [1]