45

I'm using the PHPStorm IDE, and run into trouble when I run the code inspection.

I have a method which returns a collection of objects. The Collection itself is an object, which has its own methods, and implements the Traversable interface:

class Repository
{
    public function findByCustomer(Customer $user)
    {
        // ...
        return new Collection($orders);
    }
}

If I document findByUser() to return a Collection, the code inspection understands the methods on this object, but doesn't understand what objects the collection contains:

/**
 * @return Collection
 */
public function findByCustomer() { ... }

Method getTotal() not found in class Collection

If I document findByUser() to return a collection of Order objects, the code inspection now understands what's inside the collection, but not the methods on the Collection itself:

/**
 * @return Order[]
 */
public function findByCustomer() { ... }

Method slice() not found in class Order[]

Is there a way to specify both at the same time, something like Java's syntax?

/**
 * @return Collection<Order>
 */
public function findByCustomer() { ... }
5
  • 3
    You can combine them (both types) together. May not be ideal in some situations, but works and you may consider it better than manually specifying type via @var PHPDoc comment. So ... /** @return Collection|Order[] */ Commented May 23, 2012 at 9:12
  • @LazyOne: even if not the perfect one, that's a cleaner option to me. Can you add this as an answer? Commented May 23, 2012 at 9:26
  • @LazyOne - phpstorm complains "Return type does not match the declared " Commented Feb 20, 2024 at 12:05
  • @Darius.V Please provide a full code example -- it may show what is missing / where the culprit is. P.S. If you have declared the actual PHP return type for the function, then yes, it may be doing that. Need example that can be easily copy pasted or imported (a simple test project will do). Commented Feb 20, 2024 at 12:12
  • @LazyOne I am not able to reproduce it even with Collection declared as return type. But I think it was this, just in different place which I do not remember now. Commented Feb 21, 2024 at 12:47

3 Answers 3

71

You can combine them (both types) together. May not be ideal in some situations, but works and you may consider it better than manually specifying type via @var PHPDoc comment.

/** @return Collection|Order[] */
Sign up to request clarification or add additional context in comments.

3 Comments

In that case it can even read : return Collection than can be seen as an array of Order.
@Benjamin Please provide some simple code example to copy-paste (I would like to see it myself deeper)
@LazyOne I'm surprised I can't repeat it now, I had all these inspection issues when I first opened PHPStorm 8. It might not be what I thought, and in fact be related to WI-22469 you just commented on!
41

Phpstorm starting from 2021.2 allows the following syntax:

    /**
     * @return Collection<Order>
     */

This works for Doctrine Collections - and all Laravel Collections starting with Laravel 9 (the required annotation is not present in L8).

Screenshot from PhpStorm What's New 2021.2:
Screenshot from PhpStorm What's New 2021.2

5 Comments

I was waiting for such a feature like since ever...
This works for Doctrine, but not fo Laravel collections
Actually the correct syntax should be Collection<int, Order> for Doctrine.
PHPStorm still doesn't have great support for accessing \ArrayAccess<TKey, TValue> using regular $collection[0] syntax as of 2022.3.2, see related youtrack.jetbrains.com/issue/WI-61439/…
Just want to plug the comment from @BenMorel. I tried the accepted answer, which didn't work. The minor change suggested by Ben was what worked for me. Thanks, Ben!
5

As MV1908 said, you can use generic collecions in PHPStorm since 2021.2. However, note that this is only possible for a custom collection class (that you created yourself) and Doctrine collection, see https://blog.jetbrains.com/phpstorm/2021/07/phpstorm-2021-2-release/

With the release of PHPStorm 2021.3 its now possible to use generics also for Laravel Collections (however only for Laravel 9). See https://blog.jetbrains.com/phpstorm/2021/12/phpstorm-2021-3-release/#support_for_future_laravel_collections

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.