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);
final class Config
{
private int $backing = 1;
public int $counter {
get {
return $this->backing;
}
}
}
final class Service
{
/** @phpstan-pure */
public function read(Config $config): int
{
return $config->counter;
}
}
Why is it reported? #
A function or method marked as @phpstan-pure accesses a property whose hook (PHP 8.4+) has an unknown purity. PHPStan cannot prove the hook is free of side effects, so it cannot guarantee that reading or writing the property is a pure operation.
A pure function must not have any side effects and must always return the same result for the same inputs. Related identifiers: impure.propertyHookCall is reported when the hook is known to be impure, possiblyImpure.methodCall covers the same situation for regular method calls.
How to fix it #
Mark the hook as @phpstan-pure so PHPStan knows it is safe to access from a pure context:
public int $counter {
+ /** @phpstan-pure */
get {
return $this->backing;
}
}
Or remove the @phpstan-pure annotation from the accessing method if it does not need to be pure:
-/** @phpstan-pure */
public function read(Config $config): int
{
return $config->counter;
}
How to ignore this error #
You can use the identifier possiblyImpure.propertyHookCall to ignore this error using a comment:
// @phpstan-ignore possiblyImpure.propertyHookCall
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.propertyHookCall