Skip to content

Return InternalIterator from collection getIterator() methods - #234

Merged
rtheunissen merged 1 commit into
php-ds:masterfrom
HypeMC:make-getiterator-return-internaliterator
Sep 17, 2026
Merged

rtheunissen merged 1 commit into
php-ds:masterfrom
HypeMC:make-getiterator-return-internaliterator

Conversation

@HypeMC

@HypeMC HypeMC commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

See twigphp/Twig#3857.

When using Twig with ext-ds, filters such as last can result in an infinite loop.

The issue is caused by the following loop in CoreExtension:

while ($item instanceof \IteratorAggregate) {
    $item = $item->getIterator();
}

ext-ds collections implement IteratorAggregate, but their getIterator() currently returns another IteratorAggregate, causing code such as the above to keep unwrapping indefinitely.

PHP provides InternalIterator for exposing the internal iterator of internal classes. InternalIterator implements Iterator, not IteratorAggregate.

This PR changes ext-ds to return an InternalIterator from getIterator(), matching the intended behavior for internal iterators and avoiding the infinite loop.

@rtheunissen
rtheunissen merged commit 8e80e4c into php-ds:master Sep 17, 2026
54 checks passed
@HypeMC
HypeMC deleted the make-getiterator-return-internaliterator branch September 17, 2026 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants