psalm-plugin-doctrine
psalm-plugin-doctrine copied to clipboard
Mark `Selectable::matching` as a mutation free
It returns a new collection by the contract: https://github.com/doctrine/collections/blob/1.6.x/lib/Doctrine/Common/Collections/Selectable.php#L24
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?
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.
Not mutation free: https://github.com/doctrine/orm/blob/2.14.x/lib/Doctrine/ORM/PersistentCollection.php#L646