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

Commit b4fe59a

Browse files
committed
Merge branch 'hotfix/103'
Close #103
2 parents 1727fd9 + 627d374 commit b4fe59a

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ All notable changes to this project will be documented in this file, in reverse
2222
`withHeader()` implementation to ensure that if the header existed previously
2323
but using a different casing strategy, the previous version will be removed
2424
in the cloned instance.
25+
- [#103](https://github.com/zendframework/zend-diactoros/pull/103) fixes the
26+
constructor of `Response` to ensure that null status codes are not possible.
2527

2628
## 1.2.0 - 2015-11-24
2729

src/Response.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class Response implements ResponseInterface
102102
/**
103103
* @var int
104104
*/
105-
private $statusCode = 200;
105+
private $statusCode;
106106

107107
/**
108108
* @param string|resource|StreamInterface $body Stream identifier and/or actual stream resource
@@ -120,12 +120,9 @@ public function __construct($body = 'php://memory', $status = 200, array $header
120120
);
121121
}
122122

123-
if (null !== $status) {
124-
$this->validateStatus($status);
125-
}
123+
$this->setStatusCode($status);
126124

127-
$this->stream = ($body instanceof StreamInterface) ? $body : new Stream($body, 'wb+');
128-
$this->statusCode = $status ? (int) $status : 200;
125+
$this->stream = ($body instanceof StreamInterface) ? $body : new Stream($body, 'wb+');
129126

130127
list($this->headerNames, $headers) = $this->filterHeaders($headers);
131128
$this->assertHeaders($headers);
@@ -159,9 +156,8 @@ public function getReasonPhrase()
159156
*/
160157
public function withStatus($code, $reasonPhrase = '')
161158
{
162-
$this->validateStatus($code);
163159
$new = clone $this;
164-
$new->statusCode = (int) $code;
160+
$new->setStatusCode($code);
165161
$new->reasonPhrase = $reasonPhrase;
166162
return $new;
167163
}
@@ -172,7 +168,7 @@ public function withStatus($code, $reasonPhrase = '')
172168
* @param int|string $code
173169
* @throws InvalidArgumentException on an invalid status code.
174170
*/
175-
private function validateStatus($code)
171+
private function setStatusCode($code)
176172
{
177173
if (! is_numeric($code)
178174
|| is_float($code)
@@ -184,6 +180,7 @@ private function validateStatus($code)
184180
(is_scalar($code) ? $code : gettype($code))
185181
));
186182
}
183+
$this->statusCode = $code;
187184
}
188185

189186
/**

test/ResponseTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,17 @@ public function testConstructorIgonoresInvalidHeaders()
151151
'x-valid-string' => [ 'VALID' ],
152152
'x-valid-array' => [ 'VALID' ],
153153
];
154-
$response = new Response('php://memory', null, $headers);
154+
$response = new Response('php://memory', 200, $headers);
155155
$this->assertEquals($expected, $response->getHeaders());
156156
}
157157

158+
public function testInvalidStatusCodeInConstructor()
159+
{
160+
$this->setExpectedException('InvalidArgumentException');
161+
162+
new Response('php://memory', null);
163+
}
164+
158165
public function testReasonPhraseCanBeEmpty()
159166
{
160167
$response = $this->response->withStatus(599);

0 commit comments

Comments
 (0)