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 loadConfig(string $path): mixed
{
return require $path;
}
Why is it reported? #
A function marked as @phpstan-pure must not have side effects and must depend only on its parameters. Using require or require_once inside a pure function is a side effect because it reads a file from disk and executes its contents, which can produce different results depending on external state.
How to fix it #
Remove the @phpstan-pure annotation if the function needs to use require:
<?php declare(strict_types = 1);
-/** @phpstan-pure */
function loadConfig(string $path): mixed
{
return require $path;
}
Alternatively, restructure the code so that the file loading happens outside the pure function, and pass the data as a parameter.
How to ignore this error #
You can use the identifier impure.require to ignore this error using a comment:
// @phpstan-ignore impure.require
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.require