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(): int
{
$a = 1;
$a = 2;
return $a;
}
Why is it reported? #
The value 1 assigned to $a is never read — the variable is overwritten by $a = 2 before its first value is ever used. This is a dead store: the assignment computes a value that no code observes.
The same applies to writes to an array offset, such as $a['x'] = 1; followed by $a['x'] = 2; or $a = []; before $a['x'] is read.
Unlike variable.unused, the variable itself is used later; only this particular assignment is redundant. Unlike assign.unused, another assignment replaces the value before it can be read. Dead stores like this often point to a logic error, such as forgetting to use the first value or an accidental overwrite.
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 assignment whose value is never read:
function doFoo(): int
{
- $a = 1;
$a = 2;
return $a;
}
Or, if the first value should have been used, fix the logic so it is read before being overwritten:
function doFoo(): int
{
$a = 1;
+ echo $a;
$a = 2;
return $a;
}
How to ignore this error #
You can use the identifier assign.overwritten to ignore this error using a comment:
// @phpstan-ignore assign.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: assign.overwritten
Rules that report this error #
- PHPStan\Rules\DeadCode\UnusedVariableRule [1]