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

Return static for MessageTrait with... methods #617

Open
braindawg opened this issue Oct 23, 2024 · 0 comments
Open

Return static for MessageTrait with... methods #617

braindawg opened this issue Oct 23, 2024 · 0 comments

Comments

@braindawg
Copy link

braindawg commented Oct 23, 2024

Description
The with... methods on MessageTrait (e.g. withHeader()) currently return typehint a MessageInterface, which breaks static analysis if the analyzer is aware that the Guzzle implementation of PSR7 is being used.

Per the PSR-7 spec, these methods should all return typehint static.

Example

function emitEmptyResponse(EmitterInterface $emitter)
{
    $response = new \GuzzleHttp\Psr7\Response(body: json_encode([]));
    $emitter->emit($response->withHeader('Content-Type', 'json'));
}

This code generates a Psalm error at the emit() call, since the Emitter expects a ResponseInterface but the parent class MessageInterface is provided. I imagine PHPStan would have a similar issue, since the code is incorrect based on the return types currently specified.

Additional context
PSR-7 spec for MessageInterface

This isn't a problem if the code is written well enough, since everything should depend on PSR interfaces like Psr\Http\Message\ResponseInterface (which does typehint static as the return for all these with methods) instead of library-specific PSR implementations, but I don't know what's gained by Guzzle explicitly typing these as MessageInterface.

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

No branches or pull requests

1 participant