doctrine / common

Doctrine Common
https://www.doctrine-project.org/projects/common.html
MIT License
5.78k stars 294 forks source link

Methods static constructor is missing splat ... operator #1008

Open BonBonSlick opened 7 months ago

BonBonSlick commented 7 months ago

We have typed collections

use Doctrine\Common\Collections\ArrayCollection;
final class UserEmailCollection extends ArrayCollection
{
    public function __construct(UserEmail ...$arr)
    {
        parent::__construct($arr);
    }

But when we do any function on that collection inside model, entity

class User {

 public function disableExistingEmails(): void
    {
        $this->emails->map(static function (UserEmail $relatedEmail) use ($isFallbackEmailToDisable) {...})

we get such error

...Collection\UserEmailCollection::__construct(): Argument #1 must be of type 
App\Domain\UserPack\Email\Model\UserEmail, array given, called in 
/var/www/.../vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php on line 99

fixing this issue by creating and extending new abstract collection which overrides ArrayCollection functions e.g.

namespace App\Domain\Core\Collections;

use Closure;
use Doctrine\Common\Collections\ArrayCollection;

use function array_map;

abstract class AbstractModelCollection extends ArrayCollection
{
    public function map(Closure $func)
    {
        return new static(
            ...
            array_map(
                callback: $func,
                array   : $this->toArray()
            )
        );
    }
}

"doctrine/common": "^3.3.0",

I wonder if there is other way without overriding and extra bridge abstraction between domain collections and doctrine to fix the error. Might be a feature request to add splat operator and typed Collection params maybe with interface e.g. CollectionItemInterface

Whatever answer / response, thanks and have a great day / night hehe! ;)