您可以将任何内容传递给Zend_Registry::set('myWidget', $someWidget)
,以便稍后可以使用。
然而,当您在其他地方检索它时,PhpStorm IDE没有关于"myWidget"类型的线索。
<?php
class AwesomeWidget {
public function doBazFlurgle() {
// doesn't matter what exactly happens here
return mt_rand();
}
}
?>
<?php
class FooController {
public function init() {
$someWidget = new AwesomeWidget();
Zend_Registry::set('awesome', $someWidget);
}
public function barAction() {
$this->init();
$awesome = Zend_Registry::get('awesome');
$awesomeNumber = $awesome->doBazFlurgle();
}
}
->doBazFlurgle()
调用上的Navigate to declaration
会给我一个"找不到要转到的声明"。
- 我可以添加
/** @var AwesomeWidget $awesome */
注释,但这需要在相当大的代码库中的许多地方进行编辑 - 我还可以向Zend_Registry添加一个返回类型注释,但这看起来不太可维护(有许多不同类的实例以这种方式存储)
- 我可以搜索字符串
doBazFlurgle
到Find In Path...
,但这并不完全方便(多次击键,而不是单击Ctrl键)
我注意到,在这种确切的情况下,NetBeans能够跳转到方法定义;在PHPStorm中有没有一种简单的方法可以做到这一点,而不需要经过"搜索整个代码库中的doBazFlurgle
"?我已经搜索了可用的IDE操作、插件和论坛;这一切都是徒劳的。
有一种方法:正如@LazyOne所指出的,列出"从哪里返回的内容"有助于IDE理解这些代码;这在Jetbrains的网站上有一定的记录:
<?php
/** @link https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata */
// note that this is not valid PHP code, just a stub for the IDE
namespace PHPSTORM_META {
$STATIC_METHOD_TYPES = [
Zend_Registry::get('') => [
'awesome' instanceof AwesomeWidget, // this is the class as seen in the question
'fooSetting' instanceof Zend_Config, // this came in from application settings
'quuxData' instanceof ArrayAccess, // an arraylike object
]
];
}
将此文件(按惯例命名为.phpstorm.meta.php
)包含在项目中解决了此问题。该文件不是有效的PHP-PhpStorm仅将其用于类型提示。这样,Zend_Registry::get('awesome')->doBazFlurgle()
就被正确地解析为在AwesomeWidget
的实例上调用方法。
有一个解决方法:
- 将光标定位到
doBazFlurgle
方法调用中(点击或光标移动) - 选择单词(
Ctrl+W
) - 导航到符号(
Ctrl+Alt+Shift+N
) - 方法将在下拉列表中提供
- 选择方法(
Enter
)
尽管这不像一般的字符串搜索那样笨拙,但它仍然不像通常的navigate to declaration
那样方便。