Error Identifier: smallerOrEqual.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
{
if ($i < 2) {
if ($i <= 5) {
// ...
}
}
}
Why is it reported? #
The <= comparison is always true based on the types PHPStan has inferred for the operands. In the example above, after $i < 2 narrows $i to int<min, 1>, the comparison $i <= 5 is always true because any value less than 2 is also less than or equal to 5.
A comparison that is always true usually indicates a logic error, a redundant condition, or an overly constrained type.
How to fix it #
Fix the comparison to express the intended condition:
function doFoo(int $i): void
{
if ($i < 2) {
- if ($i <= 5) {
+ if ($i <= 0) {
// ...
}
}
}
Or remove the redundant condition:
function doFoo(int $i): void
{
if ($i < 2) {
- if ($i <= 5) {
- // ...
- }
+ // ...
}
}
If the condition is always true because of a PHPDoc type, and you believe the condition is still meaningful at runtime, the tip on the error will suggest setting treatPhpDocTypesAsCertain to false.
How to ignore this error #
You can use the identifier smallerOrEqual.alwaysTrue to ignore this error using a comment:
// @phpstan-ignore smallerOrEqual.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: smallerOrEqual.alwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\NumberComparisonOperatorsConstantConditionRule [1]