Skip to content

Commit 5790a80

Browse files
committed
Improve Rules code
1 parent ec0fd20 commit 5790a80

File tree

5 files changed

+35
-52
lines changed

5 files changed

+35
-52
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ All Notable changes to `PHP Domain Parser` **5.x** series will be documented in
1414
- `Pdp\Domain::withSubDomain` updates the `Pdp\Domain` sub domain part.
1515
- `Pdp\Domain::withLabel` adds a new label to the `Pdp\Domain`.
1616
- `Pdp\Domain::withoutLabel` removes a label from the `Pdp\Domain`.
17+
- `Pdp\Domain::resolve` attach a public suffix to the `Pdp\Domain`.
18+
- `Pdp\Domain::isResolvable` tell whether the current `Pdp\Domain` can have a public suffix attached to it or not.
1719
- `Pdp\PublicSuffix::createFromDomain` returns a new `Pdp\PublicSuffix` object from a `Pdp\Domain`object
1820

1921
### Fixed

src/Converter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private function getSection(string $section, string $line): string
8282
* distribution
8383
*
8484
* @param array $list Initially an empty array, this eventually
85-
* becomes the array representation of the Public Suffix List
85+
* becomes the array representation of a Public Suffix List section
8686
* @param array $rule_parts One line (rule) from the Public Suffix List
8787
* exploded on '.', or the remaining portion of that array during recursion
8888
*

src/Domain.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private function setPublicSuffix(PublicSuffix $publicSuffix): PublicSuffix
9595
return new PublicSuffix();
9696
}
9797

98-
if (null === $this->domain || false === strpos($this->domain, '.')) {
98+
if (!$this->isResolvable()) {
9999
throw new Exception(sprintf('The domain `%s` can not contain a public suffix', $this->domain));
100100
}
101101

@@ -264,7 +264,7 @@ public function keys(string $label): array
264264
*
265265
* The registered or registrable domain is the public suffix plus one additional label.
266266
*
267-
* This method should return null if the registrable domain is the same as the public suffix.
267+
* This method returns null if the registrable domain is equal to the public suffix.
268268
*
269269
* @return string|null registrable domain
270270
*/
@@ -278,8 +278,8 @@ public function getRegistrableDomain()
278278
*
279279
* The sub domain represents the remaining labels without the registrable domain.
280280
*
281-
* This method should return null if the registrable domain is null
282-
* This method should return null if the registrable domain is the same as the public suffix
281+
* This method returns null if the registrable domain is null
282+
* This method returns null if the registrable domain is equal to the public suffix
283283
*
284284
* @return string|null registrable domain
285285
*/
@@ -298,6 +298,16 @@ public function getPublicSuffix()
298298
return $this->publicSuffix->getContent();
299299
}
300300

