Error Identifier: preInc.nonNumeric
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.
This error is reported by phpstan/phpstan-strict-rules.
Code example #
<?php declare(strict_types = 1);
function next(string $letter): string
{
return ++$letter;
}
Why is it reported? #
The ++ (pre-increment) operator is being used on a value that is not numeric. While PHP allows incrementing strings (e.g., 'a' becomes 'b'), this behaviour can be surprising and error-prone. The strict rules require that only numeric types (int or float) are used with the increment operator to prevent unintended results.
How to fix it #
Use an explicit operation that makes the intent clear:
<?php declare(strict_types = 1);
function next(string $letter): string
{
- return ++$letter;
+ return chr(ord($letter) + 1);
}
Or ensure the variable is typed as int if you intend numeric increment:
<?php declare(strict_types = 1);
-function next(string $letter): string
+function next(int $counter): int
{
- return ++$letter;
+ return ++$counter;
}
How to ignore this error #
You can use the identifier preInc.nonNumeric to ignore this error using a comment:
// @phpstan-ignore preInc.nonNumeric
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: preInc.nonNumeric
Rules that report this error #
- PHPStan\Rules\Operators\OperandInArithmeticIncrementOrDecrementRule [1] phpstan/phpstan-strict-rules