Error Identifier: impure.shellExec

← Back to impure.*

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 listFiles(): string
{
	return (string) `ls`;
}

Why is it reported? #

A function or method marked as @phpstan-pure contains the backtick operator (`...`), which executes a shell command, the same as calling shell_exec(). Pure functions must not have side effects and their return value must depend only on their arguments. Running an external command interacts with the operating system, can change state outside the program, and its output can differ between calls, so the function is not pure.

How to fix it #

Move the shell execution out of the pure function and pass its result in as an argument:

 <?php declare(strict_types = 1);
 
 /** @phpstan-pure */
-function listFiles(): string
+function formatFileList(string $output): string
 {
-	return (string) `ls`;
+	return trim($output);
 }
+
+$files = formatFileList((string) `ls`);

If the function needs to run a shell command, remove the @phpstan-pure annotation:

 <?php declare(strict_types = 1);
 
-/** @phpstan-pure */
 function listFiles(): string
 {
 	return (string) `ls`;
 }

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\Pure\PureFunctionRule [1]
  • PHPStan\Rules\Pure\PureMethodRule [1]
  • PHPStan\Rules\Pure\PurePropertyHookRule [1]
Theme
© 2026 PHPStan s.r.o.