psalm-plugin-doctrine icon indicating copy to clipboard operation
psalm-plugin-doctrine copied to clipboard

Mark `Selectable::matching` as a mutation free

Open fluffycondor opened this issue 2 years ago • 3 comments

It returns a new collection by the contract: https://github.com/doctrine/collections/blob/1.6.x/lib/Doctrine/Common/Collections/Selectable.php#L24

fluffycondor avatar Aug 11 '22 13:08 fluffycondor

Note: I don't know Doctrine enough so I may ask stupid questions :)

Is the creation of a new Collection necessarily mutable-free (if so, should Collection::__construct be flagged as mutation-free)? Does it clone the provided Collection maybe?

orklah avatar Aug 11 '22 16:08 orklah

Is the creation of a new Collection necessarily mutable-free (if so, should Collection::__construct be flagged as mutation-free)? Does it clone the provided Collection maybe?

Collection interface doesn't specify any requirements for realizations' constructor. The only implementation Doctrine has is ArrayCollection, which takes an array. It doesn't clone deeply array values, so if it's an array of objects, objects will be copied by ref. So, Selectable::matching will definitely return a new Collection, but if the values are not scalar, it will be copied by ref. As far as I understand, it's still can be called mutation-free.

fluffycondor avatar Aug 16 '22 11:08 fluffycondor

Not mutation free: https://github.com/doctrine/orm/blob/2.14.x/lib/Doctrine/ORM/PersistentCollection.php#L646

ygottschalk avatar Jan 13 '23 09:01 ygottschalk