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 increment(bool $flag): int
{
return ++$flag;
}
Why is it reported? #
The ++ (pre-increment) operator is being used on a value that is not numeric. While PHP allows incrementing non-numeric types like bool, 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 #
Ensure the variable is typed as int if you intend numeric increment:
<?php declare(strict_types = 1);
-function increment(bool $flag): int
+function increment(int $counter): int
{
- return ++$flag;
+ 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