collections icon indicating copy to clipboard operation
collections copied to clipboard

ReadableCollection filter method with phpstan in the context of a Collection instancied as ArrayCollection

Open Sinjonathan opened this issue 2 years ago • 0 comments

HI,

I report here my problem after being redirected by phpstan, see issue here : #https://github.com/phpstan/phpstan-doctrine/issues/428

Seem like filter method docbloc is missconfigured or something alike between Collection/ReadableCollection.

With doctrine/collections 2.1.2 and phpstan/phpstan-doctrine 1.3.32 i get a phpstan error on a valid code before update (1.7.2 => 2.1.2)

Here a phpstan playground to expose how we process to get the error : playground

We get :

Method Test::getFiltered() should return Collection<int, int> but returns ReadableCollection<int, int>.

I can avoid the error by adding this in Collection interface but I'm not sure that it is logical with your choice of create a ReadableCollection interface.

   /**
     * {@inheritDoc}
     *
     * @return static
     * @psalm-return static<TKey,T>
     */
    public function filter(Closure $p);

If it's not the solution, how can I manage to avoid the error in the context of a collection instancied as ArrayCollection that we would filter ? What is the best approach ?

Thank for your time

Sinjonathan avatar Feb 21 '23 09:02 Sinjonathan