doctrine/mongodb-odm

Specify return type of PersistentCollection::filter and ::map

buffcode opened this issue · 1 comments

Bug Report

Q A
BC Break kinda
Version 2.5.2

Summary

With doctrine/collections@^2 the return type of Collection::filter changed to ReadableCollection because that's what it is extended from. ODMs PersistentCollection does not override the return type, so it returns a ReadableCollection as well when filtering on/mapping it.

Current behavior

$persistentCollection = /** obtain somewhere **/;
var_dump(get_debug_type($persistentCollection)); // Doctrine\ODM\MongoDB\PersistentCollection

$filteredCollection = $persistentCollection->filter(fn() => true);
var_dump(get_debug_type($filteredCollection)); // Doctrine\Common\Collections\ArrayCollection

// will fail in type- and IDE-checks, because return type of $filteredCollection as detected by IDE and eg. PHPStan is ReadableCollection and method `add` does not exist.
$filteredCollection->add('foo');

Expected behavior

\Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionTrait::filter (and ::map) should return Collection or ArrayCollection unless being fixed in doctrine/collections#372.

I am not sure whether custom collection currently have the requirement to extend ArrayCollection, so Collection might be the safer choice as it already contains the add method.

Closing as doctrine/collections#372 was resolved :)