DusanKasan / Knapsack

Collection pipeline library for PHP
http://dusankasan.github.io/Knapsack/
MIT License
535 stars 56 forks source link

Q: Strict collections intersection #45

Open jaroslavlibal opened 7 years ago

jaroslavlibal commented 7 years ago

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!

DusanKasan commented 7 years ago

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?

jaroslavlibal commented 7 years ago

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.

DusanKasan commented 7 years ago

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.

issei-m commented 7 years ago

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.