A generator can return a value, and getReturn reads it

A generator yields a sequence, and it can also return a single value at the end — which is how a streaming reader reports a summary without breaking the stream to carry it.

function importRows($handle) {
    $bad = 0;

    while (($row = fgetcsv($handle)) !== false) {
        if (! isValid($row)) { $bad++; continue; }
        yield $row;
    }

    return $bad;
}

$gen = importRows($handle);
foreach ($gen as $row) { $this->store($row); }

printf("%d rows rejectedn", $gen->getReturn());

getReturn() throws if the generator has not finished, which makes it self-documenting: the value is only meaningful once the sequence is exhausted. It also means holding the generator in a variable rather than passing the call straight into foreach, which is the small change that makes the pattern possible. Useful for exactly this — counts, checksums and totals that are known only at the end.