Typehints for generic PHP collections

Hello!
I'm looking for tips to get typehints working for variable $user2 in following example:


use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\User;


/** @var ArrayCollection|User[] $users */
$users = new ArrayCollection();
$users->add(new User());

$user1 = $users[0]; //is recognized as User instance
$user2 = $users->get(0); //not recognized as User instance

 

 

According to this article looked at possibilities to add metadata for method Doctrine\Common\Collections\ArrayCollection::get(0), but there doesn't seems to be tools available to achieve desired typehints.

 

 

 

 

1
1 comment

Here's a request for PHPDoc generics support: https://youtrack.jetbrains.com/issue/WI-43843

For now, you can use advanced metadata this way: http://prntscr.com/o9r3e3

namespace PHPSTORM_META {
    override(
        \Doctrine\Common\Collections\ArrayCollection::get(0),
        map(
            [
                '' => '\App\Entity\User',
            ]
        )
    );
}
1

Please sign in to leave a comment.