Wei*_* Ma 3 php phpdoc phpstorm
最近,我在使用 PhpStorm 实现 PHP 应用程序时遇到了一个有趣的情况。下面的代码片段说明了这个问题。
interface I{
function foo();
}
trait T{
/**
* @return string
*/
public function getTraitMsg()
{
return "I am a trait";
}
}
class A implements I{
use T;
function foo(){}
}
class C implements I{
use T;
function foo(){}
}
class B {
/**
* @param I $input <===Is there anyway to specify that $input use T?
*/
public function doSomethingCool($input){ //An instance of "A" or "C"
$msg = $input -> getTraitMsg(); //Phpstorm freaks out here
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题在评论里。如何指示$input参数实现I和使用T?
这有点简单,但您可以使用class_uses它返回已使用特征的列表。并T在 PHPDoc 中添加为 @param 类型以进行自动完成
class B {
/**
* @param I|T $input <===Is there anyway to specify that $input use T?
*/
public function doSomethingCool($input){ //An instance of "A" or "C"
$uses = class_uses(get_class($input));
if (!empty($uses['T'])) {
echo $input->getTraitMsg(); //Phpstorm freaks out here
}
}
}
Run Code Online (Sandbox Code Playgroud)