gpt4 book ai didi

php - 通过 Zend_Registry::get() 类型提示 - 如何让 PhpStorm 可以理解导航到声明?

转载 作者:搜寻专家 更新时间:2023-10-31 21:27:13 25 4
gpt4 key购买 nike

你可以传递任何东西给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() 调用上让我收到“无法找到要转到的声明”。

  • I could add一个 /** @var AwesomeWidget $awesome */ 注释,但这需要在相当大的代码库中的许多地方进行编辑
  • I could还有add a return type Zend_Registry 的注解,但这看起来不太易于维护(有许多不同类的实例以这种方式存储)。
  • 我可以通过 Find In Path... 搜索字符串 doBazFlurgle,但这并不十分方便(需要多次击键而不是一次 Ctrl+单击)

我注意到 NetBeans 能够在这种确切情况下跳转到方法定义;有没有一种简单的方法可以在 PHPStorm 中执行相同的操作而无需通过“在整个代码库中搜索 doBazFlurgle”?我搜索了可用的 IDE 操作、插件和论坛;一切都是徒劳的。

最佳答案

有一种方法:正如@LazyOne指出的那样,列出“从哪里返回的内容”有助于 IDE 理解此类代码;这是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 实例上的方法。

关于php - 通过 Zend_Registry::get() 类型提示 - 如何让 PhpStorm 可以理解导航到声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34317265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com