Skip to content

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

@BonBonSlick

Description

@BonBonSlick

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! ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions