Error Identifier: possiblyImpure.exit
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 getValue(int $input): int
{
$closure = function () {
exit();
};
$closure();
return $input * 2;
}
Why is it reported? #
The function or method is marked as @phpstan-pure but may call exit or die. These constructs terminate the entire PHP process, which is a side effect. Pure functions must not have side effects; they should only compute and return a value based on their inputs.
How to fix it #
Remove the exit/die call from the pure function, or remove the @phpstan-pure annotation if process termination is intentional:
/**
- * @phpstan-pure
+ * @phpstan-impure
*/
function getValue(int $input): int
{
$closure = function () {
exit();
};
$closure();
return $input * 2;
}
How to ignore this error #
You can use the identifier possiblyImpure.exit to ignore this error using a comment:
// @phpstan-ignore possiblyImpure.exit
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.exit