Skip to content

Commit 31fcfbf

Browse files
author
chthomas
committed
Code cleanup
1 parent 83c0b1a commit 31fcfbf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+367
-182
lines changed

bootstrap/repl.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,37 @@
55
use RemotelyLiving\PHPDNS\Entities\DNSRecord;
66
use RemotelyLiving\PHPDNS\Entities\DNSRecordType;
77
use RemotelyLiving\PHPDNS\Entities\DNSRecordCollection;
8+
use RemotelyLiving\PHPDNS\Factories\SpatieDNS;
9+
use RemotelyLiving\PHPDNS\Mappers\Dig;
10+
use RemotelyLiving\PHPDNS\Observability\Subscribers\STDIOSubscriber;
11+
use RemotelyLiving\PHPDNS\Resolvers\Cached;
12+
use RemotelyLiving\PHPDNS\Resolvers\Chain;
13+
use RemotelyLiving\PHPDNS\Resolvers\CloudFlare;
14+
use RemotelyLiving\PHPDNS\Resolvers\GoogleDNS;
15+
use RemotelyLiving\PHPDNS\Resolvers\LocalSystem;
16+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
817

918
class_alias(Hostname::class, 'Hostname');
1019
class_alias(DNSRecord::class, 'DNSRecord');
1120
class_alias(DNSRecordType::class, 'DNSRecordType');
1221
class_alias(DNSRecordCollection::class, 'DNSRecordCollection');
1322

14-
$stdOut = new \SplFileObject('php://stdout');
15-
$stdErr = new \SplFileObject('php://stderr');
16-
$IOSubscriber = new \RemotelyLiving\PHPDNS\Observability\Subscribers\STDIOSubscriber($stdOut, $stdErr);
23+
$stdOut = new SplFileObject('php://stdout');
24+
$stdErr = new SplFileObject('php://stderr');
25+
$IOSubscriber = new STDIOSubscriber($stdOut, $stdErr);
1726

18-
$localSystemResolver = new \RemotelyLiving\PHPDNS\Resolvers\LocalSystem();
27+
$localSystemResolver = new LocalSystem();
1928
$localSystemResolver->addSubscriber($IOSubscriber);
2029

21-
$googleDNSResolver = new \RemotelyLiving\PHPDNS\Resolvers\GoogleDNS();
30+
$googleDNSResolver = new GoogleDNS();
2231
$googleDNSResolver->addSubscriber($IOSubscriber);
2332

24-
$cloudFlareResolver = new \RemotelyLiving\PHPDNS\Resolvers\CloudFlare();
33+
$cloudFlareResolver = new CloudFlare();
2534
$cloudFlareResolver->addSubscriber($IOSubscriber);
2635

27-
$digResolver = new \RemotelyLiving\PHPDNS\Resolvers\Dig(new \RemotelyLiving\PHPDNS\Factories\SpatieDNS(), new \RemotelyLiving\PHPDNS\Mappers\Dig());
36+
$digResolver = new \RemotelyLiving\PHPDNS\Resolvers\Dig(new SpatieDNS(), new Dig());
2837
$digResolver->addSubscriber($IOSubscriber);
2938

30-
$chainResolver = new \RemotelyLiving\PHPDNS\Resolvers\Chain($cloudFlareResolver, $googleDNSResolver, $localSystemResolver);
31-
$cachedResolver = new \RemotelyLiving\PHPDNS\Resolvers\Cached(new \Symfony\Component\Cache\Adapter\FilesystemAdapter(), $chainResolver);
39+
$chainResolver = new Chain($cloudFlareResolver, $googleDNSResolver, $localSystemResolver);
40+
$cachedResolver = new Cached(new FilesystemAdapter(), $chainResolver);
3241
$cachedResolver->addSubscriber($IOSubscriber);

src/Entities/CAAData.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
use RemotelyLiving\PHPDNS\Exceptions;
66

