Search code examples
laravelphpstorm

Laravel and PhpStorm auto code hint


I have a class that returns a collection:

public function getCustomers() : Collection;

When I loop the result, PhpStorm IDE won't know what's inside the collection.

In .NET/JAVA you would write:

public <List<Customer>> getCustomers();

then IDE will knows what's inside the collection and know how to complete/hint that.

Is there any trick to introduce that to PhpStorm?


Solution

  • It's easy with the docblocks:

    /**
     * @return Customer[]|Collection
     */
    public function getCustomers();
    

    Essentially what you are doing here is telling phpStorm that the function returns an array of Customer objects and the Collection as well.

    You can type hint multiple returns separating them with a pipe symbol |