Menu

Error Identifier: assign.unused

← Back to assign.*

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
{
	$a = 1;
	echo $a;
	$a = 2;
}

Why is it reported? #

The value 2 assigned to $a is never read — no code after the assignment looks at $a again. This is a dead store: the assignment computes a value that no code observes.

Unlike variable.unused, the variable itself is read elsewhere; only this particular assignment is redundant. Unlike assign.overwritten, nothing replaces the value — it is simply never used. Dead stores often point to leftover code or a logic error, such as forgetting to use the new 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 assignment whose value is never read:

 function doFoo(): void
 {
 	$a = 1;
 	echo $a;
-	$a = 2;
 }

Or use the assigned value if it was intended to be read:

 function doFoo(): void
 {
 	$a = 1;
 	echo $a;
 	$a = 2;
+	echo $a;
 }

If you deliberately want to keep the assignment, prefix the variable name with an underscore. PHPStan ignores variables whose name starts with _.

How to ignore this error #

You can use the identifier assign.unused to ignore this error using a comment:

// @phpstan-ignore assign.unused
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.unused

Rules that report this error #

  • PHPStan\Rules\DeadCode\UnusedVariableRule [1]
Theme
A
© 2026 PHPStan s.r.o.