Menu

← Back to possiblyImpure.*

Error Identifier: possiblyImpure.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 getValue(bool $useGlobal): int
{
	if ($useGlobal) {
		global $counter;
		return $counter;
	}

	return 42;
}

Why is it reported? #

A function or method marked as @phpstan-pure may use the global keyword. The global keyword accesses mutable global state, which is a side effect. Pure functions must not have side effects and must always return the same result for the same inputs.

When PHPStan cannot determine with certainty whether the global statement always executes (for example, because it is inside a conditional branch), it reports a “possibly impure” error rather than a definite impure one.

How to fix it #

Pass the dependency as a parameter instead of accessing it via global:

 /**
  * @phpstan-pure
  */
-function getValue(bool $useGlobal): int
+function getValue(bool $useGlobal, int $counter = 42): int
 {
-	if ($useGlobal) {
-		global $counter;
-		return $counter;
-	}
-
-	return 42;
+	return $useGlobal ? $counter : 42;
 }

Or remove the @phpstan-pure annotation if the function genuinely needs to access global state:

-/**
- * @phpstan-pure
- */
 function getValue(bool $useGlobal): int
 {
 	if ($useGlobal) {
 		global $counter;
 		return $counter;
 	}

 	return 42;
 }

How to ignore this error #

You can use the identifier possiblyImpure.global to ignore this error using a comment:

// @phpstan-ignore possiblyImpure.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: possiblyImpure.global

Rules that report this error #

  • PHPStan\Rules\Pure\PureFunctionRule [1]
  • PHPStan\Rules\Pure\PureMethodRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.