Skip to content

Commit e957c17

Browse files
committed
Update PHP language syntax and remove legacy workarounds
1 parent a6cc6bb commit e957c17

37 files changed

+310
-330
lines changed

examples/01-one.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
$factory = new Factory();
1414
$resolver = $factory->create($config);
1515

16-
$name = isset($argv[1]) ? $argv[1] : 'www.google.com';
16+
$name = $argv[1] ?? 'www.google.com';
1717

1818
$resolver->resolve($name)->then(function ($ip) use ($name) {
1919
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;

examples/02-concurrent.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
$names = array_slice($argv, 1);
1717
if (!$names) {
18-
$names = array('google.com', 'www.google.com', 'gmail.com');
18+
$names = ['google.com', 'www.google.com', 'gmail.com'];
1919
}
2020

2121
foreach ($names as $name) {

examples/03-cached.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
$factory = new Factory();
1515
$resolver = $factory->createCached($config);
1616

17-
$name = isset($argv[1]) ? $argv[1] : 'www.google.com';
17+
$name = $argv[1] ?? 'www.google.com';
1818

1919
$resolver->resolve($name)->then(function ($ip) use ($name) {
2020
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;

examples/11-all-ips.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
$factory = new Factory();
1515
$resolver = $factory->create($config);
1616

17-
$name = isset($argv[1]) ? $argv[1] : 'www.google.com';
17+
$name = $argv[1] ?? 'www.google.com';
1818

1919
$resolver->resolveAll($name, Message::TYPE_A)->then(function (array $ips) use ($name) {
2020
echo 'IPv4 addresses for ' . $name . ': ' . implode(', ', $ips) . PHP_EOL;

examples/12-all-types.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
$factory = new Factory();
1717
$resolver = $factory->create($config);
1818

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

2222
$resolver->resolveAll($name, $type)->then(function (array $values) {
2323
var_dump($values);

examples/13-reverse-dns.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
$factory = new Factory();
1515
$resolver = $factory->create($config);
1616

17-
$ip = isset($argv[1]) ? $argv[1] : '8.8.8.8';
17+
$ip = $argv[1] ?? '8.8.8.8';
1818

1919
if (@inet_pton($ip) === false) {
2020
exit('Error: Given argument is not a valid IP' . PHP_EOL);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
$executor = new UdpTransportExecutor('8.8.8.8:53');
1111

12-
$name = isset($argv[1]) ? $argv[1] : 'www.google.com';
12+
$name = $argv[1] ?? 'www.google.com';
1313

1414
$ipv4Query = new Query($name, Message::TYPE_A, Message::CLASS_IN);
1515
$ipv6Query = new Query($name, Message::TYPE_AAAA, Message::CLASS_IN);

examples/92-query-any.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
$executor = new TcpTransportExecutor('8.8.8.8:53');
1515

16-
$name = isset($argv[1]) ? $argv[1] : 'google.com';
16+
$name = $argv[1] ?? 'google.com';
1717

1818
$any = new Query($name, Message::TYPE_ANY, Message::CLASS_IN);
1919

src/Config/Config.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static function loadResolvConfBlocking($path = null)
8282
throw new RuntimeException('Unable to load resolv.conf file "' . $path . '"');
8383
}
8484

85-
$matches = array();
85+
$matches = [];
8686
preg_match_all('/^nameserver\s+(\S+)\s*$/m', $contents, $matches);
8787

8888
$config = new self();
@@ -133,5 +133,5 @@ public static function loadWmicBlocking($command = null)
133133
return $config;
134134
}
135135

136-
public $nameservers = array();
136+
public $nameservers = [];
137137
}

src/Config/HostsFile.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function getIpsForHost($name)
9898
{
9999
$name = strtolower($name);
100100

101-
$ips = array();
101+
$ips = [];
102102
foreach (preg_split('/\r?\n/', $this->contents) as $line) {
103103
$parts = preg_split('/\s+/', $line);
104104
$ip = array_shift($parts);
@@ -128,10 +128,10 @@ public function getHostsForIp($ip)
128128
// check binary representation of IP to avoid string case and short notation
129129
$ip = @inet_pton($ip);
130130
if ($ip === false) {
131-
return array();
131+
return [];
132132
}
133133

134-
$names = array();
134+
$names = [];
135135
foreach (preg_split('/\r?\n/', $this->contents) as $line) {
136136
$parts = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY);
137137
$addr = (string) array_shift($parts);

src/Model/Message.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -190,31 +190,31 @@ private static function generateId()
190190
* An array of Query objects
191191
*
192192
* ```php
193-
* $questions = array(
193+
* $questions = [
194194
* new Query(
195195
* 'reactphp.org',
196196
* Message::TYPE_A,
197197
* Message::CLASS_IN
198198
* )
199-
* );
199+
* ];
200200
* ```
201201
*
202202
* @var Query[]
203203
*/
204-
public $questions = array();
204+
public $questions = [];
205205

206206
/**
207207
* @var Record[]
208208
*/
209-
public $answers = array();
209+
public $answers = [];
210210

211211
/**
212212
* @var Record[]
213213
*/
214-
public $authority = array();
214+
public $authority = [];
215215

216216
/**
217217
* @var Record[]
218218
*/
219-
public $additional = array();
219+
public $additional = [];
220220
}

src/Protocol/Parser.php

+29-29
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,20 @@ private function parseQuestion($data, $consumed)
108108
list($labels, $consumed) = $this->readLabels($data, $consumed);
109109

110110
if ($labels === null || !isset($data[$consumed + 4 - 1])) {
111-
return array(null, null);
111+
return [null, null];
112112
}
113113

114114
list($type, $class) = array_values(unpack('n*', substr($data, $consumed, 4)));
115115
$consumed += 4;
116116

117-
return array(
117+
return [
118118
new Query(
119119
implode('.', $labels),
120120
$type,
121121
$class
122122
),
123123
$consumed
124-
);
124+
];
125125
}
126126

127127
/**
@@ -134,7 +134,7 @@ private function parseRecord($data, $consumed)
134134
list($name, $consumed) = $this->readDomain($data, $consumed);
135135

136136
if ($name === null || !isset($data[$consumed + 10 - 1])) {
137-
return array(null, null);
137+
return [null, null];
138138
}
139139

140140
list($type, $class) = array_values(unpack('n*', substr($data, $consumed, 4)));
@@ -152,7 +152,7 @@ private function parseRecord($data, $consumed)
152152
$consumed += 2;
153153

154154
if (!isset($data[$consumed + $rdLength - 1])) {
155-
return array(null, null);
155+
return [null, null];
156156
}
157157

158158
$rdata = null;
@@ -171,7 +171,7 @@ private function parseRecord($data, $consumed)
171171
} elseif (Message::TYPE_CNAME === $type || Message::TYPE_PTR === $type || Message::TYPE_NS === $type) {
172172
list($rdata, $consumed) = $this->readDomain($data, $consumed);
173173
} elseif (Message::TYPE_TXT === $type || Message::TYPE_SPF === $type) {
174-
$rdata = array();
174+
$rdata = [];
175175
while ($consumed < $expected) {
176176
$len = ord($data[$consumed]);
177177
$rdata[] = (string)substr($data, $consumed + 1, $len);
@@ -182,34 +182,34 @@ private function parseRecord($data, $consumed)
182182
list($priority) = array_values(unpack('n', substr($data, $consumed, 2)));
183183
list($target, $consumed) = $this->readDomain($data, $consumed + 2);
184184

185-
$rdata = array(
185+
$rdata = [
186186
'priority' => $priority,
187187
'target' => $target
188-
);
188+
];
189189
}
190190
} elseif (Message::TYPE_SRV === $type) {
191191
if ($rdLength > 6) {
192192
list($priority, $weight, $port) = array_values(unpack('n*', substr($data, $consumed, 6)));
193193
list($target, $consumed) = $this->readDomain($data, $consumed + 6);
194194

195-
$rdata = array(
195+
$rdata = [
196196
'priority' => $priority,
197197
'weight' => $weight,
198198
'port' => $port,
199199
'target' => $target
200-
);
200+
];
201201
}
202202
} elseif (Message::TYPE_SSHFP === $type) {
203203
if ($rdLength > 2) {
204204
list($algorithm, $hash) = \array_values(\unpack('C*', \substr($data, $consumed, 2)));
205205
$fingerprint = \bin2hex(\substr($data, $consumed + 2, $rdLength - 2));
206206
$consumed += $rdLength;
207207

208-
$rdata = array(
208+
$rdata = [
209209
'algorithm' => $algorithm,
210210
'type' => $hash,
211211
'fingerprint' => $fingerprint
212-
);
212+
];
213213
}
214214
} elseif (Message::TYPE_SOA === $type) {
215215
list($mname, $consumed) = $this->readDomain($data, $consumed);
@@ -219,18 +219,18 @@ private function parseRecord($data, $consumed)
219219
list($serial, $refresh, $retry, $expire, $minimum) = array_values(unpack('N*', substr($data, $consumed, 20)));
220220
$consumed += 20;
221221

222-
$rdata = array(
222+
$rdata = [
223223
'mname' => $mname,
224224
'rname' => $rname,
225225
'serial' => $serial,
226226
'refresh' => $refresh,
227227
'retry' => $retry,
228228
'expire' => $expire,
229229
'minimum' => $minimum
230-
);
230+
];
231231
}
232232
} elseif (Message::TYPE_OPT === $type) {
233-
$rdata = array();
233+
$rdata = [];
234234
while (isset($data[$consumed + 4 - 1])) {
235235
list($code, $length) = array_values(unpack('n*', substr($data, $consumed, 4)));
236236
$value = (string) substr($data, $consumed + 4, $length);
@@ -254,11 +254,11 @@ private function parseRecord($data, $consumed)
254254
$value = substr($data, $consumed + 2 + $tagLength, $rdLength - 2 - $tagLength);
255255
$consumed += $rdLength;
256256

257-
$rdata = array(
257+
$rdata = [
258258
'flag' => $flag,
259259
'tag' => $tag,
260260
'value' => $value
261-
);
261+
];
262262
}
263263
}
264264
} else {
@@ -269,25 +269,25 @@ private function parseRecord($data, $consumed)
269269

270270
// ensure parsing record data consumes expact number of bytes indicated in record length
271271
if ($consumed !== $expected || $rdata === null) {
272-
return array(null, null);
272+
return [null, null];
273273
}
274274

275-
return array(
275+
return [
276276
new Record($name, $type, $class, $ttl, $rdata),
277277
$consumed
278-
);
278+
];
279279
}
280280

281281
private function readDomain($data, $consumed)
282282
{
283283
list ($labels, $consumed) = $this->readLabels($data, $consumed);
284284

285285
if ($labels === null) {
286-
return array(null, null);
286+
return [null, null];
287287
}
288288

289289
// use escaped notation for each label part, then join using dots
290-
return array(
290+
return [
291291
\implode(
292292
'.',
293293
\array_map(
@@ -298,7 +298,7 @@ function ($label) {
298298
)
299299
),
300300
$consumed
301-
);
301+
];
302302
}
303303

304304
/**
@@ -309,11 +309,11 @@ function ($label) {
309309
*/
310310
private function readLabels($data, $consumed, $compressionDepth = 127)
311311
{
312-
$labels = array();
312+
$labels = [];
313313

314314
while (true) {
315315
if (!isset($data[$consumed])) {
316-
return array(null, null);
316+
return [null, null];
317317
}
318318

319319
$length = \ord($data[$consumed]);
@@ -328,14 +328,14 @@ private function readLabels($data, $consumed, $compressionDepth = 127)
328328
if (($length & 0xc0) === 0xc0 && isset($data[$consumed + 1]) && $compressionDepth) {
329329
$offset = ($length & ~0xc0) << 8 | \ord($data[$consumed + 1]);
330330
if ($offset >= $consumed) {
331-
return array(null, null);
331+
return [null, null];
332332
}
333333

334334
$consumed += 2;
335335
list($newLabels) = $this->readLabels($data, $offset, $compressionDepth - 1);
336336

337337
if ($newLabels === null) {
338-
return array(null, null);
338+
return [null, null];
339339
}
340340

341341
$labels = array_merge($labels, $newLabels);
@@ -344,13 +344,13 @@ private function readLabels($data, $consumed, $compressionDepth = 127)
344344

345345
// length MUST be 0-63 (6 bits only) and data has to be large enough
346346
if ($length & 0xc0 || !isset($data[$consumed + $length - 1])) {
347-
return array(null, null);
347+
return [null, null];
348348
}
349349

350350
$labels[] = substr($data, $consumed + 1, $length);
351351
$consumed += $length + 1;
352352
}
353353

354-
return array($labels, $consumed);
354+
return [$labels, $consumed];
355355
}
356356
}

src/Query/CachingExecutor.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,22 @@ public function __construct(ExecutorInterface $executor, CacheInterface $cache)
2727
public function query(Query $query)
2828
{
2929
$id = $query->name . ':' . $query->type . ':' . $query->class;
30-
$cache = $this->cache;
31-
$that = $this;
32-
$executor = $this->executor;
3330

34-
$pending = $cache->get($id);
35-
return new Promise(function ($resolve, $reject) use ($query, $id, $cache, $executor, &$pending, $that) {
31+
$pending = $this->cache->get($id);
32+
return new Promise(function ($resolve, $reject) use ($query, $id, &$pending) {
3633
$pending->then(
37-
function ($message) use ($query, $id, $cache, $executor, &$pending, $that) {
34+
function ($message) use ($query, $id, &$pending) {
3835
// return cached response message on cache hit
3936
if ($message !== null) {
4037
return $message;
4138
}
4239

4340
// perform DNS lookup if not already cached
44-
return $pending = $executor->query($query)->then(
45-
function (Message $message) use ($cache, $id, $that) {
41+
return $pending = $this->executor->query($query)->then(
42+
function (Message $message) use ($id) {
4643
// DNS response message received => store in cache when not truncated and return
4744
if (!$message->tc) {
48-
$cache->set($id, $message, $that->ttl($message));
45+
$this->cache->set($id, $message, $this->ttl($message));
4946
}
5047

5148
return $message;

0 commit comments

Comments
 (0)