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

Commit c798ed9

Browse files
committed
port is always integer
1 parent b04ebc5 commit c798ed9

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/Uri.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,15 @@ public function withHost($host)
335335
*/
336336
public function withPort($port)
337337
{
338-
if (! is_int($port) && $port !== null) {
339-
throw new InvalidArgumentException(sprintf(
340-
'Invalid port "%s" specified; must be an integer, or null',
341-
(is_object($port) ? get_class($port) : gettype($port))
342-
));
338+
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+
346+
$port = (int) $port;
343347
}
344348

345349
if ($port === $this->port) {

test/UriTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public function validPorts()
137137
return [
138138
'null' => [ null ],
139139
'int' => [ 3000 ],
140+
'string-int' => [ '3000' ],
140141
];
141142
}
142143

@@ -158,7 +159,7 @@ public function testWithPortReturnsNewInstanceWithProvidedPort($port)
158159
public function testWithPortReturnsSameInstanceWithProvidedPortIsSameAsBefore()
159160
{
160161
$uri = new Uri('https://user:[email protected]:3001/foo?bar=baz#quz');
161-
$new = $uri->withPort(3001);
162+
$new = $uri->withPort('3001');
162163
$this->assertSame($uri, $new);
163164
$this->assertEquals(3001, $new->getPort());
164165
}
@@ -169,9 +170,9 @@ public function invalidPorts()
169170
'true' => [ true ],
170171
'false' => [ false ],
171172
'string' => [ 'string' ],
172-
'string-int' => [ '3000' ],
173+
'float' => [ 55.5 ],
173174
'array' => [ [ 3000 ] ],
174-
'object' => [ (object) [ 3000 ] ],
175+
'object' => [ (object) ['port' => 3000 ] ],
175176
'zero' => [ 0 ],
176177
'too-small' => [ -1 ],
177178
'too-big' => [ 65536 ],

0 commit comments

Comments
 (0)