Error Identifier: possiblyImpure.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);
class Logger
{
/** @phpstan-pure */
public function format(string $message): string
{
print $message;
return '[LOG] ' . $message;
}
}
Why is it reported? #
The function or method is marked as pure (via @phpstan-pure), but it uses print or echo inside its body. Outputting text is a side effect because it sends data to the output buffer, which modifies external state. This makes the function possibly impure.
A pure function must have no side effects and must return a result based only on its arguments.
How to fix it #
Remove the print/echo statement from the pure function:
<?php declare(strict_types = 1);
class Logger
{
/** @phpstan-pure */
public function format(string $message): string
{
- print $message;
-
return '[LOG] ' . $message;
}
}
Alternatively, if the function genuinely needs to produce output, remove the @phpstan-pure annotation:
<?php declare(strict_types = 1);
class Logger
{
- /** @phpstan-pure */
public function format(string $message): string
{
print $message;
return '[LOG] ' . $message;
}
}
How to ignore this error #
You can use the identifier possiblyImpure.print to ignore this error using a comment:
// @phpstan-ignore possiblyImpure.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: possiblyImpure.print