Error Identifier: possiblyImpure.include
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 Config
{
/** @phpstan-pure */
public function load(string $file): array
{
return include $file;
}
}
Why is it reported? #
The function or method is marked as pure (via @phpstan-pure), but it uses include or require inside its body. Including or requiring files is a side effect because it executes arbitrary code from the filesystem, which may modify global state, define functions or classes, or produce output. This makes the function possibly impure.
A pure function must have no side effects and must depend only on its arguments.
How to fix it #
Remove the include/require statement and pass the data as a parameter instead:
<?php declare(strict_types = 1);
class Config
{
- /** @phpstan-pure */
- public function load(string $file): array
+ /** @phpstan-pure */
+ public function load(array $data): array
{
- return include $file;
+ return $data;
}
}
Alternatively, if the function genuinely needs to include files, remove the @phpstan-pure annotation:
<?php declare(strict_types = 1);
class Config
{
- /** @phpstan-pure */
public function load(string $file): array
{
return include $file;
}
}
How to ignore this error #
You can use the identifier possiblyImpure.include to ignore this error using a comment:
// @phpstan-ignore possiblyImpure.include
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.include