website/errors/impure.staticPropertyAccess.md
<?php declare(strict_types = 1);
class Counter
{
public static int $count = 0;
}
/**
* @phpstan-pure
*/
function getCount(): int
{
return Counter::$count; // ERROR: Impure static property access in pure function getCount().
}
A function or method marked as @phpstan-pure must not have side effects and must always return the same result for the same inputs. Accessing a static property inside a pure function is considered impure because static properties represent shared mutable state. Their values can change between function calls, which means the function's return value may differ even when called with the same arguments.
Pass the value as a parameter instead of reading a static property:
<?php declare(strict_types = 1);
/**
* @phpstan-pure
*/
-function getCount(): int
+function getCount(int $count): int
{
- return Counter::$count;
+ return $count;
}
Or remove the @phpstan-pure annotation if the function genuinely needs to access static properties:
<?php declare(strict_types = 1);
-/**
- * @phpstan-pure
- */
function getCount(): int
{
return Counter::$count;
}