Skip to content

Commit 05bd363

Browse files
committed
add change & patch bugs
1 parent 5a35e87 commit 05bd363

Some content is hidden

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

54 files changed

+586
-274
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ jobs:
2525
run: php vendor/bin/phpcs --standard=phpcs.xml
2626
- name: "Run PHP Unit Test"
2727
run: php vendor/bin/phpunit --configuration=phpunit.xml
28+
- name: "Run Phpstan"
29+
run: php vendor/bin/phpstan --configuration=phpstan.neon --memory-limit=256M
2830
continuous-integration-php82:
2931
name: "Continuous Integration php8.2"
3032
runs-on: ubuntu-latest
@@ -45,6 +47,8 @@ jobs:
4547
run: php vendor/bin/phpcs --standard=phpcs.xml
4648
- name: "Run PHP Unit Test"
4749
run: php vendor/bin/phpunit --configuration=phpunit.xml
50+
- name: "Run Phpstan"
51+
run: php vendor/bin/phpstan --configuration=phpstan.neon --memory-limit=256M
4852
coding-standards-php83:
4953
name: "Continuous Integration php8.3"
5054
runs-on: ubuntu-latest
@@ -65,3 +69,5 @@ jobs:
6569
run: php vendor/bin/phpcs --standard=phpcs.xml
6670
- name: "Run PHP Unit Test"
6771
run: php vendor/bin/phpunit --configuration=phpunit.xml
72+
- name: "Run Phpstan"
73+
run: php vendor/bin/phpstan --configuration=phpstan.neon --memory-limit=256M

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ $resolver = new Resolver($dnsServer, $cache);
3535
*/
3636
$useCache = true; // default to true
3737
$timeout = 3.5; // 3.5 seconds
38-
$response = $resolver->lookup('domainname.ext', 'A', 'IN', $timeout, $useCache);
38+
$response = $resolver->lookup('domain-name.ext', 'A', 'IN', $timeout, $useCache);
3939

4040
/**
4141
* Enable Pseudo OPT
4242
*/
4343
$resolver->setDnsSec(true);
44-
$response = $resolver->lookup('domainname.ext', 'A', 'IN');
44+
$response = $resolver->lookup('domain-name.ext', 'A', 'IN');
4545
$answers = $response->getAnswers();
4646
$records = $answers->getRecords();
4747
// Filter "A" Address Only
4848
$arrayA = $records->getFilteredType('A');
4949

