How to inherit phpdoc from another method (not the one we are overwriting)

2.6k views Asked by At

I was wandering if there is a method to copy (inherit) exact same phpdoc on different methods in the same and/or different classes.

I know about inline {@inheritdoc}, which only provide inheritance from a parent to its child class (as one should expect :).

But look at the following, there i have to copy the very same description in 3 different places, 2 methods in the same class and one in a separate class (not inherited but initiated). I was wondering if there is a method that might does the trick, without copy pasting.

class Example
{
    /**
     * here i want to have exactly the same phpdoc,
     * like what i created for getMeSomethingFromTheShelf() in the same class
     */
    public function getMeSomething($id, $option = [])
    {
        return $this->getMeSomethingFromTheShelf($id, $option);
    }

    /**
     * Some description
     * and some more detail about how the $option value will be used
     *
     * @param int $id
     * @param array $option and here some we have some sample
     * @return string
     */
    public function getMeSomethingFromTheShelf($id, $option = [])
    {
    return 'here you go :)';
    }
}

class AnotherClass
{
    /**
     * here also i want to have exactly the same phpdoc,
     * like what i created for Example::getMeSomethingFromTheShelf() in the Example class
     */
    public function fetchSomething($id, $option = [])
    {
        return (new Example)->getMeSomething($id, $option = []);
    }
}

Thanks for any suggestion, in advance :)

1

There are 1 answers

4
Mike On BEST ANSWER

{@inheritdoc} is a built-in, without support for specifying the inheritance source. Please make use of the @see tag.

It is unlikely that two methods should be described the same way. If it is a proxy, or simply passes data through, the description should note this behavior, and the @see tag should direct the user to the interfaced method.

I routinely use the @see and @link tags to link to SAPI documentation, or to resolve proxies. That said, I always try to include some sort of description as well, and always document parameters and returns, even if they are identical (helps when looking at the code).

Be careful of getting too reliant on PHPDoc's shortcuts.