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

Commit b04ebc5

Browse files
committed
Port must be an int, and other small improvements
1 parent 7561382 commit b04ebc5

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

src/Uri.php

Lines changed: 10 additions & 12 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,17 +335,13 @@ public function withHost($host)
333335
*/
334336
public function withPort($port)
335337
{
336-
if (! is_numeric($port) && $port !== null) {
338+
if (! is_int($port) && $port !== null) {
337339
throw new InvalidArgumentException(sprintf(
338-
'Invalid port "%s" specified; must be an integer, an integer string, or null',
340+
'Invalid port "%s" specified; must be an integer, or null',
339341
(is_object($port) ? get_class($port) : gettype($port))
340342
));
341343
}
342344

343-
if ($port !== null) {
344-
$port = (int) $port;
345-
}
346-
347345
if ($port === $this->port) {
348346
// Do nothing if no change was made.
349347
return $this;
@@ -559,7 +557,7 @@ private function filterScheme($scheme)
559557
return '';
560558
}
561559

562-
if (! array_key_exists($scheme, $this->allowedSchemes)) {
560+
if (! isset($this->allowedSchemes[$scheme])) {
563561
throw new InvalidArgumentException(sprintf(
564562
'Unsupported scheme "%s"; must be any empty string or in the set (%s)',
565563
$scheme,
@@ -655,7 +653,7 @@ private function filterQuery($query)
655653
private function splitQueryValue($value)
656654
{
657655
$data = explode('=', $value, 2);
658-
if (1 === count($data)) {
656+
if (! isset($data[1])) {
659657
$data[] = null;
660658
}
661659
return $data;

test/UriTest.php

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

@@ -159,22 +158,23 @@ public function testWithPortReturnsNewInstanceWithProvidedPort($port)
159158
public function testWithPortReturnsSameInstanceWithProvidedPortIsSameAsBefore()
160159
{
161160
$uri = new Uri('https://user:[email protected]:3001/foo?bar=baz#quz');
162-
$new = $uri->withPort('3001');
161+
$new = $uri->withPort(3001);
163162
$this->assertSame($uri, $new);
164-
$this->assertEquals('3001', $new->getPort());
163+
$this->assertEquals(3001, $new->getPort());
165164
}
166165

167166
public function invalidPorts()
168167
{
169168
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 ],
169+
'true' => [ true ],
170+
'false' => [ false ],
171+
'string' => [ 'string' ],
172+
'string-int' => [ '3000' ],
173+
'array' => [ [ 3000 ] ],
174+
'object' => [ (object) [ 3000 ] ],
175+
'zero' => [ 0 ],
176+
'too-small' => [ -1 ],
177+
'too-big' => [ 65536 ],
178178
];
179179
}
180180

0 commit comments

Comments
 (0)