php-ds / ext-ds

An extension providing efficient data structures for PHP 7

Home Page:https://medium.com/p/9dda7af674cd

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug with IteratorIterator

enumag opened this issue · comments

I have a case where I need to create an Iterator from a \Ds\Vector. Since Vector is Traversable but doesn't implement Iterator or IteratorAggregate I thought I'd just use IteratorIterator. This is however broken.

Testing code:

$vector = new \Ds\Vector(['x']);

$iterator = new IteratorIterator($vector);
var_export($iterator->valid());

$iterator = new ArrayIterator($vector->toArray());
var_export($iterator->valid());

Expected result: truetrue

Actual result: falsetrue

Calling rewind on the IteratorIterator fixes the issue though so maybe it's ok. Not sure. It was just a bit confusing for me.

It is the general PHP iterator protocol that you must rewind before doing any other iterator operations. Anything else is not guaranteed, as you see here.