framework icon indicating copy to clipboard operation
framework copied to clipboard

Collection each() function broken, merge with map()?

Open DanSmith70 opened this issue 13 years ago • 5 comments

The each() function in Collection.php is currently broken (doesn't return the map), however the fix is to change the function to exactly match the map() function below it. Should the each function just be removed, or aliased to map?

DanSmith70 avatar May 22 '13 16:05 DanSmith70

How is each broken, exactly? The fact that it uses array_map internally is only an implementation detail, the API itself does what it is supposed to do, right?

Maybe we can use array_walk or something but I don't think it matters, really.

JoostK avatar May 22 '13 18:05 JoostK

array_map() does not change the array passed to it, it returns a new array thats had the callback applied to each element. However in Collection its just called -- the result is not assigned to anything.

DanSmith70 avatar May 22 '13 19:05 DanSmith70

There also doesn't appear to be any tests for that method, either. From the docs it seems like it should function the same as filter, which returns the result. But it looks like map does that, not each?

andrewryno avatar May 22 '13 19:05 andrewryno

Why do you want each to return anything? It's not the use of each, then you need map. each is there to perform some operations on all items, that's all there is to it and that is what each is already doing.

JoostK avatar May 22 '13 20:05 JoostK

Each is just a way to manipulate objects in the Collection, and it does work for that purpose. I think you are wanting the map function.

taylorotwell avatar May 22 '13 21:05 taylorotwell