Error Identifier: postInc.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.
Code example #
<?php declare(strict_types = 1);
$value = false;
$value++; // ERROR: Only numeric types are allowed in post-increment, false given.
Why is it reported? #
The post-increment operator ($variable++) is intended for numeric types. Using it on non-numeric values like false, null, objects, or union types containing non-numeric members leads to unexpected behavior. In PHP, incrementing null produces 1, incrementing false produces 1 (in PHP 8.3+, this is deprecated), and incrementing objects or other non-numeric types is either undefined or produces warnings.
This rule is provided by the phpstan-strict-rules package.
How to fix it #
Use the post-increment operator only on numeric types (int, float, or numeric strings):
<?php declare(strict_types = 1);
-$value = false;
+$value = 0;
$value++;
If the variable might be non-numeric, initialize it properly before incrementing:
<?php declare(strict_types = 1);
-$value = null;
-$value++;
+$value = 0;
+$value++;
How to ignore this error #
You can use the identifier postInc.nonNumeric to ignore this error using a comment:
// @phpstan-ignore postInc.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: postInc.nonNumeric
Rules that report this error #
- PHPStan\Rules\Operators\OperandInArithmeticIncrementOrDecrementRule [1] phpstan/phpstan-strict-rules