Search code examples
phpdoctrinedoctrine-ormphpdocdocblocks

Docblocks for Doctrine collections


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.


Solution

  • UPDATE 2022

    Doctrine now documents the generic types of Collection with the @template syntax, which is nowadays supported by PhpStorm & static analysis tools (Psalm & PHPStan) at least:

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

    The old PhpStorm hacky way of documenting this, Collection|User[] is no longer required.