Knapsack icon indicating copy to clipboard operation
Knapsack copied to clipboard

Q: Strict collections intersection

Open jaroslavlibal opened this issue 7 years ago • 4 comments

Hello,

is there any way how to compare collections strictly in the intersect() method?

The problem is that the in_array function used at https://github.com/DusanKasan/Knapsack/blob/master/src/collection_functions.php#L1301 causes the "Nesting level too deep - recursive dependency?" error when using a Collection of big objects.

I have been thinking about sending a PR with the third param set to true; however then I realised that it could be a big BC break for some users so I would like to ask you if you have some workaround suggestion instead, pelase.

Thanks!

jaroslavlibal avatar Sep 08 '17 15:09 jaroslavlibal

Hmm, i see. I think the solution could be to rewrite intersect so that we iterate over first 2 collections, create a temporary collection of the intersection, then repeat this over the next collection, the next and so on. This would not create a BC so it should be fine and will also lessen the memory load.

Would this solve your problem?

DusanKasan avatar Sep 08 '17 17:09 DusanKasan

Hello, thanks for the answer. It would probably not help as the "problem" occurs also during the intersection of two collections which contains some PhpUnit mocks of Doctrine entities.

jaroslavlibal avatar Sep 11 '17 07:09 jaroslavlibal

Sorry, i misunderstood, i thought the problem was being with large collections. In the case of large objects we could introduce a method intersectStrict(collections...) or intersectUsing(comparator, collections...).

For now however, your best bet is calling filter/reject to remove the elements that are in both collections and then checking if the result is an empty collection.

DusanKasan avatar Sep 11 '17 07:09 DusanKasan

I'm digressing from topic a bit, but in_array without the 3rd argument seems to be scattered in codes. IMHO, the 3rd argument of in_array should always be true nowadays, and even if we'd pass true to them, that does not make a massive impact in the PHP7 (static-type-hinting) world.

issei-m avatar Sep 11 '17 09:09 issei-m