Skip to content

Commit

Permalink
Merge pull request #295 from cosmastech/fix/port-as-string
Browse files Browse the repository at this point in the history
Cast `Uri@filterPort()` `$port` parameter to string if non-null
  • Loading branch information
akrabat authored Jun 8, 2024
2 parents f7b2d1a + 132ae65 commit 753e964
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,21 @@ protected function hasStandardPort(): bool
/**
* Filter Uri port.
*
* @param int|null $port The Uri port number.
* @param int|string|null $port The Uri port number.
*
* @return int|null
*
* @throws InvalidArgumentException If the port is invalid.
*/
protected function filterPort($port): ?int
{
if (is_null($port) || (is_integer($port) && ($port >= 1 && $port <= 65535))) {
if (is_null($port)) {
return null;
}

$port = (int) $port;

if ($port >= 1 && $port <= 65535) {
return $port;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ public function testWithPortInvalidString()
$this->uriFactory()->withPort('Foo');
}

public function testWithPortIntegerAsString()
{
$uri = $this->uriFactory()->withPort("199");

$this->assertEquals(199, $uri->getPort());
}

public function testGetPath()
{
$this->assertEquals('/foo/bar', $this->uriFactory()->getPath());
Expand Down

0 comments on commit 753e964

Please sign in to comment.