Menu

← Back to impure.*

Error Identifier: impure.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);

#[\Pure]
function loadTranslations(string $locale): array
{
	return include __DIR__ . '/translations/' . $locale . '.php';
}

Why is it reported? #

A function or method marked as pure must not have side effects and must depend only on its parameters. Using include or include_once inside a pure function is an impure operation because it reads a file from disk and executes its contents. File system access is inherently impure since the result can vary depending on external state such as file contents, file existence, or file permissions.

How to fix it #

Remove the #[\Pure] attribute if the function needs to use include:

 <?php declare(strict_types = 1);
 
-#[\Pure]
 function loadTranslations(string $locale): array
 {
 	return include __DIR__ . '/translations/' . $locale . '.php';
 }

Alternatively, restructure the code so that the file loading happens outside the pure function, and pass the data as a parameter:

 <?php declare(strict_types = 1);
 
-#[\Pure]
-function loadTranslations(string $locale): array
-{
-	return include __DIR__ . '/translations/' . $locale . '.php';
-}
+#[\Pure]
+function filterTranslations(array $translations, string $key): string
+{
+	return $translations[$key] ?? $key;
+}

How to ignore this error #

You can use the identifier impure.include to ignore this error using a comment:

// @phpstan-ignore impure.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: impure.include

Rules that report this error #

  • PHPStan\Rules\Pure\PureFunctionRule [1]
  • PHPStan\Rules\Pure\PureMethodRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.