website/errors/generator.void.md
<?php declare(strict_types = 1);
/**
* @return \Generator<int, int, void, void>
*/
function numbers(): \Generator
{
yield 1;
$value = yield 2;
}
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.
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;
}