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);
/** @param negative-int $i */
function doFoo(int $i): void
{
if ($i < 0) {
echo 'always negative';
}
}
Why is it reported? #
The < comparison is always true based on the types of the operands. This indicates that the condition is redundant because the left side is always strictly less than the right side given their possible values. Such comparisons often signal a logic error or an overly broad type.
In the example above, $i is a negative-int (always <= -1), so $i < 0 is always true.
How to fix it #
Remove the unnecessary condition:
/** @param negative-int $i */
function doFoo(int $i): void
{
- if ($i < 0) {
- echo 'always negative';
- }
+ echo 'always negative';
}
Or adjust the comparison to reflect the actual constraint you intend to enforce:
/** @param negative-int $i */
function doFoo(int $i): void
{
- if ($i < 0) {
+ if ($i < -10) {
echo 'very negative';
}
}
How to ignore this error #
You can use the identifier smaller.alwaysTrue to ignore this error using a comment:
// @phpstan-ignore smaller.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: smaller.alwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\NumberComparisonOperatorsConstantConditionRule [1]