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.
Please sign in to leave a 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