5050
```
5151

52-
> IXFR & AXFR not yet implemented
52+
> IXFR & AXFR aren't fully implemented yet
5353
5454

5555
## Note

src/Abstracts/AbstractDnsServer.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function getSecondaryServer(): ?string
6464
public function getName(): string
6565
{
6666
// fallback default class name
67-
return $this->name ??= ltrim(strrchr($this::class, '\\'))?:$this::class;
67+
return $this->name ??= ltrim(strrchr($this::class, '\\')?:$this::class)?:$this::class;
6868
}
6969

7070
/**
@@ -97,6 +97,15 @@ public function unserialize(string $data) : void
9797
$this->__unserialize(unserialize($data));
9898
}
9999

100+
/**
101+
* @return array{
102+
* identity:string,
103+
* name: string,
104+
* primaryServer: string,
105+
* secondaryServer: ?string,
106+
* port: int,
107+
* }
108+
*/
100109
public function __serialize(): array
101110
{
102111
return [
@@ -111,7 +120,13 @@ public function __serialize(): array
111120
/**
112121
* Magic method for unserialize
113122
*
114-
* @param array $data
123+
* @param array{
124+
* identity:string,
125+
* name: string,
126+
* primaryServer: string,
127+
* secondaryServer: ?string,
128+
* port: int,
129+
* } $data
115130
* @return void
116131
*/
117132
public function __unserialize(array $data): void

src/Abstracts/AbstractResourceRecordType.php

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
use ArrayAccess\DnsRecord\Interfaces\ResourceRecord\ResourceRecordMetaTypeInterface;
1111
use ArrayAccess\DnsRecord\Interfaces\ResourceRecord\ResourceRecordQTypeDefinitionInterface;
1212
use ArrayAccess\DnsRecord\Interfaces\ResourceRecord\ResourceRecordTypeInterface;
13-
use ArrayAccess\DnsRecord\ResourceRecord\Definitions\QType;
1413
use ArrayAccess\DnsRecord\Utils\Buffer;
1514
use ArrayAccess\DnsRecord\Utils\Lookup;
16-
use function is_string;
15+
use function is_array;
1716
use function ord;
1817
use function serialize;
1918
use function sprintf;
@@ -47,11 +46,11 @@ abstract class AbstractResourceRecordType implements ResourceRecordTypeInterface
4746
/**
4847
* Response type
4948
*
50-
* @var string|ResourceRecordQTypeDefinitionInterface
49+
* @var ResourceRecordQTypeDefinitionInterface
5150
* @see ResourceRecordTypeInterface::getType()
5251
* @see Lookup::RR_TYPES
5352
*/
54-
protected ResourceRecordQTypeDefinitionInterface|string $type;
53+
protected ResourceRecordQTypeDefinitionInterface $type;
5554

5655
/**
5756
* @var ResourceRecordClassInterface
@@ -112,10 +111,6 @@ public function getOffsetPosition(): int
112111
protected function parseMessage(): void
113112
{
114113
$type = static::TYPE;
115-
if (!isset($this->type) && is_string($type)) {
116-
$this->type = $type;
117-
}
118-
119114
$message = $this->message->getMessage();
120115
$offsetPosition = $this->offsetPosition;
121116
$this->name = Buffer::readLabel($message, $offsetPosition);
@@ -125,28 +120,32 @@ protected function parseMessage(): void
125120
'Response header length is invalid'
126121
);
127122
}
128-
[
129-
'type' => $type,
130-
'class' => $class,
131-
'ttl' => $this->ttl,
132-
'length' => $this->rdLength,
133-
] = unpack("ntype/nclass/Nttl/nlength", $this->header);
123+
$headerArray = unpack("ntype/nclass/Nttl/nlength", $this->header);
124+
$this->rdLength = 0;
125+
if (is_array($headerArray)) {
126+
[
127+
'type' => $type,
128+
'class' => $class,
129+
'ttl' => $this->ttl,
130+
'length' => $this->rdLength,
131+
] = $headerArray;
132+
}
133+
134134
$this->rData = substr($message, $offsetPosition, $this->rdLength);
135135
if (strlen($this->rData) !== $this->rdLength) {
136136
throw new LengthException(
137137
'Rdata & length from response header is mismatch'
138138
);
139139
}
140140

141-
$type = Lookup::resourceType($type);
142-
$class = Lookup::resourceClass($class);
143-
if (isset($this->type)) {
144-
$originType = $this->getType();
145-
if ($originType->getName() !== $type->getName()) {
141+
$type = $type ? Lookup::resourceType($type) : null;
142+
$class = Lookup::resourceClass($class??'');
143+
if (isset($this->type) || !$type) {
144+
if ($this->type->getName() !== $type?->getName()) {
146145
throw new MalformedDataException(
147146
sprintf(
148147
'Response type does not match with current object type. object type: [%s] response type: [%s]',
149-
$originType,
148+
$this->type->getName(),
150149
$type
151150
)
152151
);
@@ -165,6 +164,7 @@ protected function parseMessage(): void
165164
* @param string $message
166165
* @param int $rdataOffset
167166
* @noinspection PhpMissingReturnTypeInspection
167+
* @phpstan-ignore-next-line
168168
*/
169169
protected function parseRData(string $message, int $rdataOffset)
170170
{
@@ -212,11 +212,6 @@ public function getMessage(): PacketMessageInterface
212212
*/
213213
public function getType(): ResourceRecordQTypeDefinitionInterface
214214
{
215-
if (isset($this->type)) {
216-
is_string($this->type) && $this->type = QType::create($this->type);
217-
return $this->type;
218-
}
219-
220215
return $this->type;
221216
}
222217

@@ -294,7 +289,7 @@ public function __serialize(): array
294289
/**
295290
* Magic method for unserialize
296291
*
297-
* @param array $data
292+
* @param array{message: PacketMessageInterface, offsetPosition: int} $data
298293
* @return void
299294
* @throws MalformedDataException
300295
*/

src/Cache/Adapter/FileCacheAdapter.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use function is_dir;
3030
use function is_file;
3131
use function is_int;
32+
use function is_resource;
3233
use function is_string;
3334
use function md5;
3435
use function preg_match;
@@ -38,6 +39,7 @@
3839
use function restore_error_handler;
3940
use function serialize;
4041
use function set_error_handler;
42+
use function sprintf;
4143
use function str_replace;
4244
use function strlen;
4345
use function substr;
@@ -139,9 +141,10 @@ private function doInit(string $namespace, ?string $directory): void
139141
{
140142
$namespace = trim($namespace);
141143
$namespace = preg_replace('~[^a-z0-9_\-.]~i', '', $namespace);
142-
if ($namespace === '') {
144+
if (!$namespace || !is_string($namespace)) {
143145
$namespace = '@';
144146
}
147+
145148
if (!$directory
146149
|| trim($directory) === ''
147150
|| trim(trim($directory), '/\\') === ''
@@ -187,12 +190,17 @@ private static function isOpcacheSupport(): bool
187190
);
188191
}
189192

193+
/**
194+
* @param string $directory
195+
* @param bool $loopDir
196+
* @return Generator<string, string>
197+
*/
190198
private function scanFiles(string $directory, bool $loopDir = false) : Generator
191199
{
192200
if (!is_dir($directory)) {
193201
return '';
194202
}
195-
$directory = rtrim(realpath($directory)??$directory, '\\/');
203+
$directory = rtrim(realpath($directory)?:$directory, '\\/');
196204
$chars = '+-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
197205
$length = strlen($chars);
198206
for ($i = 0; $i < $length; ++$i) {
@@ -235,9 +243,12 @@ public function prune(): bool
235243
}
236244
$value = null;
237245
$valid = $this->validatePartialFile($file);
246+
if (!$valid) {
247+
$this->doUnlink($file);
248+
continue;
249+
}
238250
try {
239-
if ($valid
240-
&& is_array(($expiresAt = include $file))
251+
if (is_array(($expiresAt = include $file))
241252
&& count($expiresAt) === 3
242253
&& is_int($expiresAt[0]??null)
243254
&& is_string($expiresAt[1]??null)
@@ -285,7 +296,7 @@ private function clearLocalCache(): void
285296
}
286297
if (count($this->files) > $maximum) {
287298
while (count($this->files) > $max) {
288-
array_shift(self::$cachesExpires);
299+
array_shift($this->files);
289300
}
290301
}
291302
}
@@ -359,7 +370,14 @@ private function write(string $file, string $data) : bool
359370
$tmp = $this->generateTempFile();
360371
$h = fopen($tmp, 'x');
361372
}
362-
373+
if (!is_resource($h)) {
374+
throw new Exception(
375+
sprintf(
376+
'Canot create resource from file : %s',
377+
$tmp
378+
)
379+
);
380+
}
363381
fwrite($h, $data);
364382
fclose($h);
365383
$unlink = true;
@@ -426,7 +444,7 @@ private function doDelete(string $id, string ...$ids) : bool
426444
return $this->doIdDelete(...$arrayId);
427445
}
428446

429-
private function doUnlink(string $file)
447+
private function doUnlink(string $file) : bool
430448
{
431449
if (self::isOpcacheSupport()) {
432450
Caller::call('opcache_invalidate', $file, true);
@@ -508,7 +526,7 @@ private function doFetch(string $key): ?CacheDataInterface
508526
$unlink = true;
509527
return null;
510528
} finally {
511-
if ($unlink && file_exists($file)) {
529+
if ($unlink) {
512530
self::$cachesExpires[$file] = 0;
513531
unset($this->values[$key]);
514532
$this->doUnlink($file);
@@ -559,7 +577,7 @@ public function getItem(string $key): CacheDataInterface
559577
if (isset($this->values[$key])) {
560578
return $this->values[$key];
561579
}
562-
return $this->doFetch($key)??(new CacheData($key))->expiresAt(
580+
return $this->doFetch($key)??(new CacheData($key))->expiresAfter(
563581
$this->defaultLifetime === 0
564582
? null
565583
: $this->defaultLifetime

src/Cache/Adapter/Psr6CacheAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function saveItem(CacheDataInterface $cacheData): bool
6060
$item
6161
->set($cacheData)
6262
->expiresAfter($cacheData->getExpiresAfter());
63-
return $this->getCacheItemPool()->save($item);
63+
return $this->getCacheItemPool()?->save($item)??false;
6464
}
6565

6666
/**
@@ -95,7 +95,7 @@ public function deleteItems(string ...$keys): bool
9595
public function getItem(string $key): CacheDataInterface
9696
{
9797
$cacheItem = $this->getCacheItem($key);
98-
$cacheItem = $cacheItem->get();
98+
$cacheItem = $cacheItem?->get();
9999
if ($cacheItem instanceof CacheDataInterface) {
100100
return $cacheItem;
101101
}

src/Cache/CacheData.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33

44
namespace ArrayAccess\DnsRecord\Cache;
55

6-
use ArrayAccess\DnsRecord\Exceptions\CacheException;
76
use ArrayAccess\DnsRecord\Interfaces\Cache\CacheDataInterface;
87
use DateInterval;
98
use DateTimeInterface;
10-
use function is_array;
11-
use function is_string;
129
use function serialize;
1310
use function unserialize;
1411

@@ -121,23 +118,20 @@ public function serialize(): string
121118

122119
/**
123120
* @inheritdoc
124-
* @throws CacheException
125121
*/
126122
public function unserialize(string $data): void
127123
{
128-
$data = unserialize($data);
129-
if (!is_array($data) || !is_string($data['key']??null)) {
130-
throw new CacheException(
131-
'Invalid serialized data'
132-
);
133-
}
134-
$this->__unserialize($data);
124+
$this->__unserialize(unserialize($data));
135125
}
136126

137127
/**
138128
* Magic method for unserialize
139129
*
140-
* @param array $data
130+
* @param array{
131+
* key: string,
132+
* data: mixed,
133+
* ttl: ?int
134+
* } $data
141135
* @return void
142136
*/
143137
public function __unserialize(array $data): void
@@ -150,7 +144,7 @@ public function __unserialize(array $data): void
150144
/**
151145
* Magic method for serializing
152146
*
153-
* @return array{key: string, data: mixed, ttl:int}
147+
* @return array{key: string, data: mixed, ttl: ?int}
154148
*/
155149
public function __serialize(): array
156150
{

src/DnsServer/CustomDnsServer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* User defined dns server
1212
*/
13-
class CustomDnsServer extends AbstractDnsServer
13+
final class CustomDnsServer extends AbstractDnsServer
1414
{
1515
use DisableSetterTrait;
1616

0 commit comments

Comments
 (0)