Fix: Return static instead of self #37
Fix: Return static instead of self #37
Conversation
|
Thank you for pointing me to the draft, had a look, but unsure - maybe I don't understand the document right. |
If you think return type is wrong please open an issue in the https://github.com/php-fig/http-message project first. |
Since There are three options possible:
|
Thanks a lot for clarifying! |
We can update in here, and later I'll update the http-message library and
|
@weierophinney Unfortunatelly it's not clear for me that Interface will change the type since require a change in the PSR-7. |
@weierophinney I must have missed that; I have had a rough couple of months so things might easily have been missed |
Anyway I'm not sure if the debate is about how PSR-5 should understand the return type for interface methods. Because the return type is defined in an interface then |
It's not a change in the PSR. The intent is exactly the same: return a different instance. We were
|
Can we reopen the PR, then? I've opened a PR upstream, see php-fig/http-message#50. |
@@ -66,7 +66,7 @@ public function getProtocolVersion() | |||
* new protocol version. | |||
* | |||
* @param string $version HTTP protocol version | |||
* @return self | |||
* @return static |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just joining the conversation: since the interface defines that a MessageInterface
is returned, so should also the trait providing the concrete impl :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ocramius No, it does not. The interface indicates uses @return self
, and indicates a new instance is returned. Considering that MessageInterface
is not expected to be directly implemented, but rather implemented via one of RequestInterface
or ResponseInterface
, the question is whether self
or static
should be specified in the return annotation to ensure that IDEs, PHPDocumentor, and implementors are following the spec properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering that
MessageInterface
is not expected to be directly implemented
It's an interface. It will be implemented, and nobody says that it will necessarily be a RequestInterface
or a ResponseInterface
:-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ocramius The issue here is the inheritance tree, which includes interfaces, and, more specifically, traits. Per this comment from @mvriel, self
indicates that it's an instance of the class where the docblock is defined, while static
is defined to return an instance of the current class, following inheritance.
So, a change is required here for sure. The question is whether changes are needed on the PSR-7 interfaces. I'd argue they are for MessageInterface
and RequestInterface
, as those two both are inherited by other interfaces in the specification.
Fix: Return static instead of self
I totally disagree with this. If there is an issue with the interface then should be fixed in the interface first. |
Has there been a PR sent against fig-standards or http-message? If not, we should do that right away. We should also get feedback from others on whether this should be an allowed change to PSR-7 now that it has been accepted. I think that the change from |
Where is the limit when you say your own side changes are compatible? This is the first step for define your own standard. |
@Maks3w I am following the advice of the author/editor of psr-5, and will
|
We are currently only applying it to the two traits, to clarify inheritance
|
@simensen I believe the author of this proposal has created patches already
|
For reference, here it is: php-fig/http-message#50. |
This PR
MessageTrait
andResponseTrait
to returnstatic
instead ofself
❓ Seems to make more sense to me, how about you?