Swader / diffbot-php-client

[Deprecated - Maintenance mode - use APIs directly please!] The official Diffbot client library
MIT License
53 stars 20 forks source link

EntityIterator bugged on Unset #15

Closed Swader closed 9 years ago

Swader commented 9 years ago

When unsetting some elements in the EntityIterator, the next iteration over it will be bugged in that it will only go through some of the elements. Example code for reproduction (dump is from Symfony VarDumper, use var_dump if you don't have it installed):

$a = range(1, 30);
$r = new Response(200);
$ei = new EntityIterator($a, $r);

dump('First run');
foreach ($ei as $i) {
    dump($i);
}

dump('Second run');
foreach ($ei as $i) {
    dump($i);
}

dump($ei);

foreach ($ei as $index => $element) {
    if ($element % 3 === 0) {
        dump("Unsetting " . $element . ' at index ' . $index);
        unset($ei[$index]);
    }
}

dump($ei);

dump('Third run');
foreach ($ei as $i) {
    dump($i);
}

The first and second loop display all 30 elements. The third loop unsets multiples of 3. The fourth loop then only loops as far as number 23, but no further.