Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ext-ds Vector/Map not supported by the following filters: slice, first, last (infinite loop) #3857

Open
josephLaurent opened this issue Jul 12, 2023 · 4 comments

Comments

@josephLaurent
Copy link

We use ext-ds object in twig files and the slice/first or last filters get stuck when using it on Vector or Map.
Exemple: {% set result = myVector|last %}
This is due to the following while loop in the twig_slice function (https://github.com/twigphp/Twig/blob/3.x/src/Extension/CoreExtension.php#L651):

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

And because $item->getIterator() return a Vector which implement \IteratorAggregate.

I don't know if it can be fixed ?
Thank you in advance for your help

@josephLaurent
Copy link
Author

Up

@ericmorand
Copy link
Contributor

Is Vector a traversable?

The last filter returns the last "element" of a sequence, a mapping, or a string
[...]
It also works with objects implementing the Traversable

@josephLaurent
Copy link
Author

Yes Vector and Map are both traversable and they also implement the \IteratorAggregate interface which end up in the following infinite loop

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

because the getIterator of Vector/Map always return an \IteratorAggregate.
But I don't understand this while loop because nothing say that this while loop will end.

@stof
Copy link
Member

stof commented Mar 30, 2024

Well, in userland, you cannot have an infinite loop there, because at some point, you need to reach a place actually implementing the iteration protocol. But for extensions, they might be able to override the iteration protocol without actually implementing Iterator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants