小编Ars*_*nik的帖子

PhpStorm 完成,类型提示泛型

我很想找到一种方法(如果这甚至可能?)让 PhpStorm 自动完成我的Factory类生成的代码,而不必每次我从中提取类时都编写 PHPDoc。

这是我的代码:

<?php
class Factory
{
    public function getManager(string $class)
    {
        // if in cache ... returns

        // not in cache > init
        $manager = new $class();
        $this->doStuff($manager);
        return $manager;
    }

    public function doStuff($manager) {}
}

$factory = new Factory();
/** @var DateTime $dtClass */
$dtClass = $factory->getManager(DateTime::class);
$dtClass->getTimestamp();
Run Code Online (Sandbox Code Playgroud)

因此,为了自动完成并消除 PhpStorm 警告,我必须在每次getManager()调用相应类后添加该行。

/** @var DateTime $dtClass */
Run Code Online (Sandbox Code Playgroud)

我想知道 PHPDoc、PhpStorm 帮助文件或任何东西是否可以帮助做到这一点?

    /**
     * @template T
     * @param string $class <T>
     * @return <T>
     */ …
Run Code Online (Sandbox Code Playgroud)

generics phpdoc type-hinting phpstorm

5
推荐指数
1
解决办法
856
查看次数

标签 统计

generics ×1

phpdoc ×1

phpstorm ×1

type-hinting ×1