Error Identifier: closure.useSuperGlobal
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 () use ($_GET) {
return $_GET['key'];
};
Why is it reported? #
PHP does not allow using superglobal variables ($_GET, $_POST, $_SERVER, $_COOKIE, $_FILES, $_SESSION, $_REQUEST, $_ENV, $GLOBALS) as lexical variables in a closure use clause. Superglobals are already available in every scope, so importing them is unnecessary and produces a fatal error.
How to fix it #
Remove the superglobal from the use clause and access it directly inside the closure:
<?php declare(strict_types = 1);
-$fn = function () use ($_GET) {
+$fn = function () {
return $_GET['key'];
};
How to ignore this error #
You can use the identifier closure.useSuperGlobal to ignore this error using a comment:
// @phpstan-ignore closure.useSuperGlobal
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: closure.useSuperGlobal
Rules that report this error #
- PHPStan\Rules\Functions\InvalidLexicalVariablesInClosureUseRule [1]