Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
BonBonSlick opened this issue Feb 10, 2024 · 0 comments
Open

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

BonBonSlick opened this issue Feb 10, 2024 · 0 comments

Comments

@BonBonSlick
Copy link

BonBonSlick commented Feb 10, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant