Hec*_*nez 8 php code-hinting phpstorm
在PHPStorm中,对象数组的代码提示简单而且令人敬畏;
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;
}
}
Run Code Online (Sandbox Code Playgroud)
所以如果我......
$fooList = new FooList();
foreach($fooList as $foo)
{
// Nice hinting.
$foo->FooMethod...
}
Run Code Online (Sandbox Code Playgroud)
PHPStorm理解$ fooList是一个Foos数组,因此知道$ foo的类型是Foo.
问题是我想要一个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 :(
}
}
Run Code Online (Sandbox Code Playgroud)
我知道你可以在foreach中手动编写提示,比如......
foreach ($listOfLists as $fooList)
{
foreach($fooList as $foo)
{
/** $var $foo Foo */
// Code hinting, yay!!
}
}
Run Code Online (Sandbox Code Playgroud)
要么 ...
foreach ($listOfLists as $fooList)
{
/** $var $fooList Foo[] */
foreach($fooList as $foo)
{
// Code hinting, yay!!
}
}
Run Code Online (Sandbox Code Playgroud)
但是我觉得这很难看,因为$ listOfLists是Foo数组的构建,它应该知道我在说什么,而不是在每次实现listOfLists时都提醒它.
有没有办法实现这个?
根据@LazyOne在评论中链接的错误报告,从PhpStorm EAP 138.256开始(因此在PHPStorm 8中)现在支持统一的多级数组doc解析.
这意味着你现在可以这样做:
/**
* @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();
}
}
Run Code Online (Sandbox Code Playgroud)
得到预期的:

| 归档时间: |
|
| 查看次数: |
4712 次 |
| 最近记录: |