Error Identifier: impure.superglobal
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);
/** @phpstan-pure */
function getParam(string $key): ?string
{
return $_GET[$key] ?? null;
}
Why is it reported? #
A function marked as @phpstan-pure accesses a PHP superglobal variable ($_GET, $_POST, $_SESSION, $_SERVER, $_COOKIE, $_FILES, $_ENV, $_REQUEST, or $GLOBALS). Superglobal access is impure because it reads from global mutable state. Pure functions should only depend on their input parameters.
How to fix it #
Pass the needed value as a parameter instead:
<?php declare(strict_types = 1);
-/** @phpstan-pure */
-function getParam(string $key): ?string
+/** @phpstan-pure */
+function getParam(array $params, string $key): ?string
{
- return $_GET[$key] ?? null;
+ return $params[$key] ?? null;
}
Or remove the @phpstan-pure annotation if the function needs to access superglobals.
How to ignore this error #
You can use the identifier impure.superglobal to ignore this error using a comment:
// @phpstan-ignore impure.superglobal
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: impure.superglobal