|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-09-30 13:48 UTC] goetas at lignano dot it
Description:
------------
iterating over an empty seekable iterator causes an unexpected exception.
with actual implementation of LimitIterator::rewind() there is no way to do an empty loop over an empty ArrayIterator.
i suggest this implementation for LimitIterator::rewind() method
function rewind(){
$this->it->rewind();
$this->pos = 0;
if($this->it->valid()){ // check for empty iterators
$this->seek($this->offset);
}
}
Reproduce code:
---------------
$it = new ArrayIterator(array());
$limIt = new LimitIterator($it, 0, 5);
foreach ($limIt as $item){
echo $item;
}
Expected result:
----------------
an empty loop
Actual result:
--------------
Uncaught exception 'OutOfBoundsException' with message 'Seek position 0 is out of range'
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 20:00:01 2025 UTC |
source code of LimitIterator, "seek" method at line 63: if ($this->it instanceof SeekableIterator) { $this->it->seek($position); <-- there is no check if i can move pointer to $position like on else branch $this->pos = $position; } else { while($this->pos < $position && $this->it->valid()) { <-- valid() ensures that i can move to $position $this->next(); } }i think this can be a bug... or not correcty explained feature, becasue following code works fine: $it = new EmptyIterator(); // EmptyIterator instead of ArrayIterator $limIt = new LimitIterator($it, 0, 5); foreach ($limIt as $item){ echo $item; } both range are empty, but EmptyIterator does not implements SeekableIterator while ArrayIterator it does. exception is thrown only if iterator implements SeekableIterator and is empty.