Docblocks for Doctrine collections

BenMorel picture BenMorel · Sep 3, 2011 · Viewed 8.8k times · Source

Is there a standard way to document the expected class of entities inside a Collection in the docblock comment in a Doctrine project? Something like:

/**
 * @var Collection<User>
 */
protected $users;

Looks like PHPDoc is the de-facto standard for docblock annotations now, but I couldn't find any mention for this use case.

Answer

Matthieu Napoli picture Matthieu Napoli · Feb 6, 2013

Here is a solution that enables you to have autocompletion both on the Collection methods and your objects methods:

/**
 * @param Collection|User[] $users
 */
public function foo($users)
{
    $users-> // autocompletion on Collection methods works

    foreach ($users as $user) {
        $user-> // autocompletion on User methods work
    }
}

It works like a charm in PhpStorm at least.