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

Commit f7686f5

Browse files
committed
Merge pull request #321 from snapshotpl/uri-improvements
Port must be an integer and other small improvements in Uri
2 parents d78a757 + c798ed9 commit f7686f5

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

src/Uri.php

Lines changed: 15 additions & 13 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"',
117121
(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
/**
@@ -282,7 +284,7 @@ public function withUserInfo($user, $password = null)
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__,
287289
(is_object($password) ? get_class($password) : gettype($password))
288290
));
@@ -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

@@ -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->assertEquals(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)