通过Zend_Registry::get()进行类型提示-如何使到声明的导航对PhpStorm来说是可理解的?

通过Zend_Registry::get()进行类型提示-如何使到声明的导航对PhpStorm来说是可理解的?,php,ide,phpstorm,Php,Ide,Phpstorm,您可以将任何内容传递给Zend_Registry::set('myWidget',$someWidget) 然而,当您在其他地方检索它时,phpstormide没有关于“myWidget”类型的线索 <?php class AwesomeWidget { public function doBazFlurgle() { // doesn't matter what exactly happens here return mt_rand(); } } ?&

您可以将任何内容传递给Zend_Registry::set('myWidget',$someWidget)

然而,当您在其他地方检索它时,phpstormide没有关于“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();
    }

}

有一个解决方法:

  • 将光标定位到方法调用中(单击或光标移动)
  • 选择单词(
    Ctrl+W
  • 导航到符号(
    Ctrl+Alt+Shift+N
  • 方法将在下拉列表中提供
  • 选择方法(
    输入

尽管这并不像一般的字符串搜索那么笨拙,但它仍然不像通常的
导航到声明
那么方便。有一种方法:正如所指出的,列出“从何处返回的内容”有助于IDE理解此类代码;这是:


@LazyOne:太好了,它能用!如果您不知道要搜索什么,则很难找到;)
<?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
        ]
    ];
}