Error Identifier: preDec.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);
function doFoo(string $s): void
{
--$s;
}
Why is it reported? #
The pre-decrement operator -- is intended for numeric types. Applying it to a non-numeric value such as string, bool, null, or an object does not produce meaningful results and is likely a bug.
This error is reported by phpstan/phpstan-strict-rules.
How to fix it #
Convert the value to an appropriate numeric type before decrementing, or use explicit arithmetic:
<?php declare(strict_types = 1);
-function doFoo(string $s): void
+function doFoo(int $count): void
{
- --$s;
+ --$count;
}
How to ignore this error #
You can use the identifier preDec.nonNumeric to ignore this error using a comment:
// @phpstan-ignore preDec.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: preDec.nonNumeric
Rules that report this error #
- PHPStan\Rules\Operators\OperandInArithmeticIncrementOrDecrementRule [1] phpstan/phpstan-strict-rules