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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败