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

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

Closed
thomasvargiu opened this issue Nov 22, 2020 · 3 comments · Fixed by #42
Closed

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

thomasvargiu opened this issue Nov 22, 2020 · 3 comments · Fixed by #42
Assignees
Labels
Bug Something isn't working
Milestone

Comments

@thomasvargiu
Copy link

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.

if (! is_array($iterator) && ! $iterator instanceof Traversable) {
throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable object');
}
if (! $recursive) {
if (is_array($iterator)) {
return $iterator;
}
return iterator_to_array($iterator);
}
if (is_object($iterator) && method_exists($iterator, 'toArray')) {
return $iterator->toArray();
}

@thomasvargiu thomasvargiu added the Bug Something isn't working label Nov 22, 2020
@Ocramius
Copy link
Member

Looks like a clear bug to me 👍

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
Copy link
Contributor

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

@Ocramius Ocramius added this to the 3.6.3 milestone Dec 24, 2021
Ocramius added a commit that referenced this issue Dec 24, 2021
…ay-different-behaviour-when-recursive-is-false

Fix #18 call `->toArray()` on `Traversable` instances given to `ArrayUtils::iteratorToArray()` only for instances which are **not** `Iterator` implementations
@Ocramius
Copy link
Member

Handled in #42

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