genericsdoctrinestatic-analysisphpdocphpstan

PHPStan: Property with generic class does not specify its types: TKey, T


I'm running PHPStan on a Symfony project where I have the following relation in a Doctrine entity:

/**
 * @ORM\OneToMany(targetEntity="App\Entity\Course\Slide", mappedBy="chapter", cascade={"persist"}, orphanRemoval=true)
 * @ORM\OrderBy({"listOrder" = "ASC"})
 *
 * @var ArrayCollection<Slide>
 */
private $slides;

Running analysis with rule level 6 I got the following message about the property (and its getter return type):

Property App\Entity\Course\Chapter::$slides with generic class Doctrine\Common\Collections\ArrayCollection does not specify its types: TKey, T
💡 You can turn this off by setting checkGenericClassInNonGenericObjectType: false in your phpstan.neon.

My edit attempts only confused PHPStan, maybe because I'm not fully understanding generics here. But silencing the message just because I don't get it would be stupid.

What am I supposed to add or change in the PHPDoc ?


Solution

  • ArrayCollection has two type variables:

    So ArrayCollection<Slide> isn't sufficient, you need something like ArrayCollection<int, Slide>.