Error Identifier: impure.functionCall
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);
/** @phpstan-pure */
function computeAndLog(int $value): int
{
file_put_contents('/tmp/log.txt', (string) $value);
return $value * 2;
}
Why is it reported? #
An impure function is called inside a function or method marked as @phpstan-pure. Pure functions must not have side effects – they should only compute and return a value based on their inputs. Calling an impure function (one that performs I/O, modifies global state, etc.) violates this contract.
In the example above, file_put_contents() writes to a file, which is a side effect, making the function impure despite the @phpstan-pure annotation.
How to fix it #
Remove the impure function call from the pure function:
<?php declare(strict_types = 1);
/** @phpstan-pure */
function compute(int $value): int
{
- file_put_contents('/tmp/log.txt', (string) $value);
-
return $value * 2;
}
Or remove the purity annotation if the function genuinely needs to perform side effects:
<?php declare(strict_types = 1);
-/** @phpstan-pure */
function computeAndLog(int $value): int
{
file_put_contents('/tmp/log.txt', (string) $value);
return $value * 2;
}
How to ignore this error #
You can use the identifier impure.functionCall to ignore this error using a comment:
// @phpstan-ignore impure.functionCall
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.functionCall