Skip to content

Commit 69cde32

Browse files
committed
WIP COMMIT
1 parent 30c5e43 commit 69cde32

Some content is hidden

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

51 files changed

+1042
-558
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/.github/ export-ignore
33
/.gitignore export-ignore
44
/examples export-ignore
5+
/phpstan.neon.dist export-ignore
56
/phpunit.xml.dist export-ignore
67
/phpunit.xml.legacy export-ignore
78
/tests export-ignore

.github/workflows/ci.yml

+23
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,26 @@ jobs:
4545
ini-file: development
4646
- run: composer install
4747
- run: vendor/bin/phpunit --coverage-text
48+
49+
PHPStan:
50+
name: PHPStan (PHP ${{ matrix.php }})
51+
runs-on: ubuntu-22.04
52+
strategy:
53+
matrix:
54+
php:
55+
- 8.3
56+
- 8.2
57+
- 8.1
58+
- 8.0
59+
- 7.4
60+
- 7.3
61+
- 7.2
62+
- 7.1
63+
steps:
64+
- uses: actions/checkout@v4
65+
- uses: shivammathur/setup-php@v2
66+
with:
67+
php-version: ${{ matrix.php }}
68+
coverage: none
69+
- run: composer install
70+
- run: vendor/bin/phpstan

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"react/promise": "^3.2 || ^2.7 || ^1.2.1"
3333
},
3434
"require-dev": {
35+
"phpstan/phpstan": "1.11.1 || 1.4.10",
3536
"phpunit/phpunit": "^9.6 || ^7.5",
3637
"react/async": "^4.3 || ^3 || ^2",
3738
"react/promise-timer": "^1.11"

examples/01-one.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@
1717

1818
$resolver->resolve($name)->then(function ($ip) use ($name) {
1919
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
20-
}, 'printf');
20+
}, static function (Throwable $error) {
21+
echo $error;
22+
});

examples/02-concurrent.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@
2121
foreach ($names as $name) {
2222
$resolver->resolve($name)->then(function ($ip) use ($name) {
2323
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
24-
}, 'printf');
24+
}, static function (Throwable $error) {
25+
echo $error;
26+
});
2527
}

examples/03-cached.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,30 @@
1818

1919
$resolver->resolve($name)->then(function ($ip) use ($name) {
2020
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
21-
}, 'printf');
21+
}, static function (Throwable $error) {
22+
echo $error;
23+
});
2224

2325
Loop::addTimer(1.0, function() use ($name, $resolver) {
2426
$resolver->resolve($name)->then(function ($ip) use ($name) {
2527
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
26-
}, 'printf');
28+
}, static function (Throwable $error) {
29+
echo $error;
30+
});
2731
});
2832

2933
Loop::addTimer(2.0, function() use ($name, $resolver) {
3034
$resolver->resolve($name)->then(function ($ip) use ($name) {
3135
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
32-
}, 'printf');
36+
}, static function (Throwable $error) {
37+
echo $error;
38+
});
3339
});
3440

3541
Loop::addTimer(3.0, function() use ($name, $resolver) {
3642
$resolver->resolve($name)->then(function ($ip) use ($name) {
3743
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
38-
}, 'printf');
44+
}, static function (Throwable $error) {
45+
echo $error;
46+
});
3947
});

examples/11-all-ips.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
$resolver = $factory->create($config);
1616

1717
$name = $argv[1] ?? 'www.google.com';
18+
assert(is_string($name));
1819

