Error Identifier: possiblyImpure.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 Formatter
{
public function format(string $value): string
{
return strtoupper($value);
}
}
class Service
{
/**
* @phpstan-pure
*/
public function process(Formatter $formatter): string
{
return $formatter->format('hello'); // ERROR: Possibly impure call to method Formatter::format() in pure method Service::process().
}
}
Why is it reported? #
A function or method marked as @phpstan-pure calls another method whose purity is unknown. The called method might have side effects, which would make the calling function impure. PHPStan cannot guarantee that the method call is pure, so it reports it as possibly impure.
A pure function must not have any side effects and must always return the same result for the same inputs.
How to fix it #
Mark the called method as @phpstan-pure so PHPStan knows it is safe to call from a pure context:
<?php declare(strict_types = 1);
class Formatter
{
+ /** @phpstan-pure */
public function format(string $value): string
{
return strtoupper($value);
}
}
Or remove the @phpstan-pure annotation from the calling method if it does not need to be pure:
<?php declare(strict_types = 1);
class Service
{
- /**
- * @phpstan-pure
- */
public function process(Formatter $formatter): string
{
return $formatter->format('hello');
}
}
How to ignore this error #
You can use the identifier possiblyImpure.methodCall to ignore this error using a comment:
// @phpstan-ignore possiblyImpure.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: possiblyImpure.methodCall