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);
/** @param mixed $v */
function sink($v): void
{
}
function doFoo(): void
{
$a = ['x' => 1, 'y' => 2];
sink($a['y']);
}
Why is it reported? #
The array assigned to $a stores a value under offset 'x', but only offset 'y' is ever read back from $a. The value under 'x' is a dead store: it is computed and kept, but no code observes it.
Unlike variable.unused, the array $a itself is used; only this particular offset is redundant. A dead offset often points to a leftover key or a logic error, such as reading the wrong offset name.
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 offset whose value is never read:
function doFoo(): void
{
- $a = ['x' => 1, 'y' => 2];
+ $a = ['y' => 2];
sink($a['y']);
}
Or read the offset if it was intended to be used:
function doFoo(): void
{
$a = ['x' => 1, 'y' => 2];
+ sink($a['x']);
sink($a['y']);
}
How to ignore this error #
You can use the identifier array.unusedOffset to ignore this error using a comment:
// @phpstan-ignore array.unusedOffset
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: array.unusedOffset
Rules that report this error #
- PHPStan\Rules\DeadCode\UnusedVariableRule [1]