Skip to content

Commit e9eb70f

Browse files
committed
Improve internal codebase
1 parent 65be016 commit e9eb70f

File tree

6 files changed

+34
-25
lines changed

6 files changed

+34
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ All Notable changes to `PHP Domain Parser` starting from the **5.x** series will
66

77
### Added
88

9-
- `RegisteredName` class to allow subdomain with IP-like labels [#347](https://github.com/jeremykendall/php-domain-parser/issues/347)
9+
- `RegisteredName` class to allow subdomain with IP-like labels
1010

1111
### Fixed
1212

1313
- Using PHPUnit 10
14+
- `Domain` decorates the new `RegisteredName` class [#347](https://github.com/jeremykendall/php-domain-parser/issues/347)
1415

1516
### Deprecated
1617

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
"friendsofphp/php-cs-fixer": "^v3.13.2",
5050
"guzzlehttp/guzzle": "^7.5",
5151
"guzzlehttp/psr7": "^1.6 || ^2.4.3",
52-
"phpstan/phpstan": "^1.9.17",
52+
"phpstan/phpstan": "^1.9.18",
5353
"phpstan/phpstan-phpunit": "^1.3.4",
5454
"phpstan/phpstan-strict-rules": "^1.4.5",
55-
"phpunit/phpunit": "^10.0.7",
55+
"phpunit/phpunit": "^10.0.11",
5656
"psr/http-factory": "^1.0.1",
5757
"psr/simple-cache": "^1.0.1",
5858
"symfony/cache": "^v5.0.0 || ^v6.0.0"

src/Domain.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
use Iterator;
88
use Stringable;
9+
use const FILTER_FLAG_IPV4;
10+
use const FILTER_VALIDATE_IP;
911

1012
final class Domain implements DomainName
1113
{
1214
private function __construct(private RegisteredName $registeredName)
1315
{
14-
if ($this->registeredName->isIpv4()) {
16+
if (false !== filter_var($this->registeredName->value(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1517
throw SyntaxError::dueToUnsupportedType($this->registeredName->toString());
1618
}
1719
}
@@ -88,11 +90,6 @@ public function labels(): array
8890
return $this->registeredName->labels();
8991
}
9092

91-
public function isIpv4(): bool
92-
{
93-
return $this->registeredName->isIpv4();
94-
}
95-
9693
private function newInstance(RegisteredName $registeredName): self
9794
{
9895
if ($registeredName->value() === $this->registeredName->value()) {
@@ -133,9 +130,9 @@ public function withLabel(int $key, DomainNameProvider|Host|string|Stringable|nu
133130
return $this->newInstance($this->registeredName->withLabel($key, $label));
134131
}
135132

136-
public function withoutLabel(int $key, int ...$keys): self
133+
public function withoutLabel(int ...$keys): self
137134
{
138-
return $this->newInstance($this->registeredName->withoutLabel($key, ...$keys));
135+
return $this->newInstance($this->registeredName->withoutLabel(...$keys));
139136
}
140137

141138
public function clear(): self

src/DomainName.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function withLabel(int $key, DomainNameProvider|Host|string|Stringable|nu
101101
*
102102
* @throws CannotProcessHost If the key is out of bounds
103103
*/
104-
public function withoutLabel(int $key, int ...$keys): self;
104+
public function withoutLabel(int ...$keys): self;
105105

106106
/**
107107
* Returns an instance with all labels removed.

src/Host.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
* @see https://tools.ietf.org/html/rfc1034#section-3.5
1212
* @see https://tools.ietf.org/html/rfc1123#section-2.1
1313
* @see https://tools.ietf.org/html/rfc5890
14-
*
15-
* @method bool isIpv4()
1614
*/
1715
interface Host extends Countable, JsonSerializable
1816
{

src/RegisteredName.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use function array_keys;
1111
use function array_reverse;
1212
use function array_slice;
13-
use function array_unshift;
1413
use function count;
1514
use function explode;
1615
use function filter_var;
@@ -42,30 +41,44 @@ final class RegisteredName implements DomainName
4241
private readonly array $labels;
4342
private readonly ?string $domain;
4443

45-
private function __construct(private string $type, DomainNameProvider|Host|Stringable|string|int|null $domain)
44+
/**
45+
* @throws CannotProcessHost
46+
*/
47+
private function __construct(private readonly string $type, DomainNameProvider|Host|Stringable|string|int|null $domain)
4648
{
4749
$this->domain = $this->parseDomain($domain);
4850
$this->labels = null === $this->domain ? [] : array_reverse(explode('.', $this->domain));
4951
}
5052

5153
/**
5254
* @param array{domain:string|null, type:string} $properties
55+
*
56+
* @throws CannotProcessHost
5357
*/
5458
public static function __set_state(array $properties): self
5559
{
5660
return new self($properties['type'], $properties['domain']);
5761
}
5862

63+
/**
64+
* @throws CannotProcessHost
65+
*/
5966
public static function fromIDNA2003(DomainNameProvider|Host|Stringable|string|int|null $domain): self
6067
{
6168
return new self(self::IDNA_2003, $domain);
6269
}
6370

71+
/**
72+
* @throws CannotProcessHost
73+
*/
6474
public static function fromIDNA2008(DomainNameProvider|Host|Stringable|string|int|null $domain): self
6575
{
6676
return new self(self::IDNA_2008, $domain);
6777
}
6878

79+
/**
80+
* @throws CannotProcessHost
81+
*/
6982
private function parseDomain(DomainNameProvider|Host|Stringable|string|int|null $domain): ?string
7083
{
7184
if ($domain instanceof DomainNameProvider) {
@@ -136,11 +149,6 @@ private function domainToUnicode(string $domain): string
136149
)->result();
137150
}
138151

139-
public function isIpv4(): bool
140-
{
141-
return false !== filter_var($this->domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
142-
}
143-
144152
/**
145153
* @return Iterator<string>
146154
*/
@@ -245,7 +253,7 @@ private function normalize(DomainNameProvider|Host|Stringable|string|null $domai
245253
}
246254

247255
if (null === $domain) {
248-
return $domain;
256+
return null;
249257
}
250258

251259
$domain = (string) $domain;
@@ -297,13 +305,12 @@ public function withLabel(int $key, DomainNameProvider|Host|string|Stringable|nu
297305
return new self($this->type, implode('.', array_reverse($labels)));
298306
}
299307

300-
public function withoutLabel(int $key, int ...$keys): self
308+
public function withoutLabel(int ...$keys): self
301309
{
302-
array_unshift($keys, $key);
303310
$nbLabels = count($this->labels);
304311
foreach ($keys as &$offset) {
305312
if (- $nbLabels > $offset || $nbLabels - 1 < $offset) {
306-
throw SyntaxError::dueToInvalidLabelKey($this, $key);
313+
throw SyntaxError::dueToInvalidLabelKey($this, $offset);
307314
}
308315

309316
if (0 > $offset) {
@@ -327,6 +334,9 @@ public function withoutLabel(int $key, int ...$keys): self
327334
return new self($this->type, [] === $labels ? null : implode('.', array_reverse($labels)));
328335
}
329336

337+
/**
338+
* @throws CannotProcessHost
339+
*/
330340
public function clear(): self
331341
{
332342
if (null === $this->domain) {
@@ -336,6 +346,9 @@ public function clear(): self
336346
return new self($this->type, null);
337347
}
338348

349+
/**
350+
* @throws CannotProcessHost
351+
*/
339352
public function slice(int $offset, int $length = null): self
340353
{
341354
$nbLabels = count($this->labels);

0 commit comments

Comments
 (0)