Skip to content

Commit ab9fc67

Browse files
committed
Add native types to public API
This changeset adds native types to the public API as discussed in reactphp#219. Once merged, I'm planning to add PHPStan in a follow-up PR which would take advantage of these types. Builds on top of reactphp#222, reactphp#223 and reactphp/cache#60
1 parent 30c5e43 commit ab9fc67

20 files changed

+72
-60
lines changed

src/Config/Config.php

+6-3
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,7 +122,7 @@ 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);
128128
preg_match_all('/(?<=[{;,"])([\da-f.:]{4,})(?=[};,"])/i', $contents, $matches);
@@ -133,5 +133,8 @@ public static function loadWmicBlocking($command = null)
133133
return $config;
134134
}
135135

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

src/Config/HostsFile.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class HostsFile
2727
* @return string
2828
* @codeCoverageIgnore
2929
*/
30-
public static function getDefaultPath()
30+
public static function getDefaultPath(): string
3131
{
3232
// use static path for all Unix-based systems
3333
if (DIRECTORY_SEPARATOR !== '\\') {
@@ -59,7 +59,7 @@ public static function getDefaultPath()
5959
* @return self
6060
* @throws RuntimeException if the path can not be loaded (does not exist)
6161
*/
62-
public static function loadFromPathBlocking($path = null)
62+
public static function loadFromPathBlocking(?string $path = null): self
6363
{
6464
if ($path === null) {
6565
$path = self::getDefaultPath();
@@ -80,7 +80,7 @@ public static function loadFromPathBlocking($path = null)
8080
*
8181
* @param string $contents
8282
*/
83-
public function __construct($contents)
83+
public function __construct(string $contents)
8484
{
8585
// remove all comments from the contents
8686
$contents = preg_replace('/[ \t]*#.*/', '', strtolower($contents));
@@ -92,9 +92,9 @@ public function __construct($contents)
9292
* Returns all IPs for the given hostname
9393
*
9494
* @param string $name
95-
* @return string[]
95+
* @return array<string>
9696
*/
97-
public function getIpsForHost($name)
97+
public function getIpsForHost(string $name): array
9898
{
9999
$name = strtolower($name);
100100

@@ -121,9 +121,9 @@ public function getIpsForHost($name)
121121
* Returns all hostnames for the given IPv4 or IPv6 address
122122
*
123123
* @param string $ip
124-
* @return string[]
124+
* @return array<string>
125125
*/
126-
public function getHostsForIp($ip)
126+
public function getHostsForIp(string $ip): array
127127
{
128128
// check binary representation of IP to avoid string case and short notation
129129
$ip = @inet_pton($ip);

src/Model/Message.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,10 @@ final class Message
8282

8383
/**
8484
* Creates a new request message for the given query
85-
*
8685
* @param Query $query
8786
* @return self
8887
*/
89-
public static function createRequestForQuery(Query $query)
88+
public static function createRequestForQuery(Query $query): self
9089
{
9190
$request = new Message();
9291
$request->id = self::generateId();
@@ -99,11 +98,11 @@ public static function createRequestForQuery(Query $query)
9998
/**
10099
* Creates a new response message for the given query with the given answer records
101100
*
102-
* @param Query $query
103-
* @param Record[] $answers
101+
* @param Query $query
102+
* @param array<Record> $answers
104103
* @return self
105104
*/
106-
public static function createResponseWithAnswersForQuery(Query $query, array $answers)
105+
public static function createResponseWithAnswersForQuery(Query $query, array $answers): self
107106
{
108107
$response = new Message();
109108
$response->id = self::generateId();
@@ -199,22 +198,22 @@ private static function generateId()
199198
* ];
200199
* ```
201200
*
202-
* @var Query[]
201+
* @var array<Query>
203202
*/
204203
public $questions = [];
205204

206205
/**
207-
* @var Record[]
206+
* @var array<Record>
208207
*/
209208
public $answers = [];
210209

211210
/**
212-
* @var Record[]
211+
* @var array<Record>
213212
*/
214213
public $authority = [];
215214

216215
/**
217-
* @var Record[]
216+
* @var array<Record>
218217
*/
219218
public $additional = [];
220219
}

src/Model/Record.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,18 @@ final class Record
131131
* considered a BC break. See the format definition of known types above
132132
* for more details.
133133
*
134-
* @var string|string[]|array
134+
* @var string|array<string>
135135
*/
136136
public $data;
137137

138138
/**
139-
* @param string $name
140-
* @param int $type
141-
* @param int $class
142-
* @param int $ttl
143-
* @param string|string[]|array $data
139+
* @param string $name
140+
* @param int $type
141+
* @param int $class
142+
* @param int $ttl
143+
* @param string|array<string> $data
144144
*/
145-
public function __construct($name, $type, $class, $ttl, $data)
145+
public function __construct(string $name, int $type, int $class, int $ttl, $data)
146146
{
147147
$this->name = $name;
148148
$this->type = $type;

src/Protocol/BinaryDumper.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88

99
final class BinaryDumper
1010
{
11-
/**
12-
* @param Message $message
13-
* @return string
14-
*/
15-
public function toBinary(Message $message)
11+
public function toBinary(Message $message): string
1612
{
1713
$data = '';
1814

src/Protocol/Parser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class Parser
2121
* @throws InvalidArgumentException
2222
* @return Message
2323
*/
24-
public function parseMessage($data)
24+
public function parseMessage(string $data): Message
2525
{
2626
$message = $this->parse($data, 0);
2727
if ($message === null) {

src/Query/CachingExecutor.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use React\Cache\CacheInterface;
66
use React\Dns\Model\Message;
77
use React\Promise\Promise;
8+
use React\Promise\PromiseInterface;
89

910
final class CachingExecutor implements ExecutorInterface
1011
{
@@ -24,7 +25,7 @@ public function __construct(ExecutorInterface $executor, CacheInterface $cache)
2425
$this->cache = $cache;
2526
}
2627

27-
public function query(Query $query)
28+
public function query(Query $query): PromiseInterface
2829
{
2930
$id = $query->name . ':' . $query->type . ':' . $query->class;
3031

src/Query/CoopExecutor.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace React\Dns\Query;
44

55
use React\Promise\Promise;
6+
use React\Promise\PromiseInterface;
67

78
/**
89
* Cooperatively resolves hosts via the given base executor to ensure same query is not run concurrently
@@ -45,7 +46,7 @@ public function __construct(ExecutorInterface $base)
4546
$this->executor = $base;
4647
}
4748

48-
public function query(Query $query)
49+
public function query(Query $query): PromiseInterface
4950
{
5051
$key = $this->serializeQueryToIdentity($query);
5152
if (isset($this->pending[$key])) {

src/Query/ExecutorInterface.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace React\Dns\Query;
44

5+
use React\Promise\PromiseInterface;
6+
57
interface ExecutorInterface
68
{
79
/**
@@ -39,5 +41,5 @@ interface ExecutorInterface
3941
* @return \React\Promise\PromiseInterface<\React\Dns\Model\Message>
4042
* resolves with response message on success or rejects with an Exception on error
4143
*/
42-
public function query(Query $query);
44+
public function query(Query $query): PromiseInterface;
4345
}

src/Query/FallbackExecutor.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace React\Dns\Query;
44

55
use React\Promise\Promise;
6+
use React\Promise\PromiseInterface;
67

78
final class FallbackExecutor implements ExecutorInterface
89
{
@@ -15,7 +16,7 @@ public function __construct(ExecutorInterface $executor, ExecutorInterface $fall
1516
$this->fallback = $fallback;
1617
}
1718

18-
public function query(Query $query)
19+
public function query(Query $query): PromiseInterface
1920
{
2021
$cancelled = false;
2122
$promise = $this->executor->query($query);

src/Query/HostsFileExecutor.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use React\Dns\Config\HostsFile;
66
use React\Dns\Model\Message;
77
use React\Dns\Model\Record;
8+
use React\Promise\PromiseInterface;
89
use function React\Promise\resolve;
910

1011
/**
@@ -25,7 +26,7 @@ public function __construct(HostsFile $hosts, ExecutorInterface $fallback)
2526
$this->fallback = $fallback;
2627
}
2728

28-
public function query(Query $query)
29+
public function query(Query $query): PromiseInterface
2930
{
3031
if ($query->class === Message::CLASS_IN && ($query->type === Message::TYPE_A || $query->type === Message::TYPE_AAAA)) {
3132
// forward lookup for type A or AAAA

src/Query/Query.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class Query
3535
* @param int $type query type, see Message::TYPE_* constants
3636
* @param int $class query class, see Message::CLASS_IN constant
3737
*/
38-
public function __construct($name, $type, $class)
38+
public function __construct(string $name, int $type, int $class)
3939
{
4040
$this->name = $name;
4141
$this->type = $type;
@@ -51,7 +51,7 @@ public function __construct($name, $type, $class)
5151
* @return string "example.com (A)" or "example.com (CLASS0 TYPE1234)"
5252
* @link https://tools.ietf.org/html/rfc3597
5353
*/
54-
public function describe()
54+
public function describe(): string
5555
{
5656
$class = $this->class !== Message::CLASS_IN ? 'CLASS' . $this->class . ' ' : '';
5757

src/Query/RetryExecutor.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ final class RetryExecutor implements ExecutorInterface
1010
private $executor;
1111
private $retries;
1212

13-
public function __construct(ExecutorInterface $executor, $retries = 2)
13+
public function __construct(ExecutorInterface $executor, int $retries = 2)
1414
{
1515
$this->executor = $executor;
1616
$this->retries = $retries;
1717
}
1818

19-
public function query(Query $query)
19+
public function query(Query $query): PromiseInterface
2020
{
2121
return $this->tryQuery($query, $this->retries);
2222
}
2323

24-
public function tryQuery(Query $query, $retries)
24+
public function tryQuery(Query $query, int $retries): PromiseInterface
2525
{
2626
$deferred = new Deferred(function () use (&$promise) {
2727
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {

src/Query/SelectiveTransportExecutor.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace React\Dns\Query;
44

55
use React\Promise\Promise;
6+
use React\Promise\PromiseInterface;
67

78
/**
89
* Send DNS queries over a UDP or TCP/IP stream transport.
@@ -61,7 +62,7 @@ public function __construct(ExecutorInterface $datagramExecutor, ExecutorInterfa
6162
$this->streamExecutor = $streamExecutor;
6263
}
6364

64-
public function query(Query $query)
65+
public function query(Query $query): PromiseInterface
6566
{
6667
$pending = $this->datagramExecutor->query($query);
6768

src/Query/TcpTransportExecutor.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use React\Dns\Protocol\BinaryDumper;
77
use React\Dns\Protocol\Parser;
88
use React\EventLoop\Loop;
9-
use React\EventLoop\LoopInterface;
9+
use React\EventLoop\LoopInterface;;
1010
use React\Promise\Deferred;
11+
use React\Promise\PromiseInterface;
1112
use function React\Promise\reject;
1213

1314
/**
@@ -135,7 +136,7 @@ class TcpTransportExecutor implements ExecutorInterface
135136
* @param string $nameserver
136137
* @param ?LoopInterface $loop
137138
*/
138-
public function __construct($nameserver, ?LoopInterface $loop = null)
139+
public function __construct(string $nameserver, ?LoopInterface $loop = null)
139140
{
140141
if (\strpos($nameserver, '[') === false && \substr_count($nameserver, ':') >= 2 && \strpos($nameserver, '://') === false) {
141142
// several colons, but not enclosed in square brackets => enclose IPv6 address in square brackets
@@ -153,7 +154,7 @@ public function __construct($nameserver, ?LoopInterface $loop = null)
153154
$this->dumper = new BinaryDumper();
154155
}
155156

156-
public function query(Query $query)
157+
public function query(Query $query): PromiseInterface
157158
{
158159
$request = Message::createRequestForQuery($query);
159160

src/Query/TimeoutExecutor.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@
55
use React\EventLoop\Loop;
66
use React\EventLoop\LoopInterface;
77
use React\Promise\Promise;
8+
use React\Promise\PromiseInterface;
89

910
final class TimeoutExecutor implements ExecutorInterface
1011
{
1112
private $executor;
1213
private $loop;
1314
private $timeout;
1415

15-
public function __construct(ExecutorInterface $executor, $timeout, ?LoopInterface $loop = null)
16+
public function __construct(ExecutorInterface $executor, float $timeout, ?LoopInterface $loop = null)
1617
{
1718
$this->executor = $executor;
1819
$this->loop = $loop ?: Loop::get();
1920
$this->timeout = $timeout;
2021
}
2122

22-
public function query(Query $query)
23+
public function query(Query $query): PromiseInterface
2324
{
2425
$promise = $this->executor->query($query);
2526

0 commit comments

Comments
 (0)