Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 821
AymDev Avatar asked Apr 24 '20 22:04

AymDev


1 Answers

ArrayCollection has two type variables: TKey and T. So ArrayCollection<Slide> isn't sufficient, you need something like ArrayCollection<int, Slide>.

like image 102
Ondřej Mirtes Avatar answered Sep 21 '22 13:09

Ondřej Mirtes