Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v6

- name: Run
env:
Expand All @@ -30,7 +30,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v6

- name: Setup php
uses: shivammathur/setup-php@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v6

- name: Setup php
uses: shivammathur/setup-php@v2
Expand All @@ -36,7 +36,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v6

- name: Run actionlint
run: docker run --rm -v "$PWD:/repo" --workdir /repo rhysd/actionlint:latest -color
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v6

- name: Setup php
uses: shivammathur/setup-php@v2
Expand Down
29 changes: 0 additions & 29 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
declare(strict_types=1);

$finder = (new PhpCsFixer\Finder())
// ->in(__DIR__ . '/bin')/
// ->in(__DIR__ . '/public')
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
;
Expand All @@ -21,33 +19,6 @@
'php_unit_internal_class' => false,
'php_unit_test_class_requires_covers' => false,
'declare_strict_types' => true,
// 'blank_line_before_statement' => [
// 'statements' => [
// 'break',
//// 'case',
// 'continue',
// 'declare',
// 'default',
// 'phpdoc',
// 'do',
// 'exit',
// 'for',
//// 'foreach',
// 'goto',
//// 'if',
// 'include',
// 'include_once',
// 'require',
// 'require_once',
// 'return',
// 'switch',
// 'throw',
// 'try',
// 'while',
// 'yield',
// 'yield_from',
// ],
// ],
])
->setFinder($finder)
;
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"php": "^7.4|^8.0|^8.1",
"psr/http-message": "^1",
"algo26-matthias/idna-convert": "^3.0",
"hookedmedia/ip-utils": "^1.1"
"xrstf/ip-utils": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
16 changes: 8 additions & 8 deletions src/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class Normalizer
public const OPTION_REMOVE_PATH_FILES_PATTERNS = 'remove-path-files-patterns';
public const OPTION_REMOVE_QUERY_PARAMETERS_PATTERNS = 'remove-query-parameters-patterns';

public const PRESERVING_NORMALIZATIONS =
self::CAPITALIZE_PERCENT_ENCODING |
self::DECODE_UNRESERVED_CHARACTERS |
self::CONVERT_EMPTY_HTTP_PATH |
self::REMOVE_DEFAULT_FILE_HOST |
self::REMOVE_DEFAULT_PORT |
self::REMOVE_PATH_DOT_SEGMENTS |
self::CONVERT_HOST_UNICODE_TO_PUNYCODE;
public const PRESERVING_NORMALIZATIONS
= self::CAPITALIZE_PERCENT_ENCODING
| self::DECODE_UNRESERVED_CHARACTERS
| self::CONVERT_EMPTY_HTTP_PATH
| self::REMOVE_DEFAULT_FILE_HOST
| self::REMOVE_DEFAULT_PORT
| self::REMOVE_PATH_DOT_SEGMENTS
| self::CONVERT_HOST_UNICODE_TO_PUNYCODE;

// Semantically-lossless normalizations
public const CAPITALIZE_PERCENT_ENCODING = 1;
Expand Down
127 changes: 33 additions & 94 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ public function __construct(string $url)

$userInfo = new UserInfo($user, $pass);

self::applyComponents($this, $scheme, (string) $userInfo, $host, $port, $path, $query, $fragment);
$this->scheme = strtolower($scheme);
$this->userInfo = (string) $userInfo;
$this->host = strtolower($host);
$this->path = Filter::filterPath($path);
$this->query = Filter::filterQueryOrFragment($query);
$this->fragment = Filter::filterQueryOrFragment($fragment);
$this->port = Filter::filterPort($port, $this->getScheme());
}

public function __toString(): string
Expand Down Expand Up @@ -155,16 +161,10 @@ public function withScheme($scheme): self
return $this;
}

return self::applyComponents(
clone $this,
$scheme,
$this->userInfo,
$this->host,
$this->port,
$this->path,
$this->query,
$this->fragment
);
$new = clone $this;
$new->scheme = $scheme;

return $new->withPort($new->getPort());
}

public function withUserInfo($user, $password = null): self
Expand All @@ -175,16 +175,10 @@ public function withUserInfo($user, $password = null): self
return $this;
}

return self::applyComponents(
clone $this,
$this->scheme,
$userInfo,
$this->host,
$this->port,
$this->path,
$this->query,
$this->fragment
);
$new = clone $this;
$new->userInfo = $userInfo;

return $new;
}

public function withHost($host): self
Expand All @@ -195,16 +189,10 @@ public function withHost($host): self
return $this;
}

return self::applyComponents(
clone $this,
$this->scheme,
$this->userInfo,
$host,
$this->port,
$this->path,
$this->query,
$this->fragment
);
$new = clone $this;
$new->host = $host;

