Error Identifier: generator.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);
/**
* @return \Generator<int, int, void, void>
*/
function numbers(): \Generator
{
yield 1;
$value = yield 2;
}
Why is it reported? #
The generator declares void as its send type (the TSend template parameter of Generator), meaning that no value is sent back into the generator via Generator::send(). Using the result of a yield expression when the send type is void will always produce null, which indicates a logic error – the code appears to expect a value to be sent into the generator, but the type declaration says otherwise.
How to fix it #
If the generator needs to receive values via send(), declare the correct send type:
/**
- * @return \Generator<int, int, void, void>
+ * @return \Generator<int, int, string, void>
*/
function numbers(): \Generator
{
yield 1;
$value = yield 2;
}
If the generator does not need to receive values, remove the unused assignment:
/**
* @return \Generator<int, int, void, void>
*/
function numbers(): \Generator
{
yield 1;
- $value = yield 2;
+ yield 2;
}
How to ignore this error #
You can use the identifier generator.void to ignore this error using a comment:
// @phpstan-ignore generator.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: generator.void