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

Commit

Permalink
Merge branch 'hotfix/321'
Browse files Browse the repository at this point in the history
Close #321
  • Loading branch information
weierophinney committed Jul 24, 2018
2 parents d78a757 + cc8d859 commit cd3d811
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- [#321](https://github.com/zendframework/zend-diactoros/pull/321) updates the logic in `Uri::withPort()` to ensure that it checks that the
value provided is either an integer or a string integer, as only those values
may be cast to integer without data loss.

- [#320](https://github.com/zendframework/zend-diactoros/pull/320) adds checking within `Response` to ensure that the provided reason
phrase is a string; an `InvalidArgumentException` is now raised if it is not. This change
ensures the class adheres strictly to the PSR-7 specification.
Expand Down
40 changes: 21 additions & 19 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))
is_object($uri) ? get_class($uri) : gettype($uri)
));
}

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

/**
Expand Down Expand Up @@ -246,7 +248,7 @@ public function withScheme($scheme)
throw new InvalidArgumentException(sprintf(
'%s expects a string argument; received %s',
__METHOD__,
(is_object($scheme) ? get_class($scheme) : gettype($scheme))
is_object($scheme) ? get_class($scheme) : gettype($scheme)
));
}

Expand Down Expand Up @@ -277,14 +279,14 @@ public function withUserInfo($user, $password = null)
throw new InvalidArgumentException(sprintf(
'%s expects a string user argument; received %s',
__METHOD__,
(is_object($user) ? get_class($user) : gettype($user))
is_object($user) ? get_class($user) : gettype($user)
));
}
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))
is_object($password) ? get_class($password) : gettype($password)
));
}

Expand Down Expand Up @@ -313,7 +315,7 @@ public function withHost($host)
throw new InvalidArgumentException(sprintf(
'%s expects a string argument; received %s',
__METHOD__,
(is_object($host) ? get_class($host) : gettype($host))
is_object($host) ? get_class($host) : gettype($host)
));
}

Expand All @@ -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 @@ -437,7 +439,7 @@ public function withFragment($fragment)
throw new InvalidArgumentException(sprintf(
'%s expects a string argument; received %s',
__METHOD__,
(is_object($fragment) ? get_class($fragment) : gettype($fragment))
is_object($fragment) ? get_class($fragment) : gettype($fragment)
));
}

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:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withPort('3001');
$this->assertSame($uri, $new);
$this->assertEquals('3001', $new->getPort());
$this->assertSame(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

0 comments on commit cd3d811

Please sign in to comment.