website/errors/impure.global.md
<?php declare(strict_types = 1);
/**
* @phpstan-pure
*/
function getDbConnection(): object
{
global $db; // ERROR: Impure global variable in pure function getDbConnection().
return $db;
}
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.
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;
}