Menu

← Back to impure.*

Error Identifier: impure.static

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 counter(): int
{
	static $count = 0; // error: Impure static variable in pure function counter().

	return ++$count;
}

Why is it reported? #

The function or method is marked as @phpstan-pure, but it uses a static variable. Static variables persist their value between calls, which means the function has hidden state and its return value can change across calls even with the same arguments. This violates the definition of a pure function, which must always return the same result for the same inputs and have no side effects.

How to fix it #

If the function truly needs to be pure, remove the static variable and pass the state as a parameter instead:

 <?php declare(strict_types = 1);
 
 /**
  * @phpstan-pure
  */
-function counter(): int
+function increment(int $count): int
 {
-	static $count = 0;
-
-	return ++$count;
+	return $count + 1;
 }

If the function genuinely requires persistent state, it is not pure. Remove the @phpstan-pure annotation:

 <?php declare(strict_types = 1);
 
-/**
- * @phpstan-pure
- */
 function counter(): int
 {
 	static $count = 0;

 	return ++$count;
 }

How to ignore this error #

You can use the identifier impure.static to ignore this error using a comment:

// @phpstan-ignore impure.static
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.static

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.