Error Identifier: impure.print
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 formatAndPrint(string $name): int
{
return print 'Hello, ' . $name;
}
Why is it reported? #
The print 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. Using print outputs text to the standard output, which is a side effect.
How to fix it #
Remove the print call from the pure function and return the formatted string instead:
<?php declare(strict_types = 1);
/** @phpstan-pure */
-function formatAndPrint(string $name): int
+function format(string $name): string
{
- return print 'Hello, ' . $name;
+ return 'Hello, ' . $name;
}
Or remove the purity annotation if the function genuinely needs to produce output:
<?php declare(strict_types = 1);
-/** @phpstan-pure */
function formatAndPrint(string $name): int
{
return print 'Hello, ' . $name;
}
How to ignore this error #
You can use the identifier impure.print to ignore this error using a comment:
// @phpstan-ignore impure.print
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.print