Menu

Error Identifier: catch.unusedVariableFlow

← Back to catch.*

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
{
	try {
		mightThrow();
	} catch (\Exception $e) {
		while (rand(0, 1)) {
			$e = rand(0, 1) ? $e : null;
		}
	}
}

function mightThrow(): void
{
}

Why is it reported? #

The catch block binds the caught exception to $e, and that value is read — but only by $e = rand(0, 1) ? $e : null, whose result no code ever observes. The exception feeds a closed computation that produces nothing.

A catch variable that is never read at all is not reported. Here, however, the value flows through further computation that is itself dead, so binding the exception has no effect. This usually points to a logic error where the exception was meant to be used.

This is only reported on PHP 8.0 and later, where non-capturing catches are available.

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 #

Use the exception in the catch block if it was intended to be read:

 	try {
 		mightThrow();
 	} catch (\Exception $e) {
-		while (rand(0, 1)) {
-			$e = rand(0, 1) ? $e : null;
-		}
+		echo $e->getMessage();
 	}

Or use a non-capturing catch (PHP 8.0+) when the exception is not needed:

 	try {
 		mightThrow();
-	} catch (\Exception $e) {
-		while (rand(0, 1)) {
-			$e = rand(0, 1) ? $e : null;
-		}
+	} catch (\Exception) {
+		// handle failure
 	}

How to ignore this error #

You can use the identifier catch.unusedVariableFlow to ignore this error using a comment:

// @phpstan-ignore catch.unusedVariableFlow
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: catch.unusedVariableFlow

Rules that report this error #

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