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 positive-int $i */
function doFoo(int $i): void
{
if ($i > 0) {
echo 'always positive';
}
}
Why is it reported? #
The > comparison always evaluates to true based on the types of the operands. In this example, $i is a positive-int (always >= 1), so $i > 0 is always true.
This usually indicates redundant logic or a mistake in the comparison values. The condition adds no meaningful check and can be simplified.
How to fix it #
Remove the redundant condition if it is not needed:
<?php declare(strict_types = 1);
/** @param positive-int $i */
function doFoo(int $i): void
{
- if ($i > 0) {
- echo 'always positive';
- }
+ echo 'always positive';
}
Or fix the comparison to check the intended value:
<?php declare(strict_types = 1);
/** @param positive-int $i */
function doFoo(int $i): void
{
- if ($i > 0) {
+ if ($i > 10) {
echo 'large positive';
}
}
How to ignore this error #
You can use the identifier greater.alwaysTrue to ignore this error using a comment:
// @phpstan-ignore greater.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: greater.alwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\NumberComparisonOperatorsConstantConditionRule [1]