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

Commit 32e9b89

Browse files
snapshotplweierophinney
authored andcommitted
Reason phrase must be a string
1 parent e396d37 commit 32e9b89

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/Response.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4-
* @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
4+
* @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
66
*/
77

@@ -113,7 +113,7 @@ class Response implements ResponseInterface
113113
/**
114114
* @var string
115115
*/
116-
private $reasonPhrase = '';
116+
private $reasonPhrase;
117117

118118
/**
119119
* @var int
@@ -146,12 +146,6 @@ public function getStatusCode()
146146
*/
147147
public function getReasonPhrase()
148148
{
149-
if (! $this->reasonPhrase
150-
&& isset($this->phrases[$this->statusCode])
151-
) {
152-
$this->reasonPhrase = $this->phrases[$this->statusCode];
153-
}
154-
155149
return $this->reasonPhrase;
156150
}
157151

@@ -161,18 +155,18 @@ public function getReasonPhrase()
161155
public function withStatus($code, $reasonPhrase = '')
162156
{
163157
$new = clone $this;
164-
$new->setStatusCode($code);
165-
$new->reasonPhrase = $reasonPhrase;
158+
$new->setStatusCode($code, $reasonPhrase);
166159
return $new;
167160
}
168161

169162
/**
170163
* Set a valid status code.
171164
*
172165
* @param int $code
166+
* @param string $reasonPhrase
173167
* @throws InvalidArgumentException on an invalid status code.
174168
*/
175-
private function setStatusCode($code)
169+
private function setStatusCode($code, $reasonPhrase = '')
176170
{
177171
if (! is_numeric($code)
178172
|| is_float($code)
@@ -186,6 +180,19 @@ private function setStatusCode($code)
186180
static::MAX_STATUS_CODE_VALUE
187181
));
188182
}
183+
184+
if (! is_string($reasonPhrase)) {
185+
throw new InvalidArgumentException(sprintf(
186+
'Unsupported reason phrase; must be a string, received %s',
187+
is_object($reasonPhrase) ? get_class($reasonPhrase) : gettype($reasonPhrase)
188+
));
189+
}
190+
191+
if ($reasonPhrase === '' && isset($this->phrases[$code])) {
192+
$reasonPhrase = $this->phrases[$code];
193+
}
194+
195+
$this->reasonPhrase = $reasonPhrase;
189196
$this->statusCode = (int) $code;
190197
}
191198
}

test/ResponseTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ public function testCanSetCustomReasonPhrase()
112112
$this->assertSame('Foo Bar!', $response->getReasonPhrase());
113113
}
114114

115+
public function testCannotCreateReasonPhraseNotString()
116+
{
117+
$this->expectException(InvalidArgumentException::class);
118+
119+
$response = $this->response->withStatus(422, null);
120+
}
121+
115122
public function testConstructorRaisesExceptionForInvalidStream()
116123
{
117124
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)