I have a collection (internally a FilterIterator), that starts with 3 elements and has a rejection that reduces that to 2. But running a
foreach on it processes only a single element. I’m trying to debug this, but at a loss as to where I can put a useful breakpoint.
I’m using CakePHP 3.8.6, and PHPStorm. If I put a breakpoint on the
foreach line and step into it, it goes into the
reject function of the
CollectionTrait, where it’s going to call my callback function, and the call stack indicates that this is called from the standard CallbackFilterIterator. Other breakpoints can find me inside the
The collection does know that it’s got 2 items in it, and if I add
toArray() in the
foreach, then it iterates over all the elements. (I am using this to work around this particular issue in the short term, but I don’t like not knowing what’s actually wrong, because it throws every other iteration on collections everywhere in my application into question.) Adding
->rewind() before the
foreach has no effect. Uses of the same collection elsewhere in the code seem just fine. (Note: Not strictly the same object, but a separate collection generated by the exact same code on the exact same input, so should be the same for all intents and purposes.)
This really should be rock-solid, I’d have thought. I can’t think of what might be breaking this. But even more frustrating, I can’t seem to debug what’s going wrong.