0

I receive an error from PHPStan:

Property App\Entity\Product::$productArticles type mapping mismatch: property can contain Doctrine\Common\Collections\Collection but database  
         expects Doctrine\Common\Collections\Collection&iterable<App\Entity\ProductArticle>

My variable declaration:

#[ORM\OneToMany(mappedBy: 'product', targetEntity: ProductArticle::class)]
    /** @var  Collection<int, ProductArticle> */
    private Collection $productArticles;

How can I use both: annotation to declare variable for ORM and PHPDoc comment to declare Collection type?

3
  • Have you tried moving ORM annotation into PHPDock after @var Collection<int, ProductArticle>? Commented Apr 27, 2022 at 9:28
  • You mean something like this? pastebin.com/raw/7RxJC64J It... works. But looks terrible to me. Commented Apr 27, 2022 at 9:36
  • Use old syntax @ORM\OneToMany. And you want working code or to look pretty? Commented Apr 27, 2022 at 10:05

1 Answer 1

6

PHPDoc needs to be above the attribute.

class Foo
{
    /** @var  Collection<int, ProductArticle> */
    #[ORM\OneToMany(mappedBy: 'product', targetEntity: ProductArticle::class)]
    private Collection $productArticles;
}

This is a bug with the parser library (nikic/php-parser) PHPStan uses. So it has to be this way for now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.