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

Commit

Permalink
Merge branch 'hotfix/320-string-reason-phrase' into develop
Browse files Browse the repository at this point in the history
Forward port #320
  • Loading branch information
weierophinney committed Jul 24, 2018
2 parents 43ed6b6 + f6a9cf8 commit 9cd3c94
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- [#320](https://github.com/zendframework/zend-diactoros/pull/320) adds checking within `Response` to ensure that the provided reason
phrase is a string; an `InvalidArgumentException` is now raised if it is not. This change
ensures the class adheres strictly to the PSR-7 specification.

- [#319](https://github.com/zendframework/zend-diactoros/pull/319) provides a fix to `Zend\Diactoros\Response` that ensures that the status
code returned is _always_ an integer (and never a string containing an
integer), thus ensuring it strictly adheres to the PSR-7 specification.
Expand Down
31 changes: 19 additions & 12 deletions src/Response.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @see https://github.com/zendframework/zend-diactoros for the canonical source repository
* @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
*/

Expand Down Expand Up @@ -113,7 +113,7 @@ class Response implements ResponseInterface
/**
* @var string
*/
private $reasonPhrase = '';
private $reasonPhrase;

/**
* @var int
Expand Down Expand Up @@ -146,12 +146,6 @@ public function getStatusCode()
*/
public function getReasonPhrase()
{
if (! $this->reasonPhrase
&& isset($this->phrases[$this->statusCode])
) {
$this->reasonPhrase = $this->phrases[$this->statusCode];
}

return $this->reasonPhrase;
}

Expand All @@ -161,18 +155,18 @@ public function getReasonPhrase()
public function withStatus($code, $reasonPhrase = '')
{
$new = clone $this;
$new->setStatusCode($code);
$new->reasonPhrase = $reasonPhrase;
$new->setStatusCode($code, $reasonPhrase);
return $new;
}

/**
* Set a valid status code.
*
* @param int $code
* @param string $reasonPhrase
* @throws InvalidArgumentException on an invalid status code.
*/
private function setStatusCode($code)
private function setStatusCode($code, $reasonPhrase = '')
{
if (! is_numeric($code)
|| is_float($code)
Expand All @@ -181,11 +175,24 @@ private function setStatusCode($code)
) {
throw new InvalidArgumentException(sprintf(
'Invalid status code "%s"; must be an integer between %d and %d, inclusive',
(is_scalar($code) ? $code : gettype($code)),
is_scalar($code) ? $code : gettype($code),
static::MIN_STATUS_CODE_VALUE,
static::MAX_STATUS_CODE_VALUE
));
}

if (! is_string($reasonPhrase)) {
throw new InvalidArgumentException(sprintf(
'Unsupported response reason phrase; must be a string, received %s',
is_object($reasonPhrase) ? get_class($reasonPhrase) : gettype($reasonPhrase)
));
}

if ($reasonPhrase === '' && isset($this->phrases[$code])) {
$reasonPhrase = $this->phrases[$code];
}

$this->reasonPhrase = $reasonPhrase;
$this->statusCode = (int) $code;
}
}
23 changes: 23 additions & 0 deletions test/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,29 @@ public function testCanSetCustomReasonPhrase()
$this->assertSame('Foo Bar!', $response->getReasonPhrase());
}

public function invalidReasonPhrases()
{
return [
'true' => [ true ],
'false' => [ false ],
'array' => [ [ 200 ] ],
'object' => [ (object) [ 'reasonPhrase' => 'Ok' ] ],
'integer' => [99],
'float' => [400.5],
'null' => [null],
];
}

/**
* @dataProvider invalidReasonPhrases
*/
public function testWithStatusRaisesAnExceptionForNonStringReasonPhrases($invalidReasonPhrase)
{
$this->expectException(InvalidArgumentException::class);

$this->response->withStatus(422, $invalidReasonPhrase);
}

public function testConstructorRaisesExceptionForInvalidStream()
{
$this->expectException(InvalidArgumentException::class);
Expand Down

0 comments on commit 9cd3c94

Please sign in to comment.