website/errors/impure.include.md
<?php declare(strict_types = 1);
/** @phpstan-pure */
function loadTranslations(string $locale): mixed
{
return include __DIR__ . '/translations/' . $locale . '.php';
}
A function or method marked as @phpstan-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.
Remove the @phpstan-pure annotation if the function needs to use include:
<?php declare(strict_types = 1);
-/** @phpstan-pure */
function loadTranslations(string $locale): mixed
{
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);
-/** @phpstan-pure */
-function loadTranslations(string $locale): mixed
-{
- return include __DIR__ . '/translations/' . $locale . '.php';
-}
+/** @phpstan-pure */
+function filterTranslations(array $translations, string $key): string
+{
+ return $translations[$key] ?? $key;
+}