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(bool $c): int
{
$a = 1;
if ($c) {
$a = 1;
}
return $a;
}
Why is it reported? #
The assignment $a = 1 inside the if block gives $a a value it already has, so it changes nothing. PHPStan tracks that $a is 1 at that point, making the second assignment redundant.
A redundant assignment usually signals a copy-paste mistake or a condition that was meant to assign a different value.
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 redundant assignment:
function doFoo(bool $c): int
{
$a = 1;
- if ($c) {
- $a = 1;
- }
return $a;
}
Or, if the branch was meant to assign a different value, correct it:
$a = 1;
if ($c) {
- $a = 1;
+ $a = 2;
}
How to ignore this error #
You can use the identifier assign.redundant to ignore this error using a comment:
// @phpstan-ignore assign.redundant
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.redundant
Rules that report this error #
- PHPStan\Rules\DeadCode\UnusedVariableRule [1]