PHPStorm: correct PHPDoc for a Collection of Objects?

跟風遠走 提交于 2019-12-18 12:47:27

问题


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() { ... }

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() { ... }

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

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

回答1:


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[] */


来源:https://stackoverflow.com/questions/10706835/phpstorm-correct-phpdoc-for-a-collection-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!