Error Identifier: postInc.type
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 = new \stdClass();
$value++; // ERROR: Cannot use ++ on stdClass.
Why is it reported? #
The post-increment operator ($value++) is only valid for numeric types (int, float), strings, and in some PHP versions null and bool. Attempting to increment a value of a type that does not support this operation will either produce unexpected results or trigger a deprecation/error depending on the PHP version.
How to fix it #
Ensure the variable has a type that supports the increment operator:
<?php declare(strict_types = 1);
-$value = new \stdClass();
-$value++;
+$value = 0;
+$value++;
Or perform the arithmetic explicitly on a supported type:
<?php declare(strict_types = 1);
-$value = new \stdClass();
-$value++;
+$value = 1;
+$value = $value + 1;
How to ignore this error #
You can use the identifier postInc.type to ignore this error using a comment:
// @phpstan-ignore postInc.type
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.type
Rules that report this error #
- PHPStan\Rules\Operators\InvalidIncDecOperationRule [1]