dongti7838 2015-12-29 14:42
浏览 33
已采纳

PHP如何编写用于提示数据结构的类型的phpdoc

I am creating new stack object with

$this->postfix = new \splStack;

This stack will only handle Token Objects

How can I tell PHPSTORM that the pop command will be returning a Token Object?

Will I have to create another stack object that simply extends splStack and then write phpdocs for it or should I just deal with the fact that It will be difficult and move on?

Thanks, Jason

  • 写回答

1条回答 默认 最新

  • drbouzlxb92333332 2015-12-29 14:56
    关注

    TL;TR: deal with it. You could add an extra layer of inheritance, but if all that is good for is so you can have the docblock you want in place, that's really rather pointless. It'd be better for you to use a proper variable name ($tokenStack), or use inline comments like: /** @var Token $token */ whenever you write $token = $tokenStack->pop(); or something. Alternatively, seeing as your stack is clearly a property, write a method to pop from the stack, and call that instead ($this->popToken();).

    That being said: and additional layer of abstraction might be useful if you want a stack for objects that implement a particular interface:

    class TraversableStack extends SplStack
    {
        public function add($index, $newVal)
        {
            if (!$newVal instanceof Traversable) {
                throw new InvalidArgumentException('TraversableStack only accepts Traversable values');
            }
            return parent::add($index, $newVal);
        }
        /**
         * @return Traversable
         */
        public function pop()
        {
            return parent::pop();
        }
    }
    

    And do the same for all relevant methods. Because of the Liskov substitution principle, it is not allowed to add type hints here (child methods must be compatible with the parent), but there's nothing to prevent you from adding the if's and throw's when you need them.

    The end result of this approach, though, is that your code will be (slightly) slower, because each method call adds overhead. So IMHO, I'd just go with it, and use inline comments to shut PhpStorm up about methods/properties not existing. After all, code like this isn't that hard to read:

    $this->tokenStack = new \SplStack;
    $this->tokenStack->push($token);
    //more code
    /** @var Token $lastToken */
    $lastToken = $this->tokenStack->pop();
    

    And like I said in my comment: considering your stack is assigned to a property anyway, you could easily create a method that calls SplStack::pop internally, and add your doc-block there:

    /**
     * @return Token
     * @throws \RuntimeException
     */
    protected function popToken()
    {
        return $this->tokenStack->pop();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog