Nyholm / psr7

A super lightweight PSR-7 implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

createResponse on factory creates missing reasonPhrase

dominikzogg opened this issue · comments

PSR17's createResponse

public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface;

means, that there is never a null value on $reasonPhrase

this means that within the Response implementation, using the factory is always empty, cause the null check https://github.com/Nyholm/psr7/blob/master/src/Response.php#L49.

what is OK, but dangerous is also line https://github.com/Nyholm/psr7/blob/master/src/Response.php#L50 cause its checks against the property and the assignment agains the variable within the method.

I would suggest the following:

This would not only fix my issue, but also the issue given null as reason which a not supported status code, which leads to an interface implementation issue cause reasonPhrase should not return null as far as i unterstand the PSR7.

// If we got no body, defer initialization of the stream until Response::getBody()
if ('' !== $body && null !== $body) {
    $this->stream = Stream::create($body);
}

$this->statusCode = $status;
$this->reasonPhrase = (string) $reason;

$this->setHeaders($headers);

if ('' === $this->reasonPhrase && isset(self::PHRASES[$this->statusCode])) {
    $this->reasonPhrase = self::PHRASES[$this->statusCode];
}

$this->protocol = $version;