Error Identifier: impure.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);
class Foo
{
/** @phpstan-pure */
public function doFoo(): string
{
exit('fatal error');
}
}
Why is it reported? #
The exit language construct is used inside a function or method marked as @phpstan-pure. Pure functions must not have side effects – they should only compute and return a value based on their inputs. Calling exit terminates the entire PHP process, which is a significant side effect.
How to fix it #
Remove exit from the pure function by throwing an exception instead, or remove the @phpstan-pure annotation if the function genuinely needs to terminate the process:
class Foo
{
/** @phpstan-pure */
public function doFoo(): string
{
- exit('fatal error');
+ throw new \RuntimeException('fatal error');
}
}
Or remove the purity annotation:
class Foo
{
- /** @phpstan-pure */
public function doFoo(): string
{
exit('fatal error');
}
}
How to ignore this error #
You can use the identifier impure.exit to ignore this error using a comment:
// @phpstan-ignore impure.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: impure.exit