return $new;
}

public function withPort($port): self
Expand All @@ -213,20 +201,10 @@ public function withPort($port): self
$port = (int) $port;
}

if ($this->port === $port) {
return $this;
}
$new = clone $this;
$new->port = Filter::filterPort($port, $new->getScheme());

return self::applyComponents(
clone $this,
$this->scheme,
$this->userInfo,
$this->host,
$port,
$this->path,
$this->query,
$this->fragment
);
return $new;
}

public function withPath($path): self
Expand All @@ -237,16 +215,10 @@ public function withPath($path): self
return $this;
}

return self::applyComponents(
clone $this,
$this->scheme,
$this->userInfo,
$this->host,
$this->port,
$path,
$this->query,
$this->fragment
);
$new = clone $this;
$new->path = $path;

return $new;
}

public function withQuery($query): self
Expand All @@ -257,16 +229,10 @@ public function withQuery($query): self
return $this;
}

return self::applyComponents(
clone $this,
$this->scheme,
$this->userInfo,
$this->host,
$this->port,
$this->path,
$query,
$this->fragment
);
$new = clone $this;
$new->query = $query;

return $new;
}

public function withFragment($fragment): self
Expand All @@ -277,36 +243,9 @@ public function withFragment($fragment): self
return $this;
}

return self::applyComponents(
clone $this,
$this->scheme,
$this->userInfo,
$this->host,
$this->port,
$this->path,
$this->query,
$fragment
);
}
$new = clone $this;
$new->fragment = $fragment;

private static function applyComponents(
Uri $url,
string $scheme,
string $userInfo,
string $host,
?int $port,
string $path,
string $query,
string $fragment
): self {
$url->scheme = strtolower($scheme);
$url->userInfo = $userInfo;
$url->host = strtolower($host);
$url->path = Filter::filterPath($path);
$url->query = Filter::filterQueryOrFragment($query);
$url->fragment = Filter::filterQueryOrFragment($fragment);
$url->port = Filter::filterPort($port, $url->getScheme());

return $url;
return $new;
}
}
3 changes: 2 additions & 1 deletion tests/DefaultPortIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace webignition\Uri\Tests;

use PHPUnit\Framework\TestCase;
use webignition\Uri\DefaultPortIdentifier;

class DefaultPortIdentifierTest extends \PHPUnit\Framework\TestCase
class DefaultPortIdentifierTest extends TestCase
{
/**
* @dataProvider isDefaultPortDataProvider
Expand Down
3 changes: 2 additions & 1 deletion tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace webignition\Uri\Tests;

use PHPUnit\Framework\TestCase;
use webignition\Uri\Filter;

class FilterTest extends \PHPUnit\Framework\TestCase
class FilterTest extends TestCase
{
public const UNRESERVED_CHARACTERS = 'a-zA-Z0-9.-_~!$&\'()*+,;=:@';

Expand Down
43 changes: 22 additions & 21 deletions tests/HostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
namespace webignition\Uri\Tests;

use IpUtils\Exception\InvalidExpressionException;
use PHPUnit\Framework\TestCase;
use webignition\Uri\Host;

class HostTest extends \PHPUnit\Framework\TestCase
class HostTest extends TestCase
{
/**
* @dataProvider createDataProvider
Expand Down Expand Up @@ -225,26 +226,6 @@ public function testIpRangeIsPubliclyRoutable(string $ipRange, bool $expectedIsP
}
}

/**
* @throws InvalidExpressionException
*/
public function testLoopbackIpIsNotPubliclyRoutable(): void
{
$host = new Host('127.0.0.1');

$this->assertFalse($host->isPubliclyRoutable());
}

/**
* @throws InvalidExpressionException
*/
public function testDomainNameIsPubliclyRoutable(): void
{
$host = new Host('foo');

$this->assertTrue($host->isPubliclyRoutable());
}

/**
* @return array<mixed>
*/
Expand Down Expand Up @@ -322,6 +303,26 @@ public function ipRangeIsPubliclyRoutableDataProvider(): array
];
}

/**
* @throws InvalidExpressionException
*/
public function testLoopbackIpIsNotPubliclyRoutable(): void
{
$host = new Host('127.0.0.1');

$this->assertFalse($host->isPubliclyRoutable());
}

/**
* @throws InvalidExpressionException
*/
public function testDomainNameIsPubliclyRoutable(): void
{
$host = new Host('foo');

$this->assertTrue($host->isPubliclyRoutable());
}

/**
* @return int[]
*/
Expand Down
Loading