Error Identifier: preDec.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);
$obj = new stdClass();
--$obj;
$arr = [];
--$arr;
Why is it reported? #
The pre-decrement operator (--) can only be used on variables of type int, float, string (numeric strings only), or null. Using it on other types such as objects, arrays, booleans, or resources is not a valid operation and may lead to unexpected behaviour or deprecation notices in newer PHP versions.
How to fix it #
Ensure the variable is of a type that supports the decrement operator. If the value is supposed to be a number, assign or cast it to the correct type first:
<?php declare(strict_types = 1);
-$value = new stdClass();
---$value;
+$value = 1;
+--$value;
If the variable is a string that should be decremented, use str_decrement() (PHP 8.3+) instead:
<?php declare(strict_types = 1);
$str = 'b';
---$str;
+$str = str_decrement($str);
How to ignore this error #
You can use the identifier preDec.type to ignore this error using a comment:
// @phpstan-ignore preDec.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: preDec.type
Rules that report this error #
- PHPStan\Rules\Operators\InvalidIncDecOperationRule [1]