PHPStorm代码提示对象数组的数组 [英] PHPStorm Code Hinting for array of object arrays
问题描述
在PHPStorm中,对象数组的代码提示既简单又很棒;
In PHPStorm, the Code Hinting for an object array is simple and awesome;
class FooList {
public function __construct(){
$this->_fooList[] = new Foo(1);
$this->_fooList[] = new Foo(2);
$this->_fooList[] = new Foo(3);
$this->_fooList[] = new Foo(4);
}
/**
* @return Foo[]
*/
getFoos() {
return $this->_fooList;
}
}
如果我愿意...
$fooList = new FooList();
foreach($fooList as $foo)
{
// Nice hinting.
$foo->FooMethod...
}
PHPStorm知道$ fooList是一个Foos数组,因此知道$ foo的类型是Foo.
PHPStorm understands that $fooList is an array of Foos, and therefore knows that $foo's type is Foo.
问题是我想要一个FooList数组.
The problem is that I want an array of FooList.
$listOfLists[] = new FooList();
$listOfLists[] = new FooList();
$listOfLists[] = new FooList();
$listOfLists[] = new FooList();
foreach ($listOfLists as $fooList)
{
foreach($fooList as $foo)
{
// No code hinting for $foo :(
}
}
我知道您可以在foreach内部手动编写提示代码,例如...
I know that you can code hint manually inside the foreach, like...
foreach ($listOfLists as $fooList)
{
foreach($fooList as $foo)
{
/** $var $foo Foo */
// Code hinting, yay!!
}
}
或者...
foreach ($listOfLists as $fooList)
{
/** $var $fooList Foo[] */
foreach($fooList as $foo)
{
// Code hinting, yay!!
}
}
但是我认为这很困难,因为$ listOfLists是Foo数组的构建,所以每次实现listOfLists时,它都应该知道我在说什么,而无需提醒它.
But I think that is ugly, as $listOfLists is build of Foo arrays, it should know what I am talking about without reminding it every time I implement a listOfLists.
有没有办法实现这一目标?
Is there a way to implement this?
推荐答案
每个错误报告在评论中由@LazyOne链接 ,自 PhpStorm EAP 138.256 (因此在PHPStorm中8)现在支持统一的多级数组文档解析.
Per the bug report linked in the comments by @LazyOne, as of PhpStorm EAP 138.256 (thus in PHPStorm 8) uniform multi-level array doc parsing is now supported.
这意味着您现在可以执行以下操作:
This means you can now do this:
/**
* @var $listOfLists Foo[][]
*/
$listOfLists[] = (new FooList())->getFoos();
$listOfLists[] = (new FooList())->getFoos();
$listOfLists[] = (new FooList())->getFoos();
$listOfLists[] = (new FooList())->getFoos();
foreach ($listOfLists as $fooList)
{
foreach($fooList as $foo)
{
// Code hinting, yay!!
$foo->fooMethod();
}
}
并获得预期的结果:
这篇关于PHPStorm代码提示对象数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!