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 Calculator
{
/** @phpstan-impure */
public function add(int $a, int $b): int
{
return $a + $b;
}
}
Why is it reported? #
A method is marked as @phpstan-impure but PHPStan’s analysis found no actual side effects in its body. The method does not perform I/O, modify external state, or call other impure code. Marking a side-effect-free method as impure is misleading – callers cannot benefit from purity optimizations, and the annotation does not match the actual behavior.
This is only reported for methods that cannot be overridden – either in a final class, or when the method itself is final. For non-final methods, a subclass might introduce side effects, so PHPStan does not report this.
How to fix it #
Remove the @phpstan-impure annotation since the method has no side effects:
-/** @phpstan-impure */
+/** @phpstan-pure */
public function add(int $a, int $b): int
{
return $a + $b;
}
Or simply remove the annotation entirely and let PHPStan infer purity:
-/** @phpstan-impure */
public function add(int $a, int $b): int
{
return $a + $b;
}
How to ignore this error #
You can use the identifier impureMethod.pure to ignore this error using a comment:
// @phpstan-ignore impureMethod.pure
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: impureMethod.pure