简体   繁体   English

PhpStorm自动完成对由Pimple管理的未知对象属性的支持?

[英]PhpStorm auto-complete support for unknown object properties managed by Pimple?

I have the following code in an application powered by Silex: 我在Silex提供的应用程序中有以下代码:

$uknownObj->unkownRef

$uknownObj being a dependency injection container. $uknownObj是依赖注入容器。

I know that unkownRef is of instance MyCoolObj . 我知道unkownRef是实例MyCoolObj

Now how can I tell IntelliJ / PhpStorm to actually help me with autocomplete of this said object? 现在,我如何告诉IntelliJ / PhpStorm真正帮助我自动完成这个对象?

Simply assign unkownRef to a variable and provide annotation like this. 只需将unkownRef指定给变量并提供这样的注释即可。

/** @var MyCoolObj $obj */
$obj = $uknownObj->unkownRef;

You can either use phpdoc comments as suggested by xmoex, or assert type with instanceof : 您可以使用PHPDoc的意见通过xmoex的建议,或断言类型instanceof

在此输入图像描述

You can use phpdoc in container class, this is what i use with Slim framework : 你可以在容器类中使用phpdoc,这是我在Slim框架中使用的:

/**
* @property-read \Monolog\Logger logger
* @property-read \Slim\Views\Twig view
* @property-read \PDO db
*/
class Container extends PimpleContainer implements ContainerInterface
{
}

I just solved this by creating just _ide_autocomplete.php file in the project root where I just created a dummy class with all the slim container key as property. 我刚刚通过在项目根目录中创建_ide_autocomplete.php文件解决了这个问题,我刚刚创建了一个虚拟类,其中包含所有slim容器键作为属性。 Then in my routes, in the beginning I just put a phpblock like this 然后在我的路线中,一开始我只是放了一个像这样的phpblock

/** @var Dummy $this */

My source codes are articles are in https://blog.shaharia.com/slim-php-framework-phpstorm-ide-autocompletion-solution/ 我的源代码文章在https://blog.shaharia.com/slim-php-framework-phpstorm-ide-autocompletion-solution/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM