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

Commit cd3d811

Browse files
committed
Merge branch 'hotfix/321'
Close #321
2 parents d78a757 + cc8d859 commit cd3d811

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25+
- [#321](https://github.com/zendframework/zend-diactoros/pull/321) updates the logic in `Uri::withPort()` to ensure that it checks that the
26+
value provided is either an integer or a string integer, as only those values
27+
may be cast to integer without data loss.
28+
2529
- [#320](https://github.com/zendframework/zend-diactoros/pull/320) adds checking within `Response` to ensure that the provided reason
2630
phrase is a string; an `InvalidArgumentException` is now raised if it is not. This change
2731
ensures the class adheres strictly to the PSR-7 specification.

src/Uri.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,18 @@ class Uri implements UriInterface
111111
*/
112112
public function __construct($uri = '')
113113
{
114+
if ('' === $uri) {
115+
return;
116+
}
117+
114118
if (! is_string($uri)) {
115119
throw new InvalidArgumentException(sprintf(
116120
'URI passed to constructor must be a string; received "%s"',
117-
(is_object($uri) ? get_class($uri) : gettype($uri))
121+
is_object($uri) ? get_class($uri) : gettype($uri)
118122
));
119123
}
120124

121-
if ('' !== $uri) {
122-
$this->parseUri($uri);
123-
}
125+
$this->parseUri($uri);
124126
}
125127

126128
/**
@@ -246,7 +248,7 @@ public function withScheme($scheme)
246248
throw new InvalidArgumentException(sprintf(
247249
'%s expects a string argument; received %s',
248250
__METHOD__,
249-
(is_object($scheme) ? get_class($scheme) : gettype($scheme))
251+
is_object($scheme) ? get_class($scheme) : gettype($scheme)
250252
));
251253
}
252254

@@ -277,14 +279,14 @@ public function withUserInfo($user, $password = null)
277279
throw new InvalidArgumentException(sprintf(
278280
'%s expects a string user argument; received %s',
279281
__METHOD__,
280-
(is_object($user) ? get_class($user) : gettype($user))
282+
is_object($user) ? get_class($user) : gettype($user)
281283
));
282284
}
283285
if (null !== $password && ! is_string($password)) {
284286
throw new InvalidArgumentException(sprintf(
285-
'%s expects a string password argument; received %s',
287+
'%s expects a string or null password argument; received %s',
286288
__METHOD__,
287-
(is_object($password) ? get_class($password) : gettype($password))
289+
is_object($password) ? get_class($password) : gettype($password)
288290
));
289291
}
290292

@@ -313,7 +315,7 @@ public function withHost($host)
313315
throw new InvalidArgumentException(sprintf(
314316
'%s expects a string argument; received %s',
315317
__METHOD__,
316-
(is_object($host) ? get_class($host) : gettype($host))
318+
is_object($host) ? get_class($host) : gettype($host)
317319
));
318320
}
319321

@@ -333,14 +335,14 @@ public function withHost($host)
333335
*/
334336
public function withPort($port)
335337
{
336-
if (! is_numeric($port) && $port !== null) {
337-
throw new InvalidArgumentException(sprintf(
338-
'Invalid port "%s" specified; must be an integer, an integer string, or null',
339-
(is_object($port) ? get_class($port) : gettype($port))
340-
));
341-
}
342-
343338
if ($port !== null) {
339+
if (! is_numeric($port) || is_float($port)) {
340+
throw new InvalidArgumentException(sprintf(
341+
'Invalid port "%s" specified; must be an integer, an integer string, or null',
342+
is_object($port) ? get_class($port) : gettype($port)
343+
));
344+
}
345+
344346
$port = (int) $port;
345347
}
346348

@@ -437,7 +439,7 @@ public function withFragment($fragment)
437439
throw new InvalidArgumentException(sprintf(
438440
'%s expects a string argument; received %s',
439441
__METHOD__,
440-
(is_object($fragment) ? get_class($fragment) : gettype($fragment))
442+
is_object($fragment) ? get_class($fragment) : gettype($fragment)
441443
));
442444
}
443445

@@ -559,7 +561,7 @@ private function filterScheme($scheme)
559561
return '';
560562
}
561563

562-
if (! array_key_exists($scheme, $this->allowedSchemes)) {
564+
if (! isset($this->allowedSchemes[$scheme])) {
563565
throw new InvalidArgumentException(sprintf(
564566
'Unsupported scheme "%s"; must be any empty string or in the set (%s)',
565567
$scheme,
@@ -655,7 +657,7 @@ private function filterQuery($query)
655657
private function splitQueryValue($value)
656658
{
657659
$data = explode('=', $value, 2);
658-
if (1 === count($data)) {
660+
if (! isset($data[1])) {
659661
$data[] = null;
660662
}
661663
return $data;

test/UriTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function validPorts()
137137
return [
138138
'null' => [ null ],
139139
'int' => [ 3000 ],
140-
'string' => [ "3000" ],
140+
'string-int' => [ '3000' ],
141141
];
142142
}
143143

@@ -161,20 +161,21 @@ public function testWithPortReturnsSameInstanceWithProvidedPortIsSameAsBefore()
161161
$uri = new Uri('https://user:[email protected]:3001/foo?bar=baz#quz');
162162
$new = $uri->withPort('3001');
163163
$this->assertSame($uri, $new);
164-
$this->assertEquals('3001', $new->getPort());
164+
$this->assertSame(3001, $new->getPort());
165165
}
166166

167167
public function invalidPorts()
168168
{
169169
return [
170-
'true' => [ true ],
171-
'false' => [ false ],
172-
'string' => [ 'string' ],
173-
'array' => [ [ 3000 ] ],
174-
'object' => [ (object) [ 3000 ] ],
175-
'zero' => [ 0 ],
176-
'too-small' => [ -1 ],
177-
'too-big' => [ 65536 ],
170+
'true' => [ true ],
171+
'false' => [ false ],
172+
'string' => [ 'string' ],
173+
'float' => [ 55.5 ],
174+
'array' => [ [ 3000 ] ],
175+
'object' => [ (object) ['port' => 3000 ] ],
176+
'zero' => [ 0 ],
177+
'too-small' => [ -1 ],
178+
'too-big' => [ 65536 ],
178179
];
179180
}
180181

0 commit comments

Comments
 (0)