如何指示“包含特征"的参数?在 PHPDoc 中 [英] How to indicate a parameter that "include trait" in PHPDoc

查看:58
本文介绍了如何指示“包含特征"的参数?在 PHPDoc 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在使用 PhpStorm 实现 PHP 应用程序时遇到了一个有趣的情况.下面的代码片段说明了这个问题.

Recently I ran into an interesting situation when implementing a PHP application using PhpStorm. The following code snippet illustrates the problem.

    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
        }
    }

我的问题在评论中.如何指示 $input 参数实现 I 并使用 T?

My question is in the comment. How do I indicate that $input parameter implements I and uses T?

推荐答案

这有点古怪,但您可以使用 class_uses 它返回已使用特征的列表.并在 PHPDoc 中添加 T 作为 @param 类型以实现自动完成

It's a lit bit hackly, but you can use class_uses it returns list of used traits. And add T as a @param type in PHPDoc for autocomplete

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
        }
    }
}

这篇关于如何指示“包含特征"的参数?在 PHPDoc 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