doctrine / doctrine/collections

Extending Doctrine ArrayCollection

Open
#271 0 comments 6 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
6k
Forks
187
PR merge metrics
No merged PRs in 30d

Description

I have a Custom collection class that extends Doctrine ArrayCollection, which has template annotations out of the box.

The ArrayCollection class has a filter method which I call from my custom collection to create a new collection instance with some items removed. When I try to set the return type for the function, that calls the original filter method, in my custom collection class to self I get a PHPStan error telling me the return type should be ArrayCollection and not an instance of my custom collection.

When I remove the `@psalm-return static` annotation from the filter method things work as I expect.

Psalm does not seem bothered about it but maybe it was left over after fixing: doctrine/collections#201?

If it is intentional and needed for Psalm, how do I deal with integrating this third party code in my own project running both Psalm and PHPStan? I know there's a bunch of workarounds I can do in the custom Collection class but I have multiple of those in my actual project.

I made a simplified versions of both the ArrayCollection and the custom class:
https://phpstan.org/r/7d36bc06-b81b-45c4-9f63-38dfaa0c5458
https://psalm.dev/r/6de8f68943

So even if Psalm does not complain in the actual project, the same `@psalm-return static` annotation is causing problems in the link above.

I already started a discussion at PHPStan and their conclusion is that `@resturn static` should be sufficient, see: https://github.com/phpstan/phpstan/discussions/4825

Contributor guide

Open the contributing guide

Research direction

Start with Doctrine ArrayCollection's filter method and its @psalm-return static annotation, then compare the linked PHPStan and Psalm reproductions. Confirm whether the annotation is required for Psalm and whether it produces the reported PHPStan return-type error; done means a compatible annotation or documented integration guidance for projects using both analyzers.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.