简体   繁体   中英

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

I have the following code in an application powered by Silex:

$uknownObj->unkownRef

$uknownObj being a dependency injection container.

I know that unkownRef is of instance MyCoolObj .

Now how can I tell IntelliJ / PhpStorm to actually help me with autocomplete of this said object?

Simply assign unkownRef to a variable and provide annotation like this.

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

You can either use phpdoc comments as suggested by xmoex, or assert type with instanceof :

在此输入图像描述

You can use phpdoc in container class, this is what i use with Slim framework :

/**
* @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. Then in my routes, in the beginning I just put a phpblock like this

/** @var Dummy $this */

My source codes are articles are in https://blog.shaharia.com/slim-php-framework-phpstorm-ide-autocompletion-solution/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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