19-
$resolver->resolveAll($name, Message::TYPE_A)->then(function (array $ips) use ($name) {
20+
$resolver->resolveAll($name, Message::TYPE_A)->then(static function (array $ips) use ($name): void {
2021
echo 'IPv4 addresses for ' . $name . ': ' . implode(', ', $ips) . PHP_EOL;
21-
}, function (Exception $e) use ($name) {
22+
}, function (Throwable $e) use ($name) {
2223
echo 'No IPv4 addresses for ' . $name . ': ' . $e->getMessage() . PHP_EOL;
2324
});
2425

25-
$resolver->resolveAll($name, Message::TYPE_AAAA)->then(function (array $ips) use ($name) {
26+
$resolver->resolveAll($name, Message::TYPE_AAAA)->then(static function (array $ips) use ($name): void {
2627
echo 'IPv6 addresses for ' . $name . ': ' . implode(', ', $ips) . PHP_EOL;
27-
}, function (Exception $e) use ($name) {
28+
}, function (Throwable $e) use ($name) {
2829
echo 'No IPv6 addresses for ' . $name . ': ' . $e->getMessage() . PHP_EOL;
2930
});

examples/12-all-types.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
$resolver = $factory->create($config);
1818

1919
$name = $argv[1] ?? 'google.com';
20-
$type = constant('React\Dns\Model\Message::TYPE_' . ($argv[2] ?? 'TXT'));
20+
assert(is_string($name));
2121

22-
$resolver->resolveAll($name, $type)->then(function (array $values) {
22+
$type = constant('React\Dns\Model\Message::TYPE_' . ($type ?? 'TXT'));
23+
assert(is_int($type));
24+
25+
$resolver->resolveAll($name, $type)->then(static function (array $values): void {
2326
var_dump($values);
24-
}, function (Exception $e) {
27+
}, function (Throwable $e) {
2528
echo $e->getMessage() . PHP_EOL;
2629
});

examples/13-reverse-dns.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@
1515
$resolver = $factory->create($config);
1616

1717
$ip = $argv[1] ?? '8.8.8.8';
18+
assert(is_string($ip));
19+
$ip = @inet_pton($ip);
1820

19-
if (@inet_pton($ip) === false) {
21+
if ($ip === false) {
2022
exit('Error: Given argument is not a valid IP' . PHP_EOL);
2123
}
2224

2325
if (strpos($ip, ':') === false) {
24-
$name = inet_ntop(strrev(inet_pton($ip))) . '.in-addr.arpa';
26+
$name = inet_ntop(strrev($ip)) . '.in-addr.arpa';
2527
} else {
26-
$name = wordwrap(strrev(bin2hex(inet_pton($ip))), 1, '.', true) . '.ip6.arpa';
28+
$name = wordwrap(strrev(bin2hex($ip)), 1, '.', true) . '.ip6.arpa';
2729
}
30+
assert(is_string($name));
2831

2932
$resolver->resolveAll($name, Message::TYPE_PTR)->then(function (array $names) {
3033
var_dump($names);
31-
}, function (Exception $e) {
32-
echo $e->getMessage() . PHP_EOL;
34+
}, static function (Throwable $error) {
35+
echo $error;
3336
});

examples/91-query-a-and-aaaa.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,24 @@
1010
$executor = new UdpTransportExecutor('8.8.8.8:53');
1111

1212
$name = $argv[1] ?? 'www.google.com';
13+
assert(is_string($name));
1314

1415
$ipv4Query = new Query($name, Message::TYPE_A, Message::CLASS_IN);
1516
$ipv6Query = new Query($name, Message::TYPE_AAAA, Message::CLASS_IN);
1617

17-
$executor->query($ipv4Query)->then(function (Message $message) {
18+
$executor->query($ipv4Query)->then(static function (Message $message): void {
1819
foreach ($message->answers as $answer) {
20+
assert(\is_string($answer->data));
1921
echo 'IPv4: ' . $answer->data . PHP_EOL;
2022
}
21-
}, 'printf');
22-
$executor->query($ipv6Query)->then(function (Message $message) {
23+
}, static function (Throwable $error) {
24+
echo $error;
25+
});
26+
$executor->query($ipv6Query)->then(static function (Message $message): void {
2327
foreach ($message->answers as $answer) {
28+
assert(\is_string($answer->data));
2429
echo 'IPv6: ' . $answer->data . PHP_EOL;
2530
}
26-
}, 'printf');
31+
}, static function (Throwable $error) {
32+
echo $error;
33+
});

examples/92-query-any.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -14,68 +14,81 @@
1414
$executor = new TcpTransportExecutor('8.8.8.8:53');
1515

1616
$name = $argv[1] ?? 'google.com';
17+
assert(is_string($name));
1718

1819
$any = new Query($name, Message::TYPE_ANY, Message::CLASS_IN);
1920

20-
$executor->query($any)->then(function (Message $message) {
21+
$executor->query($any)->then(function (Message $message): void {
2122
foreach ($message->answers as $answer) {
22-
/* @var $answer Record */
23-
2423
$data = $answer->data;
2524

2625
switch ($answer->type) {
2726
case Message::TYPE_A:
27+
assert(\is_string($data));
2828
$type = 'A';
2929
break;
3030
case Message::TYPE_AAAA:
31+
assert(\is_string($data));
3132
$type = 'AAAA';
3233
break;
3334
case Message::TYPE_NS:
35+
assert(\is_string($data));
3436
$type = 'NS';
3537
break;
3638
case Message::TYPE_PTR:
39+
assert(\is_string($data));
3740
$type = 'PTR';
3841
break;
3942
case Message::TYPE_CNAME:
43+
assert(\is_string($data));
4044
$type = 'CNAME';
4145
break;
4246
case Message::TYPE_TXT:
47+
assert(\is_array($data));
4348
// TXT records can contain a list of (binary) strings for each record.
4449
// here, we assume this is printable ASCII and simply concatenate output
4550
$type = 'TXT';
4651
$data = implode('', $data);
4752
break;
4853
case Message::TYPE_MX:
54+
assert(\is_array($data));
4955
// MX records contain "priority" and "target", only dump its values here
5056
$type = 'MX';
5157
$data = implode(' ', $data);
5258
break;
5359
case Message::TYPE_SRV:
60+
assert(\is_array($data));
5461
// SRV records contain priority, weight, port and target, dump structure here
5562
$type = 'SRV';
5663
$data = json_encode($data);
5764
break;
5865
case Message::TYPE_SSHFP:
66+
assert(\is_array($data));
5967
// SSHFP records contain algorithm, fingerprint type and hex fingerprint value
6068
$type = 'SSHFP';
6169
$data = implode(' ', $data);
6270
break;
6371
case Message::TYPE_SOA:
72+
assert(\is_array($data));
6473
// SOA records contain structured data, dump structure here
6574
$type = 'SOA';
6675
$data = json_encode($data);
6776
break;
6877
case Message::TYPE_CAA:
78+
assert(\is_array($data));
6979
// CAA records contains flag, tag and value
7080
$type = 'CAA';
7181
$data = $data['flag'] . ' ' . $data['tag'] . ' "' . $data['value'] . '"';
7282
break;
7383
default:
84+
assert(\is_string($data));
7485
// unknown type uses HEX format
7586
$type = 'TYPE' . $answer->type;
7687
$data = wordwrap(strtoupper(bin2hex($data)), 2, ' ', true);
7788
}
7889

7990
echo $type . ': ' . $data . PHP_EOL;
8091
}
81-
}, 'printf');
92+
}, static function (Throwable $error) {
93+
echo $error;
94+
});

phpstan.neon.dist

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parameters:
2+
level: max
3+
4+
paths:
5+
- examples/
6+
- src/
7+
- tests/

src/Config/Config.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class Config
2626
* @return self
2727
* @codeCoverageIgnore
2828
*/
29-
public static function loadSystemConfigBlocking()
29+
public static function loadSystemConfigBlocking(): self
3030
{
3131
// Use WMIC output on Windows
3232
if (DIRECTORY_SEPARATOR === '\\') {
@@ -71,7 +71,7 @@ public static function loadSystemConfigBlocking()
7171
* @return self
7272
* @throws RuntimeException if the path can not be loaded (does not exist)
7373
*/
74-
public static function loadResolvConfBlocking($path = null)
74+
public static function loadResolvConfBlocking(?string $path = null): self
7575
{
7676
if ($path === null) {
7777
$path = '/etc/resolv.conf';
@@ -122,16 +122,19 @@ public static function loadResolvConfBlocking($path = null)
122122
* @return self
123123
* @link https://ss64.com/nt/wmic.html
124124
*/
125-
public static function loadWmicBlocking($command = null)
125+
public static function loadWmicBlocking(?string $command = null): self
126126
{
127127
$contents = shell_exec($command === null ? 'wmic NICCONFIG get "DNSServerSearchOrder" /format:CSV' : $command);
128-
preg_match_all('/(?<=[{;,"])([\da-f.:]{4,})(?=[};,"])/i', $contents, $matches);
128+
preg_match_all('/(?<=[{;,"])([\da-f.:]{4,})(?=[};,"])/i', $contents, $matches); /** @phpstan-ignore-line */
129129

130130
$config = new self();
131131
$config->nameservers = $matches[1];
132132

133133
return $config;
134134
}
135135

136+
/**
137+
* @var array<string>
138+
*/
136139
public $nameservers = [];
137140
}

0 commit comments

Comments
 (0)