Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix instance returned by MessageResponseTrait #1440

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 19 additions & 28 deletions src/Traits/MessageResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ public function getProtocolVersion(): string
return $this->response->getProtocolVersion();
}

/**
* @return MessageInterface
*/
public function withProtocolVersion($version): MessageInterface
{
return $this->response->withProtocolVersion($version);
$this->response->withProtocolVersion($version);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to return the withProtocolVersion($version) otherwise we'll lost the response. We cannot return $this, the PSR-7 MessageInterface is immutable.

Copy link
Contributor Author

@AJenbo AJenbo Mar 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case you need to do a bigger refactoring in order to resolve this properly. withProtocolVersion() should not return a different implementation of MessageInterface. Maybe it would be better to make a function for getting the response object, not no longer implement MessageInterface in the wrapper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a good solution would be to clone the object and swap the response object with the new one and then return that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is more a PHPStan issue rather that a real PHP one. In the meantime we'll leave the extra comment to prevent the error message from PHPStan.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not PHPStan specific, since it's the PHPDoc it's obviously not PHP specific either since the whole point of PHPDoc is to express type information that isn't possible in PHP.

See the original debate from when they added it to the interface: zendframework/zend-diactoros#37 (comment) / php-fig/fig-standards#793 (comment)

The intended meaning is clearly to return an instance of the same class as the one the method was invoked on, not simply any class that implements the interface.


return $this;
}

public function getHeaders(): array
Expand All @@ -57,61 +56,53 @@ public function getHeaderLine(string $name): string
return $this->response->getHeaderLine($name);
}

/**
* @return MessageInterface
*/
public function withHeader(string $name, $value): MessageInterface
{
return $this->response->withHeader($name, $value);
$this->response->withHeader($name, $value);

return $this;
}

/**
* @return MessageInterface
*/
public function withAddedHeader(string $name, $value): MessageInterface
{
return $this->response->withAddedHeader($name, $value);
$this->response->withAddedHeader($name, $value);

return $this;
}

/**
* @return MessageInterface
*/
public function withoutHeader(string $name): MessageInterface
{
return $this->response->withoutHeader($name);
$this->response->withoutHeader($name);

return $this;
}

/**
* @return StreamInterface
*/
public function getBody(): StreamInterface
{
return $this->response->getBody();
}

/**
* @return MessageInterface
*/
public function withBody(StreamInterface $body): MessageInterface
{
return $this->response->withBody($body);
$this->response->withBody($body);

return $this;
}

public function getStatusCode(): int
{
return $this->response->getStatusCode();
}

/**
* @return ResponseInterface
*/
public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface
{
return $this->response->withStatus($code, $reasonPhrase);
$this->response->withStatus($code, $reasonPhrase);

return $this;
}

public function getReasonPhrase(): string
{
return $this->response->getReasonPhrase();
}
}
}
Loading