diff --git a/src/Uri.php b/src/Uri.php index d9833f63..b68ef7d5 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -111,6 +111,10 @@ class Uri implements UriInterface */ public function __construct($uri = '') { + if ('' === $uri) { + return; + } + if (! is_string($uri)) { throw new InvalidArgumentException(sprintf( 'URI passed to constructor must be a string; received "%s"', @@ -118,9 +122,7 @@ public function __construct($uri = '') )); } - if ('' !== $uri) { - $this->parseUri($uri); - } + $this->parseUri($uri); } /** @@ -282,7 +284,7 @@ public function withUserInfo($user, $password = null) } if (null !== $password && ! is_string($password)) { throw new InvalidArgumentException(sprintf( - '%s expects a string password argument; received %s', + '%s expects a string or null password argument; received %s', __METHOD__, (is_object($password) ? get_class($password) : gettype($password)) )); @@ -333,14 +335,14 @@ public function withHost($host) */ public function withPort($port) { - if (! is_numeric($port) && $port !== null) { - throw new InvalidArgumentException(sprintf( - 'Invalid port "%s" specified; must be an integer, an integer string, or null', - (is_object($port) ? get_class($port) : gettype($port)) - )); - } - if ($port !== null) { + if (! is_numeric($port) || is_float($port)) { + throw new InvalidArgumentException(sprintf( + 'Invalid port "%s" specified; must be an integer, an integer string, or null', + (is_object($port) ? get_class($port) : gettype($port)) + )); + } + $port = (int) $port; } @@ -559,7 +561,7 @@ private function filterScheme($scheme) return ''; } - if (! array_key_exists($scheme, $this->allowedSchemes)) { + if (! isset($this->allowedSchemes[$scheme])) { throw new InvalidArgumentException(sprintf( 'Unsupported scheme "%s"; must be any empty string or in the set (%s)', $scheme, @@ -655,7 +657,7 @@ private function filterQuery($query) private function splitQueryValue($value) { $data = explode('=', $value, 2); - if (1 === count($data)) { + if (! isset($data[1])) { $data[] = null; } return $data; diff --git a/test/UriTest.php b/test/UriTest.php index f38c4b50..0e5f0ead 100644 --- a/test/UriTest.php +++ b/test/UriTest.php @@ -137,7 +137,7 @@ public function validPorts() return [ 'null' => [ null ], 'int' => [ 3000 ], - 'string' => [ "3000" ], + 'string-int' => [ '3000' ], ]; } @@ -161,20 +161,21 @@ public function testWithPortReturnsSameInstanceWithProvidedPortIsSameAsBefore() $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz'); $new = $uri->withPort('3001'); $this->assertSame($uri, $new); - $this->assertEquals('3001', $new->getPort()); + $this->assertEquals(3001, $new->getPort()); } public function invalidPorts() { return [ - 'true' => [ true ], - 'false' => [ false ], - 'string' => [ 'string' ], - 'array' => [ [ 3000 ] ], - 'object' => [ (object) [ 3000 ] ], - 'zero' => [ 0 ], - 'too-small' => [ -1 ], - 'too-big' => [ 65536 ], + 'true' => [ true ], + 'false' => [ false ], + 'string' => [ 'string' ], + 'float' => [ 55.5 ], + 'array' => [ [ 3000 ] ], + 'object' => [ (object) ['port' => 3000 ] ], + 'zero' => [ 0 ], + 'too-small' => [ -1 ], + 'too-big' => [ 65536 ], ]; }