Error Identifier: pureFunction.void
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 doNothing(): void // ERROR: Function doNothing() is marked as pure but returns void.
{
}
Why is it reported? #
A function marked as @phpstan-pure must not have side effects and must return a meaningful value. A pure function that returns void serves no purpose – since it has no side effects and produces no return value, calling it has no observable effect. This is almost certainly a mistake: either the function should not be marked as pure, or it should return a value.
The exception is constructors, which are allowed to be pure and return void because their purpose is to initialize an object.
How to fix it #
If the function performs side effects (like writing to a file, modifying external state, or printing output), remove the @phpstan-pure annotation:
<?php declare(strict_types = 1);
-/**
- * @phpstan-pure
- */
function doSomething(): void
{
file_put_contents('/tmp/log.txt', 'done');
}
If the function truly is pure, it should return a value:
<?php declare(strict_types = 1);
/**
* @phpstan-pure
*/
-function compute(): void
+function compute(): int
{
+ return 42;
}
How to ignore this error #
You can use the identifier pureFunction.void to ignore this error using a comment:
// @phpstan-ignore pureFunction.void
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: pureFunction.void