Skip to content

Commit df5e2c2

Browse files
committed
Improve functions with null domains
1 parent 62a89a4 commit df5e2c2

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/functions.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ function append($host, $subject): Domain
3333
return $subject;
3434
}
3535

36-
static $pattern = '/[^\x20-\x7f]/';
3736
$dContent = $subject->getContent();
37+
if (null === $dContent) {
38+
return $host instanceof DomainInterface ? new Domain($host) : $host;
39+
}
40+
41+
static $pattern = '/[^\x20-\x7f]/';
3842
$host = preg_match($pattern, $dContent) ? $host->toUnicode() : $host->toAscii();
3943
$domain = new Domain($dContent.'.'.$host->getContent());
4044

@@ -64,14 +68,18 @@ function prepend($host, $subject): Domain
6468
}
6569

6670
if (!$host instanceof DomainInterface) {
67-
$host = new PublicSuffix($host);
71+
$host = new Domain($host);
6872
}
6973

7074
if (null === $host->getContent()) {
7175
return $subject;
7276
}
7377

7478
$dContent = $subject->getContent();
79+
if (null === $dContent) {
80+
return $host instanceof DomainInterface ? new Domain($host) : $host;
81+
}
82+
7583
static $pattern = '/[^\x20-\x7f]/';
7684
$host = preg_match($pattern, $dContent) ? $host->toUnicode() : $host->toAscii();
7785

tests/FunctionsTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,22 @@ public function prependProvider()
5252
'isIcann' => false,
5353
'isPrivate' => false,
5454
],
55-
'prepend a null domain' => [
55+
'prepend with a null domain' => [
5656
'domain' => 'example.com',
5757
'host' => null,
5858
'expected' => 'example.com',
5959
'isKnown' => false,
6060
'isIcann' => false,
6161
'isPrivate' => false,
6262
],
63+
'prepend a null domain' => [
64+
'domain' => null,
65+
'host' => 'example.com',
66+
'expected' => 'example.com',
67+
'isKnown' => false,
68+
'isIcann' => false,
69+
'isPrivate' => false,
70+
],
6371
'prepend does not change PSL info (1)' => [
6472
'domain' => new Domain('example.com', new PublicSuffix('com', Rules::ICANN_DOMAINS)),
6573
'host' => new Domain(),
@@ -178,7 +186,15 @@ public function appendProvider()
178186
'isIcann' => false,
179187
'isPrivate' => false,
180188
],
181-
'adding a null public suffix returns the domain only' => [
189+
'append a null domain' => [
190+
'domain' => null,
191+
'host' => 'example.com',
192+
'expected' => 'example.com',
193+
'isKnown' => false,
194+
'isIcann' => false,
195+
'isPrivate' => false,
196+
],
197+
'append with a null domain' => [
182198
'domain' => 'example.com',
183199
'host' => null,
184200
'expected' => 'example.com',

0 commit comments

Comments
 (0)