Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,18 @@ 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"',
(is_object($uri) ? get_class($uri) : gettype($uri))
));
}

if ('' !== $uri) {
$this->parseUri($uri);
}
$this->parseUri($uri);
}

/**
Expand Down Expand Up @@ -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))
));
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 11 additions & 10 deletions test/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function validPorts()
return [
'null' => [ null ],
'int' => [ 3000 ],
'string' => [ "3000" ],
'string-int' => [ '3000' ],
];
}

Expand All @@ -161,20 +161,21 @@ public function testWithPortReturnsSameInstanceWithProvidedPortIsSameAsBefore()
$uri = new Uri('https://user:[email protected]: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 ],
];
}

Expand Down