PHPDocument中的类型转换


Type casting in PHPDocumentor

PHPStorm中有PHPDocument的类型转换吗?我的PHPStorm IntelliSense很固执,我需要教他如何表现。

这是我的案例:

我有一个管理器,它实例化所有扩展同一类的类。通常,我会在类返回后对其进行强制转换,但PHP不知道如何做到这一点。IntelliSense和PHPDocument支持对我来说已经足够了,但我不知道如何键入cast。

这是代码:

class Plugin 
{
}
class HelloPlugin extends Plugin
{
    public function hello()
    {
    }
}
class PluginManager 
{
    /** @var HelloPlugin */
    public $helloPlugin;
    function __construct()
    {
        $this->helloPlugin = $this->getPlugin('HelloPlugin');
        // Here my PHPStorm IntelliSense doesn't provide 'hello()' function for 'helloPlugin' because return type overwrote original type
        // I get error: method 'hello' not found in class
        $this->helloPlugin->hello();
    }
    /**
     * @param string $pluginClass
     * @return Plugin
     */
    function getPlugin($pluginClass)
    {
        return new $pluginClass;
    }
}

我认为这实际上是不可能的:(,除非你以某种方式重写你的代码(只是为了PhpStorm…不可能)。

您的代码和PHPDoc很好——这是一个IDE问题——它会临时(仅在该方法中)用它检测到的类型提示覆盖手动提供的类型提示(在其他方法中也能正常工作)。请投票:http://youtrack.jetbrains.com/issue/WI-17047