7+
use function preg_match;
8+
use function serialize;
9+
use function str_ireplace;
10+
use function trim;
11+
use function unserialize;
12+
713
final class CAAData extends DataAbstract
814
{
915
private int $flags;
@@ -52,25 +58,25 @@ public function toArray(): array
5258

5359
public function serialize(): string
5460
{
55-
return \serialize($this->toArray());
61+
return serialize($this->toArray());
5662
}
5763

5864
/**
5965
* @param string $serialized
6066
*/
6167
public function unserialize($serialized): void
6268
{
63-
$unserialized = \unserialize($serialized);
69+
$unserialized = unserialize($serialized);
6470
$this->flags = $unserialized['flags'];
6571
$this->tag = $unserialized['tag'];
6672
$this->value = $unserialized['value'];
6773
}
6874

6975
private function normalizeValue(string $value): string
7076
{
71-
$normalized = \trim(\str_ireplace('"', '', $value));
77+
$normalized = trim(str_ireplace('"', '', $value));
7278

73-
if (\preg_match('/\s/m', $normalized)) {
79+
if (preg_match('/\s/m', $normalized)) {
7480
throw new Exceptions\InvalidArgumentException("$value is not a valid CAA value");
7581
}
7682

src/Entities/CNAMEData.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace RemotelyLiving\PHPDNS\Entities;
44

5+
use function serialize;
6+
use function unserialize;
7+
58
final class CNAMEData extends DataAbstract
69
{
7-
private \RemotelyLiving\PHPDNS\Entities\Hostname $hostname;
10+
private Hostname $hostname;
811

912
public function __construct(Hostname $hostname)
1013
{
@@ -30,15 +33,15 @@ public function toArray(): array
3033

3134
public function serialize(): string
3235
{
33-
return \serialize($this->toArray());
36+
return serialize($this->toArray());
3437
}
3538

3639
/**
3740
* @param string $serialized
3841
*/
3942
public function unserialize($serialized): void
4043
{
41-
$unserialized = \unserialize($serialized);
44+
$unserialized = unserialize($serialized);
4245
$this->hostname = new Hostname($unserialized['hostname']);
4346
}
4447
}

src/Entities/DNSRecord.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44

55
use RemotelyLiving\PHPDNS\Entities\Interfaces\DNSRecordInterface;
66

7+
use function serialize;
8+
use function unserialize;
9+
710
final class DNSRecord extends EntityAbstract implements DNSRecordInterface
811
{
9-
private \RemotelyLiving\PHPDNS\Entities\DNSRecordType $recordType;
12+
private DNSRecordType $recordType;
1013

11-
private \RemotelyLiving\PHPDNS\Entities\Hostname $hostname;
14+
private Hostname $hostname;
1215

1316
private int $TTL;
1417

15-
private ?\RemotelyLiving\PHPDNS\Entities\IPAddress $IPAddress;
18+
private ?IPAddress $IPAddress;
1619

1720
private string $class;
1821

19-
private ?\RemotelyLiving\PHPDNS\Entities\DataAbstract $data;
22+
private ?DataAbstract $data;
2023
/**
2124
* @var string
2225
*/
@@ -128,15 +131,15 @@ public function equals(DNSRecordInterface $record): bool
128131

129132
public function serialize(): string
130133
{
131-
return \serialize($this->toArray());
134+
return serialize($this->toArray());
132135
}
133136

134137
/**
135138
* @param string $serialized
136139
*/
137140
public function unserialize($serialized): void
138141
{
139-
$unserialized = \unserialize($serialized);
142+
$unserialized = unserialize($serialized);
140143

141144
$rawIPAddres = $unserialized['IPAddress'] ?? null;
142145
$this->recordType = DNSRecordType::createFromString($unserialized['type']);

src/Entities/DNSRecordCollection.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,32 @@
22

33
namespace RemotelyLiving\PHPDNS\Entities;
44

5+
use ArrayAccess;
6+
use ArrayIterator;
7+
use Countable;
8+
use Iterator;
59
use RemotelyLiving\PHPDNS\Entities\Interfaces\Arrayable;
610
use RemotelyLiving\PHPDNS\Entities\Interfaces\DNSRecordInterface;
711
use RemotelyLiving\PHPDNS\Entities\Interfaces\Serializable;
812
use RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException;
913

14+
use function array_filter;
15+
use function array_shift;
16+
use function serialize;
17+
use function unserialize;
18+
1019
final class DNSRecordCollection extends EntityAbstract implements
11-
\ArrayAccess,
12-
\Iterator,
13-
\Countable,
20+
ArrayAccess,
21+
Iterator,
22+
Countable,
1423
Arrayable,
1524
Serializable
1625
{
17-
private \ArrayIterator $records;
26+
private ArrayIterator $records;
1827

1928
public function __construct(DNSRecordInterface ...$records)
2029
{
21-
$this->records = new \ArrayIterator($records);
30+
$this->records = new ArrayIterator($records);
2231
}
2332

2433
public function toArray(): array
@@ -30,13 +39,13 @@ public function pickFirst(): ?DNSRecordInterface
3039
{
3140
$copy = $this->records->getArrayCopy();
3241

33-
return \array_shift($copy);
42+
return array_shift($copy);
3443
}
3544

3645
public function filteredByType(DNSRecordType $type): self
3746
{
3847
return new self(
39-
...\array_filter($this->records->getArrayCopy(), fn(DNSRecord $record) => $record->getType()->equals($type))
48+
...array_filter($this->records->getArrayCopy(), fn(DNSRecord $record) => $record->getType()->equals($type))
4049
);
4150
}
4251

@@ -118,15 +127,15 @@ public function isEmpty(): bool
118127

119128
public function serialize(): string
120129
{
121-
return \serialize($this->records->getArrayCopy());
130+
return serialize($this->records->getArrayCopy());
122131
}
123132

124133
/**
125134
* @param string $serialized
126135
*/
127136
public function unserialize($serialized): void
128137
{
129-
$this->records = new \ArrayIterator(\unserialize($serialized));
138+
$this->records = new ArrayIterator(unserialize($serialized));
130139
}
131140

132141
public function jsonSerialize(): array
@@ -158,7 +167,7 @@ private function filterValues(callable $eval): self
158167
$records = $this->records->getArrayCopy();
159168

160169
/** @var \RemotelyLiving\PHPDNS\Entities\Interfaces\DNSRecordInterface $record */
161-
while ($record = \array_shift($records)) {
170+
while ($record = array_shift($records)) {
162171
if ($eval($record, new self(...$records))) {
163172
$filtered[] = $record;
164173
}

src/Entities/DNSRecordType.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException;
66

7+
use function array_flip;
8+
use function in_array;
9+
use function strtoupper;
10+
711
final class DNSRecordType extends EntityAbstract
812
{
913
public const TYPE_A = 'A';
@@ -62,7 +66,7 @@ final class DNSRecordType extends EntityAbstract
6266
*/
6367
public function __construct(string $type)
6468
{
65-
if (!\in_array($type, self::VALID_TYPES, true)) {
69+
if (!in_array($type, self::VALID_TYPES, true)) {
6670
throw new InvalidArgumentException("{$type} is not an existing DNS record type");
6771
}
6872

@@ -93,12 +97,12 @@ public static function createFromString(string $type): DNSRecordType
9397

9498
public function toInt(): int
9599
{
96-
return (int) \array_flip(self::CODE_TYPE_MAP)[$this->type];
100+
return (int) array_flip(self::CODE_TYPE_MAP)[$this->type];
97101
}
98102

99103
public function isA(string $type): bool
100104
{
101-
return $this->type === \strtoupper($type);
105+
return $this->type === strtoupper($type);
102106
}
103107

104108
public function equals(DNSRecordType $recordType): bool

src/Entities/DataAbstract.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
use RemotelyLiving\PHPDNS\Entities\Interfaces\Serializable;
77
use RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException;
88

9+
use function count;
10+
use function explode;
11+
use function trim;
12+
913
abstract class DataAbstract implements Arrayable, Serializable
1014
{
1115
abstract public function __toString(): string;
@@ -23,7 +27,7 @@ public function equals(DataAbstract $dataAbstract): bool
2327
public static function createFromTypeAndString(DNSRecordType $recordType, string $data): self
2428
{
2529
if ($recordType->isA(DNSRecordType::TYPE_TXT)) {
26-
return new TXTData(\trim($data, '"'));
30+
return new TXTData(trim($data, '"'));
2731
}
2832

2933
if ($recordType->isA(DNSRecordType::TYPE_NS)) {
@@ -52,7 +56,7 @@ public static function createFromTypeAndString(DNSRecordType $recordType, string
5256
);
5357
}
5458

55-
if ($recordType->isA(DNSRecordType::TYPE_CAA) && \count($parsed) === 3) {
59+
if ($recordType->isA(DNSRecordType::TYPE_CAA) && count($parsed) === 3) {
5660
return new CAAData((int)$parsed[0], (string)$parsed[1], $parsed[2]);
5761
}
5862

@@ -75,6 +79,6 @@ public function jsonSerialize(): array
7579

7680
private static function parseDataToArray(string $data): array
7781
{
78-
return \explode(' ', $data);
82+
return explode(' ', $data);
7983
}
8084
}

src/Entities/Hostname.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
use RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException;
66

7+
use function filter_var;
8+
use function idn_to_ascii;
9+
use function idn_to_utf8;
10+
use function mb_strtolower;
11+
use function substr;
12+
use function trim;
13+
714
final class Hostname extends EntityAbstract
815
{
916
private string $hostname;
@@ -15,7 +22,7 @@ public function __construct(string $hostname)
1522
{
1623
$hostname = $this->normalizeHostName($hostname);
1724

18-
if (\filter_var($hostname, FILTER_VALIDATE_DOMAIN) !== $hostname) {
25+
if (filter_var($hostname, FILTER_VALIDATE_DOMAIN) !== $hostname) {
1926
throw new InvalidArgumentException("{$hostname} is not a valid hostname");
2027
}
2128

@@ -44,7 +51,7 @@ public function getHostName(): string
4451

4552
public function getHostnameWithoutTrailingDot(): string
4653
{
47-
return \substr($this->hostname, 0, -1);
54+
return substr($this->hostname, 0, -1);
4855
}
4956

5057
public function isPunycoded(): bool
@@ -54,19 +61,19 @@ public function isPunycoded(): bool
5461

5562
public function toUTF8(): string
5663
{
57-
return (string)\idn_to_utf8($this->hostname, IDNA_ERROR_PUNYCODE, INTL_IDNA_VARIANT_UTS46);
64+
return (string)idn_to_utf8($this->hostname, IDNA_ERROR_PUNYCODE, INTL_IDNA_VARIANT_UTS46);
5865
}
5966

6067
private static function punyCode(string $hostname): string
6168
{
62-
return (string)\idn_to_ascii($hostname, IDNA_ERROR_PUNYCODE, INTL_IDNA_VARIANT_UTS46);
69+
return (string)idn_to_ascii($hostname, IDNA_ERROR_PUNYCODE, INTL_IDNA_VARIANT_UTS46);
6370
}
6471

6572
private function normalizeHostName(string $hostname): string
6673
{
67-
$hostname = self::punyCode(\mb_strtolower(\trim($hostname)));
74+
$hostname = self::punyCode(mb_strtolower(trim($hostname)));
6875

69-
if (\substr($hostname, -1) !== '.') {
76+
if (substr($hostname, -1) !== '.') {
7077
return "{$hostname}.";
7178
}
7279

0 commit comments

Comments
 (0)