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 = 0;
while (rand(0, 1)) {
$i++;
}
}
Why is it reported? #
The post-increment $i++ raises $i by one, and that new value is read — but only by the next iteration’s $i++, whose result no code ever observes. The increment feeds a closed computation that produces nothing.
This is different from postInc.unused, where the incremented value is never read at all. Here the value flows through further computation, but that computation is itself dead, so the whole chain has no effect. This usually points to a leftover counter or a logic error where the result was meant to be used.
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 dead computation if its result is not needed:
function doFoo(): void
{
- $i = 0;
- while (rand(0, 1)) {
- $i++;
- }
}
Or use the final value if it was intended to be read:
function doFoo(): void
{
$i = 0;
while (rand(0, 1)) {
$i++;
}
+ echo $i;
}
How to ignore this error #
You can use the identifier postInc.unusedFlow to ignore this error using a comment:
// @phpstan-ignore postInc.unusedFlow
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.unusedFlow
Rules that report this error #
- PHPStan\Rules\DeadCode\UnusedVariableRule [1]