laminas / laminas-stdlib

SPL extensions, array utilities, error handlers, and more
https://docs.laminas.dev/laminas-stdlib/
BSD 3-Clause "New" or "Revised" License
190 stars 40 forks source link

ArrayUtils::iteratorToArray() different behaviour when recursive is false #18

Closed thomasvargiu closed 2 years ago

thomasvargiu commented 3 years ago

ArrayUtils::iteratorToArray() has a different behaviour when $iterator is a Traversable object with a toArray() method.

When $recursive === false the method returns the result of its iterator, but when $recursive === true it returns the result of its toArray() method.

For a next major release I would suggest to make the method simplier without to handle objects with toArray() methods. Another solution could be to return the toArray() even when $recursive === false.

https://github.com/laminas/laminas-stdlib/blob/f0b299de11700203c4a9644c97aa869613743f4b/src/ArrayUtils.php#L233-L247

Ocramius commented 3 years ago

Looks like a clear bug to me :+1:

For a next major release I would suggest to make the method simplier without to handle objects with toArray() methods.

Totally agree: restricting inputs will help a lot here. We can most certainly design a separate utility that works with iterators, if needed.

garygitton commented 2 years ago

@thomasvargiu Do you have an example of an object that I could try ?

Ocramius commented 2 years ago

Handled in #42