Skip to content

Conversation

@raziel057
Copy link

@raziel057 raziel057 commented May 24, 2022

Currently when creating a RecursiveItemIterator passing a new ArrayIterator, the Iterator is indexed by int.

$treeIterator = new \RecursiveIteratorIterator(
            new RecursiveItemIterator(
                new \ArrayIterator(array($menu))
            ), \RecursiveIteratorIterator::SELF_FIRST
        );

So we got the following error:

Parameter #1 $iterator of class Knp\Menu\Iterator\RecursiveItemIterator constructor expects
Traversable<string, Knp\Menu\ItemInterface>, ArrayIterator<int, Knp\Menu\ItemInterface> given.

I can notice that the typehint in CurrentItemFilterIterator is ok:
https://github.com/KnpLabs/KnpMenu/blob/master/src/Knp/Menu/Iterator/CurrentItemFilterIterator.php

Currently when creating a RecursiveItemIterator passing a new ArrayIterator, the Iterator is indexed by int.

```php
$treeIterator = new \RecursiveIteratorIterator(
            new RecursiveItemIterator(
                new \ArrayIterator(array($menu))
            ), \RecursiveIteratorIterator::SELF_FIRST
        );
```

So we got the following error:

Parameter KnpLabs#1 $iterator of class Knp\Menu\Iterator\RecursiveItemIterator constructor expects
Traversable<string, Knp\Menu\ItemInterface>, ArrayIterator<int, Knp\Menu\ItemInterface> given.

I can notice that the typehint in CurrentItemFilterIterator is ok:
https://github.com/KnpLabs/KnpMenu/blob/master/src/Knp/Menu/Iterator/CurrentItemFilterIterator.php
@stof
Copy link
Collaborator

stof commented May 24, 2022

This should then update the @extends and @implements, as the key type will be the one of the inner iterator. And the best way to solve it is by making it generic using TKey, reflecting that the original keys are preserved.

passing a new ArrayIterator, the Iterator is indexed by int.

this actually depends whether your array is indexed by int or by string

@stof
Copy link
Collaborator

stof commented Jun 13, 2025

See #407 fixing this in the proper way.

@stof stof closed this Jun 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants