Error Identifier: greater.alwaysFalse
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
{
if ($i > 5) {
if ($i > 2) {
// always true
}
}
}
function doBar(int $j): void
{
if ($j >= 2 && $j < 5) {
if ($j > 8) {
// always false
}
}
}
Why is it reported? #
The comparison using the > operator is always false based on the types PHPStan has inferred. In the second example, inside the if ($j >= 2 && $j < 5) block, the value of $j is in the range [2, 4], so $j > 8 can never be true. The code inside the condition is dead code and will never execute.
This usually indicates a logic error where the condition does not match the developer’s intent, or the code structure has been refactored and the condition is now redundant.
How to fix it #
Fix the comparison to match the intended logic:
if ($j >= 2 && $j < 5) {
- if ($j > 8) {
+ if ($j > 3) {
// ...
}
}
Or remove the condition entirely if the dead code is not needed:
if ($j >= 2 && $j < 5) {
- if ($j > 8) {
- // ...
- }
+ // ...
}
How to ignore this error #
You can use the identifier greater.alwaysFalse to ignore this error using a comment:
// @phpstan-ignore greater.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: greater.alwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\NumberComparisonOperatorsConstantConditionRule [1]