Error Identifier: impure.global
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 getDbConnection(): object
{
global $db; // ERROR: Impure global variable in pure function getDbConnection().
return $db;
}
Why is it reported? #
A function or method marked as @phpstan-pure must not have side effects and must always return the same result for the same inputs. Using the global keyword inside a pure function is an impure operation because it accesses mutable global state. The value of a global variable can change between calls, which means the function’s return value may differ even when called with the same arguments. This violates the contract of a pure function.
How to fix it #
Pass the dependency as a parameter instead of accessing it via global:
<?php declare(strict_types = 1);
/**
* @phpstan-pure
*/
-function getDbConnection(): object
+function getDbConnection(object $db): object
{
- global $db;
-
return $db;
}
Or remove the @phpstan-pure annotation if the function genuinely needs to access global state:
<?php declare(strict_types = 1);
-/**
- * @phpstan-pure
- */
function getDbConnection(): object
{
global $db;
return $db;
}
How to ignore this error #
You can use the identifier impure.global to ignore this error using a comment:
// @phpstan-ignore impure.global
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.global