Skip to content

Commit ec0fd20

Browse files
committed
improve code
1 parent 18496ee commit ec0fd20

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

src/Domain.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,22 @@ private function setPublicSuffix(PublicSuffix $publicSuffix): PublicSuffix
115115
/**
116116
* Normalize the domain name encoding content.
117117
*
118-
* @param mixed $domain
118+
* @param mixed $subject
119119
*
120120
* @return mixed
121121
*/
122-
private function normalize($domain)
122+
private function normalize($subject)
123123
{
124+
if (null === $this->domain || null === $subject->getContent()) {
125+
return $subject;
126+
}
127+
124128
static $pattern = '/[^\x20-\x7f]/';
125-
if (null !== $this->domain && preg_match($pattern, $this->domain)) {
126-
return $domain->toUnicode();
129+
if (!preg_match($pattern, $this->domain)) {
130+
return $subject->toAscii();
127131
}
128132

129-
return $domain->toAscii();
133+
return $subject->toUnicode();
130134
}
131135

132136
/**
@@ -329,13 +333,17 @@ public function isPrivate(): bool
329333
*/
330334
public function toAscii()
331335
{
332-
static $pattern = '/[^\x20-\x7f]/';
333-
if (null === $this->domain || !preg_match($pattern, $this->domain)) {
336+
if (null === $this->domain) {
337+
return $this;
338+
}
339+
340+
$domain = $this->idnToAscii($this->domain);
341+
if ($domain === $this->domain) {
334342
return $this;
335343
}
336344

337345
$clone = clone $this;
338-
$clone->domain = $this->idnToAscii($this->domain);
346+
$clone->domain = $domain;
339347
$clone->labels = array_reverse(explode('.', $clone->domain));
340348
$clone->publicSuffix = $this->publicSuffix->toAscii();
341349
$clone->registrableDomain = $clone->setRegistrableDomain();

src/PublicSuffix.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,12 @@ public function isPrivate(): bool
203203
*/
204204
public function toAscii()
205205
{
206-
static $pattern = '/[^\x20-\x7f]/';
207-
if (null === $this->publicSuffix || !preg_match($pattern, $this->publicSuffix)) {
206+
if (null === $this->publicSuffix) {
207+
return $this;
208+
}
209+
210+
$publicSuffix = $this->idnToAscii($this->publicSuffix);
211+
if ($publicSuffix === $this->publicSuffix) {
208212
return $this;
209213
}
210214

src/Rules.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,7 @@ public function getPublicSuffix($domain = null, string $section = self::ALL_DOMA
111111
$publicSuffix = new PublicSuffix($domain->getLabel(0));
112112
}
113113

114-
static $pattern = '/[^\x20-\x7f]/';
115-
if (preg_match($pattern, $domain->getContent())) {
116-
return $publicSuffix->toUnicode();
117-
}
118-
119-
return $publicSuffix;
114+
return PublicSuffix::createFromDomain($domain->resolve($publicSuffix));
120115
}
121116

122117
/**

tests/DomainTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ public function toAsciiProvider()
347347

348348
/**
349349
* @covers ::resolve
350+
* @covers ::normalize
350351
* @dataProvider resolvePassProvider
351352
*
352353
* @param Domain $domain

tests/RulesTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,10 @@ public function testWithDomainObject()
300300
*/
301301
public function testWithDomainInterfaceObject()
302302
{
303-
$publicSuffix = new PublicSuffix('ac.be', Rules::ICANN_DOMAINS);
304-
$psl = $this->rules->getPublicSuffix($publicSuffix);
305-
$this->assertEquals($publicSuffix, $psl);
303+
$this->assertSame(
304+
'ac.be',
305+
$this->rules->getPublicSuffix(new PublicSuffix('ul.ac.be', Rules::ICANN_DOMAINS))->getContent()
306+
);
306307
}
307308

308309
/**

0 commit comments

Comments
 (0)