Error Identifier: possiblyImpure.echo
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): string
{
$result = 'Hello, ' . $name;
echo $result;
return $result;
}
Why is it reported? #
The function or method is marked as @phpstan-pure, but it contains an echo statement. Pure functions must not have side effects – they should only compute and return a value based on their input parameters. Outputting text with echo is a side effect because it modifies the program’s output stream.
How to fix it #
Remove the echo statement from the pure function and let the caller handle the output:
<?php declare(strict_types = 1);
/** @phpstan-pure */
function formatAndPrint(string $name): string
{
$result = 'Hello, ' . $name;
- echo $result;
return $result;
}
+
+echo formatAndPrint('World');
Alternatively, if the function needs to produce output, remove the @phpstan-pure annotation:
<?php declare(strict_types = 1);
-/** @phpstan-pure */
function formatAndPrint(string $name): string
{
$result = 'Hello, ' . $name;
echo $result;
return $result;
}
How to ignore this error #
You can use the identifier possiblyImpure.echo to ignore this error using a comment:
// @phpstan-ignore possiblyImpure.echo
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.echo