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(): void
{
$i = 10;
--$i;
$i = 5;
echo $i;
}
Why is it reported? #
The pre-decrement --$i lowers $i by one, but that new value is never read — $i = 5 replaces it before any code looks at $i. The pre-decrement has no effect on the program. This usually indicates leftover code, or a logic error where the decremented value was meant to be used before the variable is reassigned.
This is different from preDec.unused, where the new value is simply never read. Here another assignment overwrites it first.
This rule is part of PHPStan’s dead code analysis. It is reported at rule level 4 and above, and is currently part of Bleeding Edge.
How to fix it #
Remove the pre-decrement if the new value is not needed:
function doFoo(): void
{
$i = 10;
- --$i;
$i = 5;
echo $i;
}
Or use the new value before the variable is reassigned:
function doFoo(): void
{
$i = 10;
--$i;
+ echo $i;
$i = 5;
echo $i;
}
How to ignore this error #
You can use the identifier preDec.overwritten to ignore this error using a comment:
// @phpstan-ignore preDec.overwritten
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.overwritten
Rules that report this error #
- PHPStan\Rules\DeadCode\UnusedVariableRule [1]