301+
/**
302+
* Tells whether the given domain can be resolved.
303+
*
304+
* @return bool
305+
*/
306+
public function isResolvable(): bool
307+
{
308+
return 2 <= count($this->labels);
309+
}
310+
301311
/**
302312
* Tells whether the public suffix has a matching rule in a Public Suffix List.
303313
*
@@ -433,10 +443,7 @@ public function withSubDomain($subDomain): self
433443
}
434444

435445
$clone = clone $this;
436-
$clone->labels = array_merge(
437-
array_slice($this->labels, 0, count($this->publicSuffix) + 1),
438-
iterator_to_array($subDomain)
439-
);
446+
$clone->labels = array_merge(array_slice($this->labels, 0, count($this->publicSuffix) + 1), iterator_to_array($subDomain));
440447
$clone->domain = implode('.', array_reverse($clone->labels));
441448
$clone->subDomain = $subDomain->getContent();
442449

@@ -462,7 +469,7 @@ public function withPublicSuffix($publicSuffix): self
462469
}
463470

464471
if (null === $this->publicSuffix->getContent()) {
465-
throw new Exception('A public suffix can not be added to domain without a public suffix.');
472+
throw new Exception('A public suffix can not be added to a domain without a public suffix part.');
466473
}
467474

468475
$publicSuffix = $this->normalize($publicSuffix);
@@ -471,10 +478,7 @@ public function withPublicSuffix($publicSuffix): self
471478
}
472479

473480
$clone = clone $this;
474-
$clone->labels = array_merge(
475-
iterator_to_array($publicSuffix),
476-
array_slice($this->labels, count($this->publicSuffix))
477-
);
481+
$clone->labels = array_merge(iterator_to_array($publicSuffix), array_slice($this->labels, count($this->publicSuffix)));
478482
$clone->domain = implode('.', array_reverse($clone->labels));
479483
$clone->publicSuffix = $publicSuffix;
480484
$clone->registrableDomain = $this->labels[count($this->publicSuffix)].'.'.$publicSuffix->getContent();

src/Rules.php

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,11 @@ public function getPublicSuffix($domain = null, string $section = self::ALL_DOMA
102102
{
103103
$this->validateSection($section);
104104
$domain = $domain instanceof Domain ? $domain : new Domain($domain);
105-
if (!$this->isMatchable($domain)) {
105+
if (!$domain->isResolvable()) {
106106
throw new Exception(sprintf('The domain `%s` can not contain a public suffix', $domain->getContent()));
107107
}
108108

109-
$publicSuffix = $this->findPublicSuffix($domain, $section);
110-
if (null === $publicSuffix->getContent()) {
111-
$publicSuffix = new PublicSuffix($domain->getLabel(0));
112-
}
113-
114-
return PublicSuffix::createFromDomain($domain->resolve($publicSuffix));
109+
return PublicSuffix::createFromDomain($domain->resolve($this->findPublicSuffix($domain, $section)));
115110
}
116111

117112
/**
@@ -127,16 +122,11 @@ public function resolve($domain = null, string $section = self::ALL_DOMAINS): Do
127122
$this->validateSection($section);
128123
try {
129124
$domain = $domain instanceof Domain ? $domain : new Domain($domain);
130-
if (!$this->isMatchable($domain)) {
125+
if (!$domain->isResolvable()) {
131126
return $domain;
132127
}
133128

134-
$publicSuffix = $this->findPublicSuffix($domain, $section);
135-
if (null === $publicSuffix->getContent()) {
136-
$publicSuffix = new PublicSuffix($domain->getLabel(0));
137-
}
138-
139-
return $domain->resolve($publicSuffix);
129+
return $domain->resolve($this->findPublicSuffix($domain, $section));
140130
} catch (Exception $e) {
141131
return new Domain();
142132
}
@@ -156,26 +146,13 @@ private function validateSection(string $section)
156146
}
157147

158148
$rules = $this->rules[$section] ?? null;
159-
if (null !== $rules && is_array($rules)) {
149+
if (is_array($rules)) {
160150
return;
161151
}
162152

163153
throw new Exception(sprintf('%s is an unknown Public Suffix List section', $section));
164154
}
165155

166-
/**
167-
* Tells whether the given domain can be resolved.
168-
*
169-
* @param DomainInterface $domain
170-
*
171-
* @return bool
172-
*/
173-
private function isMatchable(DomainInterface $domain): bool
174-
{
175-
return 1 < count($domain)
176-
&& 0 < strpos($domain->getContent(), '.');
177-
}
178-
179156
/**
180157
* Returns the matched public suffix.
181158
*
@@ -201,7 +178,7 @@ private function findPublicSuffix(DomainInterface $domain, string $section): Pub
201178
return $icann;
202179
}
203180

204-
return new PublicSuffix();
181+
return new PublicSuffix($domain->getLabel(0));
205182
}
206183

207184
/**
@@ -214,7 +191,7 @@ private function findPublicSuffix(DomainInterface $domain, string $section): Pub
214191
*/
215192
private function findPublicSuffixFromSection(DomainInterface $domain, string $section): PublicSuffix
216193
{
217-
$rules = $this->rules[$section] ?? [];
194+
$rules = $this->rules[$section];
218195
$matches = [];
219196
foreach ($domain as $label) {
220197
//match exception rule
@@ -238,7 +215,7 @@ private function findPublicSuffixFromSection(DomainInterface $domain, string $se
238215
}
239216

240217
if (empty($matches)) {
241-
return new PublicSuffix();
218+
return new PublicSuffix($domain->getLabel(0));
242219
}
243220

244221
return new PublicSuffix(implode('.', array_reverse($matches)), $section);

tests/RulesTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testDomainInternalPhpMethod()
7070
/**
7171
* @covers ::resolve
7272
* @covers ::validateSection
73-
* @covers ::isMatchable
73+
* @covers \Pdp\Domain::isResolvable
7474
* @covers \Pdp\PublicSuffix::setSection
7575
* @covers \Pdp\PublicSuffix::isKnown
7676
* @covers \Pdp\Domain::isKnown
@@ -106,7 +106,7 @@ public function testResolveThrowsExceptionOnWrongDomainType()
106106
/**
107107
* @covers ::resolve
108108
* @covers ::validateSection
109-
* @covers ::isMatchable
109+
* @covers \Pdp\Domain::isResolvable
110110
* @covers ::findPublicSuffix
111111
* @covers ::findPublicSuffixFromSection
112112
* @covers \Pdp\PublicSuffix::setSection
@@ -123,7 +123,7 @@ public function testIsSuffixValidFalse()
123123
/**
124124
* @covers ::resolve
125125
* @covers ::validateSection
126-
* @covers ::isMatchable
126+
* @covers \Pdp\Domain::isResolvable
127127
* @covers ::findPublicSuffix
128128
* @covers ::findPublicSuffixFromSection
129129
* @covers \Pdp\PublicSuffix::setSection
@@ -147,7 +147,7 @@ public function testIsSuffixValidTrue()
147147
/**
148148
* @covers ::resolve
149149
* @covers ::validateSection
150-
* @covers ::isMatchable
150+
* @covers \Pdp\Domain::isResolvable
151151
* @covers ::findPublicSuffix
152152
* @covers ::findPublicSuffixFromSection
153153
* @covers \Pdp\PublicSuffix::setSection
@@ -171,7 +171,7 @@ public function testIsSuffixValidFalseWithPunycoded()
171171
/**
172172
* @covers ::resolve
173173
* @covers ::validateSection
174-
* @covers ::isMatchable
174+
* @covers \Pdp\Domain::isResolvable
175175
* @covers ::findPublicSuffix
176176
* @covers ::findPublicSuffixFromSection
177177
* @covers \Pdp\PublicSuffix::setSection
@@ -385,7 +385,7 @@ public function parseDataProvider()
385385
/**
386386
* @covers ::getPublicSuffix
387387
* @covers ::validateSection
388-
* @covers ::isMatchable
388+
* @covers \Pdp\Domain::isResolvable
389389
* @covers \Pdp\IDNAConverterTrait::setDomain
390390
* @dataProvider invalidParseProvider
391391
*
@@ -417,7 +417,7 @@ public function invalidParseProvider()
417417
/**
418418
* @covers ::getPublicSuffix
419419
* @covers ::validateSection
420-
* @covers ::isMatchable
420+
* @covers \Pdp\Domain::isResolvable
421421
* @covers \Pdp\IDNAConverterTrait::setDomain
422422
* @dataProvider validPublicSectionProvider
423423
*

0 commit comments

Comments
 (0)