通过 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".

我注意到 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.

的实例上调用方法