Skip to content

Commit 0e13654

Browse files
committed
Adding support for PHP Native URI and URL
1 parent 98401b3 commit 0e13654

File tree

5 files changed

+44
-42
lines changed

5 files changed

+44
-42
lines changed

.github/workflows/build.yaml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@ jobs:
1010
runs-on: ubuntu-22.04
1111
strategy:
1212
matrix:
13-
php: ['8.1', '8.2', '8.3', '8.4']
13+
php: ['8.1', '8.2', '8.3', '8.4', '8.5']
1414
stability: [prefer-lowest, prefer-stable]
15-
include:
16-
- php: '8.4'
17-
flags: "--ignore-platform-req=php"
18-
phpunit-flags: '--no-coverage'
19-
stability: prefer-stable
2015
steps:
2116
- name: Checkout code
22-
uses: actions/checkout@v3
17+
uses: actions/checkout@v4
2318

2419
- name: Setup PHP
2520
uses: shivammathur/setup-php@v2
@@ -37,7 +32,7 @@ jobs:
3732
id: composer-cache
3833
run: |
3934
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
40-
- uses: actions/cache@v4
35+
- uses: actions/cache@v3
4136
with:
4237
path: ${{ steps.composer-cache.outputs.dir }}
4338
key: ${{ runner.os }}-composer-${{ matrix.stability }}-${{ matrix.flags }}-${{ hashFiles('**/composer.lock') }}
@@ -50,16 +45,16 @@ jobs:
5045

5146
- name: Run Unit tests with coverage
5247
run: composer phpunit -- ${{ matrix.phpunit-flags }}
53-
if: ${{ matrix.php == '8.3' || matrix.php == '8.2' || matrix.php == '8.1'}}
48+
if: ${{ matrix.php != '8.5' }}
5449

55-
- name: Run Unit tests without coverage
56-
run: composer phpunit:min
57-
if: ${{ matrix.php == '8.4'}}
50+
- name: Run Unit tests without coverage
51+
run: vendor/bin/phpunit --no-coverage
52+
if: ${{ matrix.php == '8.5' }}
5853

5954
- name: Run static analysis
60-
run: composer phpstan
61-
if: ${{ matrix.php == '8.3' && matrix.stability == 'prefer-stable'}}
55+
run: composer phpstan-build
56+
if: ${{ matrix.php == '8.4' && matrix.stability == 'prefer-stable'}}
6257

6358
- name: Run Coding style rules
6459
run: composer phpcs:fix
65-
if: ${{ matrix.php == '8.3' && matrix.stability == 'prefer-stable'}}
60+
if: ${{ matrix.php == '8.4' && matrix.stability == 'prefer-stable'}}

.php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
$config = new PhpCsFixer\Config();
1010

1111
return $config
12+
->setUnsupportedPhpVersionAllowed(true)
1213
->setParallelConfig(ParallelConfigFactory::detect())
1314
->setRules([
1415
'@PSR12' => true,

composer.json

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,22 @@
4444
"ext-filter": "*"
4545
},
4646
"require-dev": {
47-
"friendsofphp/php-cs-fixer": "^3.65.0",
48-
"guzzlehttp/guzzle": "^7.9.2",
49-
"guzzlehttp/psr7": "^1.6 || ^2.7.0",
50-
"phpstan/phpstan": "^1.12.13",
47+
"friendsofphp/php-cs-fixer": "^3.91.3",
48+
"guzzlehttp/guzzle": "^7.10.0",
49+
"guzzlehttp/psr7": "^1.6 || ^2.8.0",
50+
"phpstan/phpstan": "^1.12.32",
5151
"phpstan/phpstan-phpunit": "^1.4.2",
52-
"phpstan/phpstan-strict-rules": "^1.6.1",
53-
"phpunit/phpunit": "^10.5.15 || ^11.5.1",
52+
"phpstan/phpstan-strict-rules": "^1.6.2",
53+
"phpunit/phpunit": "^10.5.15 || ^11.5.1 || ^12.5.2",
5454
"psr/http-factory": "^1.1.0",
5555
"psr/simple-cache": "^1.0.1 || ^2.0.0",
56-
"symfony/cache": "^v5.0.0 || ^6.4.16",
57-
"symfony/var-dumper": "^v6.4.18 || ^7.2"
56+
"symfony/cache": "^v5.0.0 || ^6.4.16 || ^v7.4.1 || ^v8.0.0",
57+
"symfony/var-dumper": "^v6.4.18 || ^7.4 || ^v8.0.0",
58+
"league/uri-polyfill" : ">=7.7.0"
5859
},
5960
"suggest": {
61+
"ext-uri": "to use the PHP native URI class",
62+
"league/uri-polyfill" : "Needed to backport the PHP URI extension for older versions of PHP",
6063
"psr/http-client-implementation": "To use the storage functionality which depends on PSR-18",
6164
"psr/http-factory-implementation": "To use the storage functionality which depends on PSR-17",
6265
"psr/simple-cache-implementation": "To use the storage functionality which depends on PSR-16",
@@ -70,7 +73,7 @@
7073
}
7174
},
7275
"scripts": {
73-
"phpcs": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix -vvv --diff --dry-run --allow-risky=yes --ansi",
76+
"phpcs": "php-cs-fixer fix -vvv --diff --dry-run --allow-risky=yes --ansi",
7477
"phpcs:fix": "php-cs-fixer fix -vvv --allow-risky=yes --ansi",
7578
"phpstan": "phpstan analyse -c phpstan.neon --ansi --memory-limit=192M",
7679
"phpunit": "XDEBUG_MODE=coverage phpunit --coverage-text",

src/Rules.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use SplFileObject;
88
use SplTempFileObject;
99
use Stringable;
10+
use Uri\Rfc3986\Uri;
11+
use Uri\WhatWg\Url;
1012

1113
use function array_key_exists;
1214
use function array_pop;
@@ -158,7 +160,7 @@ public static function __set_state(array $properties): self
158160
return new self($properties['rules']);
159161
}
160162

