通过 Zend_Registry::get() 类型提示 - 如何使 PhpStorm 可以理解导航到声明?
Typehinting through Zend_Registry::get() - how to make navigation to declaration understandable to PhpStorm?
您可以将任何内容传递给 Zend_Registry::set('myWidget', $someWidget)
、so that it's available later on。
但是,当您在别处检索它时,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();
}
}
Navigate to declaration
在 ->doBazFlurgle()
电话上给我 "Cannot find declaration to go to".
- I could add 一个
/** @var AwesomeWidget $awesome */
注释,但这需要在相当大的代码库中的许多地方进行编辑
- I could also add a return type 对 Zend_Registry 的注释,但这看起来不太易于维护(有许多不同 类 的实例以这种方式存储)。
- 我可以通过
Find In Path...
搜索字符串 doBazFlurgle
,但这并不完全方便(需要多次击键而不是一次 Ctrl+单击)
我注意到 NetBeans 能够在这种确切情况下跳转到方法定义;有没有一种简单的方法可以在 PHPStorm 中执行相同的操作而无需通过 "search the entire codebase for doBazFlurgle
"?我搜索了可用的 IDE 操作、插件和论坛;一切都是徒劳。
有一个解决方法:
- 将光标定位到
doBazFlurgle
方法调用(单击或光标移动)
- Select字(
Ctrl+W
)
- 导航到符号 (
Ctrl+Alt+Shift+N
)
- 方法将在下拉列表中提供
- Select 方法 (
Enter
)
虽然这不像通用字符串搜索那么笨拙,但仍然不如通常的方法方便 navigate to declaration
。
有一个方法:正如@LazyOne, making a list of "what is returned from where" helps the IDE make sense of such code; this is somewhat documented on Jetbrains' website所指出的:
<?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
.
的实例上调用方法
您可以将任何内容传递给 Zend_Registry::set('myWidget', $someWidget)
、so that it's available later on。
但是,当您在别处检索它时,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();
}
}
Navigate to declaration
在 ->doBazFlurgle()
电话上给我 "Cannot find declaration to go to".
- I could add 一个
/** @var AwesomeWidget $awesome */
注释,但这需要在相当大的代码库中的许多地方进行编辑 - I could also add a return type 对 Zend_Registry 的注释,但这看起来不太易于维护(有许多不同 类 的实例以这种方式存储)。
- 我可以通过
Find In Path...
搜索字符串doBazFlurgle
,但这并不完全方便(需要多次击键而不是一次 Ctrl+单击)
我注意到 NetBeans 能够在这种确切情况下跳转到方法定义;有没有一种简单的方法可以在 PHPStorm 中执行相同的操作而无需通过 "search the entire codebase for doBazFlurgle
"?我搜索了可用的 IDE 操作、插件和论坛;一切都是徒劳。
有一个解决方法:
- 将光标定位到
doBazFlurgle
方法调用(单击或光标移动) - Select字(
Ctrl+W
) - 导航到符号 (
Ctrl+Alt+Shift+N
) - 方法将在下拉列表中提供
- Select 方法 (
Enter
)
虽然这不像通用字符串搜索那么笨拙,但仍然不如通常的方法方便 navigate to declaration
。
有一个方法:正如@LazyOne, making a list of "what is returned from where" helps the IDE make sense of such code; this is somewhat documented on Jetbrains' website所指出的:
<?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
.