Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Status code must be an integer following psr-7 #319

Merged
merged 2 commits into from
Jul 24, 2018

Conversation

snapshotpl
Copy link
Contributor

No description provided.

Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

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

While I agree we need to ensure the status code is an integer, we were doing that previously via a combination of an is_numeric() check + casting to int. The changes you propose create a BC break in that regard, which is problematic when considering how one parses full HTTP response status lines in order to seed a response.

I'd be fine with removing the check for floats, but requiring an integer is a no-go from a BC standpoint.

src/Response.php Outdated
@@ -174,8 +174,7 @@ public function withStatus($code, $reasonPhrase = '')
*/
private function setStatusCode($code)
{
if (! is_numeric($code)
|| is_float($code)
if (! is_int($code)
Copy link
Member

Choose a reason for hiding this comment

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

One potential problem: the status code value may be derived from a string. As an example, it may come from the response line itself:

HTTP/1.1 200 OK

In that case, we previously allowed doing something like the following:

[$protocolAndVersion, $statusCode, $reasonPhrase] = explode(' ', $statusLine, 3);

and passing the values directly to the constructor and/or withStatus().

With this change, the consumer is now forced to cast to int before doing so, which becomes a BC break.

@@ -106,6 +106,6 @@ private static function getStatusLine(StreamInterface $stream)
throw new UnexpectedValueException('No status line detected');
}

return [$matches['version'], $matches['status'], isset($matches['reason']) ? $matches['reason'] : ''];
return [$matches['version'], (int) $matches['status'], isset($matches['reason']) ? $matches['reason'] : ''];
Copy link
Member

Choose a reason for hiding this comment

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

This is exactly the point I was making previously; with the change you suggest above, we now have a BC break requiring consumers make changes such as this.

@@ -78,7 +78,7 @@ public function ianaCodesReasonPhrasesProvider()
$records = $xpath->query('//ns:record');

foreach ($records as $record) {
$value = $xpath->query('.//ns:value', $record)->item(0)->nodeValue;
$value = (int) $xpath->query('.//ns:value', $record)->item(0)->nodeValue;
Copy link
Member

Choose a reason for hiding this comment

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

This also confirms the BC break; previously, casting to (int) was unnecessary.

@snapshotpl
Copy link
Contributor Author

@weierophinney can we keep this changes to v2.0?

@weierophinney
Copy link
Member

can we keep this changes to v2.0?

I'd rather not.

Since the spec itself doesn't have explicit scalar type hints defined, what the constructors and various with*() methods accept is of less importance than what the accessors return. If we can massage the incoming data to the required type without data loss, we should.

If/when we do an update to the PSR-7 specification to add scalar type hints, we can revisit.

@snapshotpl
Copy link
Contributor Author

That's explanation is enough for me. I will prepare changes

@weierophinney weierophinney merged commit acd3bee into zendframework:master Jul 24, 2018
weierophinney added a commit that referenced this pull request Jul 24, 2018
weierophinney added a commit that referenced this pull request Jul 24, 2018
weierophinney added a commit that referenced this pull request Jul 24, 2018
@weierophinney
Copy link
Member

Thanks, @snapshotpl!

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

Successfully merging this pull request may close these issues.

None yet

2 participants