Skip to content
This repository has been archived by the owner on Apr 12, 2018. It is now read-only.

$this in Return-Annotation #376

Open
SenseException opened this issue Mar 14, 2016 · 5 comments
Open

$this in Return-Annotation #376

SenseException opened this issue Mar 14, 2016 · 5 comments

Comments

@SenseException
Copy link

During My work on a PR for a PHP library I found a strange behaviour when it comes to Abstract classes using @return $this in a method's annotation.

abstract class MyAbstract
{
     /**
      * @return $this
      */
     public function setHandler($handler)
     {

is extended by 2 other classes: My\Namespace\Class and My\Namespace\OtherClass. In a builder for commands, I have the following code:

class Command
{
     public function buildStuff()
     {
         $this->beginCommand('stuff') // creates instance of My\Namespace\Class hardcoded in beginCommand()
             ->setHandler(function() {})
             ->methodName()
             ->moreMethods()

I get this message from Scrutinizer:

The method methodName does only exist in My\Namespace\Class, but not in My\Namespace\OtherClass. 

maybe because it is expected that My\Namespace\OtherClass should also contain methodName().

Shouldn't $this in the annotation only refer to the instance where setHandler() was invoked?

@aik099
Copy link

aik099 commented Mar 15, 2016

The @return $this is incorrect way of writing this and isn't supported by PhpDoc either. In ZendFramework this way was very popular however. The correct way would be @return static.

@SenseException
Copy link
Author

Hi @aik099,

$this is going to be part of PSR-5 (https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#appendix-a-types at the bottom).

@SenseException
Copy link
Author

@scrutinizer-ci Would you consider implementing @return $this?

@webmozart
Copy link

I would also like to see this!

@SenseException
Copy link
Author

It seems that there was no answer yet. If @scrutinizer-ci is kind of occupied with other work, is there a way where I can implement this feature and create a PR? I appreciate feedback if I may be of help to Scrutinizer.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants