website/errors/method.impure.md
<?php declare(strict_types = 1);
class Foo
{
/** @phpstan-pure */
public function calculate(): int
{
return 1;
}
}
class Bar extends Foo
{
/** @phpstan-impure */
public function calculate(): int // ERROR: Impure method Bar::calculate() overrides pure method Foo::calculate().
{
return random_int(0, 100);
}
}
When a parent class or interface declares a method as pure (@phpstan-pure), all overriding methods must also be pure. A pure method guarantees it has no side effects and its return value depends only on its arguments. Code that calls the parent method relies on this guarantee — if a child class introduces side effects or non-deterministic behavior, it violates the contract.
Remove the @phpstan-impure annotation if the method is actually pure:
class Bar extends Foo
{
- /** @phpstan-impure */
+ /** @phpstan-pure */
public function calculate(): int
{
- return random_int(0, 100);
+ return 42;
}
}
If the method genuinely needs to be impure, reconsider the parent's purity contract. Remove @phpstan-pure from the parent method if purity cannot be guaranteed across all implementations:
class Foo
{
- /** @phpstan-pure */
public function calculate(): int
{
return 1;
}
}
If the parent class uses @phpstan-all-methods-pure, override the specific method in the parent instead:
+/** @phpstan-all-methods-pure */
class Foo
{
+ /** @phpstan-impure */
public function calculate(): int
{
return 1;
}
}