Skip to content

Commit 42ff85f

Browse files
committed
Update PHP language syntax and remove legacy workarounds
1 parent 66c2fa5 commit 42ff85f

23 files changed

+230
-255
lines changed

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) {

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

+7-17
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,13 @@ public static function createResponseWithAnswersForQuery(Query $query, array $an
126126
* DNS response messages can not guess the message ID to avoid possible
127127
* cache poisoning attacks.
128128
*
129-
* The `random_int()` function is only available on PHP 7+ or when
130-
* https://github.com/paragonie/random_compat is installed. As such, using
131-
* the latest supported PHP version is highly recommended. This currently
132-
* falls back to a less secure random number generator on older PHP versions
133-
* in the hope that this system is properly protected against outside
134-
* attackers, for example by using one of the common local DNS proxy stubs.
135-
*
136129
* @return int
137130
* @see self::getId()
138131
* @codeCoverageIgnore
139132
*/
140133
private static function generateId()
141134
{
142-
if (function_exists('random_int')) {
143-
return random_int(0, 0xffff);
144-
}
145-
return mt_rand(0, 0xffff);
135+
return random_int(0, 0xffff);
146136
}
147137

148138
/**
@@ -200,31 +190,31 @@ private static function generateId()
200190
* An array of Query objects
201191
*
202192
* ```php
203-
* $questions = array(
193+
* $questions = [
204194
* new Query(
205195
* 'reactphp.org',
206196
* Message::TYPE_A,
207197
* Message::CLASS_IN
208198
* )
209-
* );
199+
* ];
210200
* ```
211201
*
212202
* @var Query[]
213203
*/
214-
public $questions = array();
204+
public $questions = [];
215205

216206
/**
217207
* @var Record[]
218208
*/
219-
public $answers = array();
209+
public $answers = [];
220210

221211
/**
222212
* @var Record[]
223213
*/
224-
public $authority = array();
214+
public $authority = [];
225215

226216
/**
227217
* @var Record[]
228218
*/
229-
public $additional = array();
219+
public $additional = [];
230220
}

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,23 @@ public function query(Query $query)
2828
{
2929
$id = $query->name . ':' . $query->type . ':' . $query->class;
3030
$cache = $this->cache;
31-
$that = $this;
3231
$executor = $this->executor;
3332

3433
$pending = $cache->get($id);
35-
return new Promise(function ($resolve, $reject) use ($query, $id, $cache, $executor, &$pending, $that) {
34+
return new Promise(function ($resolve, $reject) use ($query, $id, $cache, $executor, &$pending) {
3635
$pending->then(
37-
function ($message) use ($query, $id, $cache, $executor, &$pending, $that) {
36+
function ($message) use ($query, $id, $cache, $executor, &$pending) {
3837
// return cached response message on cache hit
3938
if ($message !== null) {
4039
return $message;
4140
}
4241

4342
// perform DNS lookup if not already cached
4443
return $pending = $executor->query($query)->then(
45-
function (Message $message) use ($cache, $id, $that) {
44+
function (Message $message) use ($cache, $id) {
4645
// DNS response message received => store in cache when not truncated and return
4746
if (!$message->tc) {
48-
$cache->set($id, $message, $that->ttl($message));
47+
$cache->set($id, $message, $this->ttl($message));
4948
}
5049

5150
return $message;

src/Query/CoopExecutor.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
final class CoopExecutor implements ExecutorInterface
3838
{
3939
private $executor;
40-
private $pending = array();
41-
private $counts = array();
40+
private $pending = [];
41+
private $counts = [];
4242

4343
public function __construct(ExecutorInterface $base)
4444
{

src/Query/HostsFileExecutor.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function query(Query $query)
2929
{
3030
if ($query->class === Message::CLASS_IN && ($query->type === Message::TYPE_A || $query->type === Message::TYPE_AAAA)) {
3131
// forward lookup for type A or AAAA
32-
$records = array();
32+
$records = [];
3333
$expectsColon = $query->type === Message::TYPE_AAAA;
3434
foreach ($this->hosts->getIpsForHost($query->name) as $ip) {
3535
// ensure this is an IPv4/IPV6 address according to query type
@@ -48,7 +48,7 @@ public function query(Query $query)
4848
$ip = $this->getIpFromHost($query->name);
4949

5050
if ($ip !== null) {
51-
$records = array();
51+
$records = [];
5252
foreach ($this->hosts->getHostsForIp($ip) as $host) {
5353
$records[] = new Record($query->name, $query->type, $query->class, 0, $host);
5454
}

0 commit comments

Comments
 (0)