Menu

← Back to closure.*

Error Identifier: closure.useDuplicate

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);

$fn = function (int $foo, string $bar, bool $baz) use ($baz): bool {
    return $baz;
};

Why is it reported? #

A closure’s use clause imports a variable that has the same name as one of the closure’s parameters. This is not allowed in PHP because it would be ambiguous which variable $baz refers to inside the closure body – the parameter or the captured variable. PHP raises a fatal error in this situation.

How to fix it #

Remove the conflicting variable from the use clause:

 <?php declare(strict_types = 1);
 
-$fn = function (int $foo, string $bar, bool $baz) use ($baz): bool {
+$fn = function (int $foo, string $bar, bool $baz): bool {
     return $baz;
 };

Or rename the parameter to avoid the conflict:

 <?php declare(strict_types = 1);
 
-$fn = function (int $foo, string $bar, bool $baz) use ($baz): bool {
-    return $baz;
+$fn = function (int $foo, string $bar, bool $flag) use ($baz): bool {
+    return $flag || $baz;
 };

Non-ignorable error #

This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.

Rules that report this error #

  • PHPStan\Rules\Functions\InvalidLexicalVariablesInClosureUseRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.