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

Collection not typed when using EntityRepository::matching #63

Closed
ygottschalk opened this issue Jul 20, 2020 · 2 comments · Fixed by #64
Closed

Collection not typed when using EntityRepository::matching #63

ygottschalk opened this issue Jul 20, 2020 · 2 comments · Fixed by #64

Comments

@ygottschalk
Copy link
Contributor

ygottschalk commented Jul 20, 2020

TL;DR: EntityRepository<T>::matching has return type Collection, should be Collection<int, T>

How to reproduce

Code: https://psalm.dev/r/678bf40ce6
Error: The type 'Doctrine\Common\Collections\Collection' is more general than the declared return type 'Doctrine\Common\Collections\Collection<int, Entity>' for MyRepository::foo
Versions of my packages:

doctrine/collections                  1.6.6
doctrine/orm                          v2.7.3  
vimeo/psalm                           3.12.2
weirdan/doctrine-psalm-plugin         0.11.3

Is it reproducible or is it just me?

The case for adding matching to the stub

I started digging a bit and found that:

BUT:

Therefore I'd like to add matching to the EntityRepository-stub. I'm happy to provide a PR, just let me know.

Thank you for taking the time and have a nice day!

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/678bf40ce6
<?php

use Doctrine\ORM\EntityRepository;

class Entity {}

/**
 * @extends EntityRepository<Entity>
 */
class MyRepository extends EntityRepository {
    /**
     * @return Collection<int, Entity>
     */
    function foo(): Collection {
        return $this->matching(Criteria::create());
    }
}
Psalm output (using commit 56047dd):

ERROR: UndefinedClass - 10:28 - Class or interface Doctrine\ORM\EntityRepository does not exist

@weirdan
Copy link
Member

weirdan commented Jul 20, 2020

It's not just you. It can be reproduced with the following scenario (after adding necessary uses to the preamble):

  @EntityRepository::matching
  Scenario: matching is typed
    Given I have the following code
      """
      /** @return Collection<int, I> */
      function f(): Collection {
        return repo(I::class)->matching(Criteria::create());
      }
      """
    When I run Psalm
    Then I see no errors

and has a 'perfectly typed' stub (Is this necessary, since the folks over at doctrine already typed it?)

The plugin supports fairly wide range of doctrine versions, including those that did not have Psalm types.

I'm happy to provide a PR, just let me know.

PRs are more than welcome!

and it seems to not change in the future see v2.8.x, see master

You can also submit a PR against doctrine repo. It can even be backported to older branches (but of course it will only be available with new releases).

ygottschalk added a commit to ygottschalk/doctrine-psalm-plugin that referenced this issue Jul 21, 2020
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

Successfully merging a pull request may close this issue.

2 participants