Error Identifier: impure.methodCall
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);
class Logger
{
/** @phpstan-impure */
public function log(string $message): void
{
echo $message;
}
}
class Calculator
{
private Logger $logger;
/** @phpstan-pure */
public function add(int $a, int $b): int
{
$this->logger->log('adding');
return $a + $b;
}
}
Why is it reported? #
A function or method marked as @phpstan-pure must not cause any side effects. Calling an impure method (one that performs I/O, modifies external state, or is marked @phpstan-impure) inside a pure function violates purity guarantees. Pure functions must only depend on their arguments and return a value without observable side effects.
How to fix it #
Remove the impure method call from the pure function:
/** @phpstan-pure */
public function add(int $a, int $b): int
{
- $this->logger->log('adding');
return $a + $b;
}
Or, if the side effect is intentional, remove the @phpstan-pure annotation:
-/** @phpstan-pure */
public function add(int $a, int $b): int
{
$this->logger->log('adding');
return $a + $b;
}
How to ignore this error #
You can use the identifier impure.methodCall to ignore this error using a comment:
// @phpstan-ignore impure.methodCall
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.methodCall