161-
public function resolve(DomainNameProvider|Host|Stringable|string|int|null $host): ResolvedDomainName
163+
public function resolve(DomainNameProvider|Host|Stringable|Url|Uri|string|int|null $host): ResolvedDomainName
162164
{
163165
try {
164166
return $this->getCookieDomain($host);
@@ -169,7 +171,7 @@ public function resolve(DomainNameProvider|Host|Stringable|string|int|null $host
169171
}
170172
}
171173

172-
public function getCookieDomain(DomainNameProvider|Host|Stringable|string|int|null $host): ResolvedDomainName
174+
public function getCookieDomain(DomainNameProvider|Host|Stringable|Url|Uri|string|int|null $host): ResolvedDomainName
173175
{
174176
$domain = $this->validateDomain($host);
175177
[$suffixLength, $section] = $this->resolveSuffix($domain->withoutRootLabel(), self::UNKNOWN_DOMAINS);
@@ -181,7 +183,7 @@ public function getCookieDomain(DomainNameProvider|Host|Stringable|string|int|nu
181183
};
182184
}
183185

184-
public function getICANNDomain(DomainNameProvider|Host|Stringable|string|int|null $host): ResolvedDomainName
186+
public function getICANNDomain(DomainNameProvider|Host|Stringable|Url|Uri|string|int|null $host): ResolvedDomainName
185187
{
186188
$domain = $this->validateDomain($host);
187189
[$suffixLength, $section] = $this->resolveSuffix($domain, self::ICANN_DOMAINS);
@@ -192,7 +194,7 @@ public function getICANNDomain(DomainNameProvider|Host|Stringable|string|int|nul
192194
return ResolvedDomain::fromICANN($domain, $suffixLength);
193195
}
194196

195-
public function getPrivateDomain(DomainNameProvider|Host|Stringable|string|int|null $host): ResolvedDomainName
197+
public function getPrivateDomain(DomainNameProvider|Host|Stringable|Url|Uri|string|int|null $host): ResolvedDomainName
196198
{
197199
$domain = $this->validateDomain($host);
198200
[$suffixLength, $section] = $this->resolveSuffix($domain, self::PRIVATE_DOMAINS);
@@ -209,17 +211,15 @@ public function getPrivateDomain(DomainNameProvider|Host|Stringable|string|int|n
209211
* @throws SyntaxError If the domain is invalid
210212
* @throws UnableToResolveDomain If the domain can not be resolved
211213
*/
212-
private function validateDomain(DomainNameProvider|Host|Stringable|string|int|null $domain): DomainName
214+
private function validateDomain(DomainNameProvider|Host|Stringable|Uri|Url|string|int|null $domain): DomainName
213215
{
214-
if ($domain instanceof DomainNameProvider) {
215-
$domain = $domain->domain();
216-
}
217-
218-
if (!$domain instanceof DomainName) {
219-
$domain = Domain::fromIDNA2008($domain);
220-
}
221-
222-
return $domain;
216+
return match (true) {
217+
$domain instanceof DomainNameProvider => $domain->domain(),
218+
$domain instanceof Uri => Domain::fromIDNA2008($domain->getRawHost()),
219+
$domain instanceof Url => Domain::fromIDNA2008($domain->getAsciiHost()),
220+
$domain instanceof DomainName => $domain,
221+
default => Domain::fromIDNA2008($domain),
222+
};
223223
}
224224

225225
/**

src/SuffixTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,15 @@ public function testToUnicodeReturnsSameInstance(): void
128128
self::assertEquals($instance->toUnicode(), $instance);
129129
}
130130

131+
/**
132+
* @param list<string> $labels
133+
*
134+
* @throws CannotProcessHost
135+
*/
131136
#[DataProvider('countableProvider')]
132-
public function testCountable(?string $domain, int $nbLabels): void
137+
public function testCountable(?string $domain, int $nbLabels, array $labels = []): void
133138
{
134-
$domain = Suffix::fromUnknown($domain);
135-
136-
self::assertCount($nbLabels, $domain);
139+
self::assertCount($nbLabels, Suffix::fromUnknown($domain));
137140
}
138141

139142
/**

0 commit comments

Comments
 (0)