Skip to content

Commit

Permalink
Update PHP language syntax and remove legacy workarounds
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonFrings committed May 17, 2024
1 parent a6cc6bb commit e957c17
Show file tree
Hide file tree
Showing 37 changed files with 310 additions and 330 deletions.
2 changes: 1 addition & 1 deletion examples/01-one.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$factory = new Factory();
$resolver = $factory->create($config);

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

$resolver->resolve($name)->then(function ($ip) use ($name) {
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
Expand Down
2 changes: 1 addition & 1 deletion examples/02-concurrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

$names = array_slice($argv, 1);
if (!$names) {
$names = array('google.com', 'www.google.com', 'gmail.com');
$names = ['google.com', 'www.google.com', 'gmail.com'];
}

foreach ($names as $name) {
Expand Down
2 changes: 1 addition & 1 deletion examples/03-cached.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$factory = new Factory();
$resolver = $factory->createCached($config);

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

$resolver->resolve($name)->then(function ($ip) use ($name) {
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
Expand Down
2 changes: 1 addition & 1 deletion examples/11-all-ips.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$factory = new Factory();
$resolver = $factory->create($config);

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

$resolver->resolveAll($name, Message::TYPE_A)->then(function (array $ips) use ($name) {
echo 'IPv4 addresses for ' . $name . ': ' . implode(', ', $ips) . PHP_EOL;
Expand Down
4 changes: 2 additions & 2 deletions examples/12-all-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
$factory = new Factory();
$resolver = $factory->create($config);

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

$resolver->resolveAll($name, $type)->then(function (array $values) {
var_dump($values);
Expand Down
2 changes: 1 addition & 1 deletion examples/13-reverse-dns.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$factory = new Factory();
$resolver = $factory->create($config);

$ip = isset($argv[1]) ? $argv[1] : '8.8.8.8';
$ip = $argv[1] ?? '8.8.8.8';

if (@inet_pton($ip) === false) {
exit('Error: Given argument is not a valid IP' . PHP_EOL);
Expand Down
2 changes: 1 addition & 1 deletion examples/91-query-a-and-aaaa.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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

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

$ipv4Query = new Query($name, Message::TYPE_A, Message::CLASS_IN);
$ipv6Query = new Query($name, Message::TYPE_AAAA, Message::CLASS_IN);
Expand Down
2 changes: 1 addition & 1 deletion examples/92-query-any.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function loadResolvConfBlocking($path = null)
throw new RuntimeException('Unable to load resolv.conf file "' . $path . '"');
}

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

$config = new self();
Expand Down Expand Up @@ -133,5 +133,5 @@ public static function loadWmicBlocking($command = null)
return $config;
}

public $nameservers = array();
public $nameservers = [];
}
6 changes: 3 additions & 3 deletions src/Config/HostsFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function getIpsForHost($name)
{
$name = strtolower($name);

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

$names = array();
$names = [];
foreach (preg_split('/\r?\n/', $this->contents) as $line) {
$parts = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY);
$addr = (string) array_shift($parts);
Expand Down
12 changes: 6 additions & 6 deletions src/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,31 +190,31 @@ private static function generateId()
* An array of Query objects
*
* ```php
* $questions = array(
* $questions = [
* new Query(
* 'reactphp.org',
* Message::TYPE_A,
* Message::CLASS_IN
* )
* );
* ];
* ```
*
* @var Query[]
*/
public $questions = array();
public $questions = [];

/**
* @var Record[]
*/
public $answers = array();
public $answers = [];

/**
* @var Record[]
*/
public $authority = array();
public $authority = [];

/**
* @var Record[]
*/
public $additional = array();
public $additional = [];
}
58 changes: 29 additions & 29 deletions src/Protocol/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,20 @@ private function parseQuestion($data, $consumed)
list($labels, $consumed) = $this->readLabels($data, $consumed);

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

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

return array(
return [
new Query(
implode('.', $labels),
$type,
$class
),
$consumed
);
];
}

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

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

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

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

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

$rdata = array(
$rdata = [
'priority' => $priority,
'target' => $target
);
];
}
} elseif (Message::TYPE_SRV === $type) {
if ($rdLength > 6) {
list($priority, $weight, $port) = array_values(unpack('n*', substr($data, $consumed, 6)));
list($target, $consumed) = $this->readDomain($data, $consumed + 6);

$rdata = array(
$rdata = [
'priority' => $priority,
'weight' => $weight,
'port' => $port,
'target' => $target
);
];
}
} elseif (Message::TYPE_SSHFP === $type) {
if ($rdLength > 2) {
list($algorithm, $hash) = \array_values(\unpack('C*', \substr($data, $consumed, 2)));
$fingerprint = \bin2hex(\substr($data, $consumed + 2, $rdLength - 2));
$consumed += $rdLength;

$rdata = array(
$rdata = [
'algorithm' => $algorithm,
'type' => $hash,
'fingerprint' => $fingerprint
);
];
}
} elseif (Message::TYPE_SOA === $type) {
list($mname, $consumed) = $this->readDomain($data, $consumed);
Expand All @@ -219,18 +219,18 @@ private function parseRecord($data, $consumed)
list($serial, $refresh, $retry, $expire, $minimum) = array_values(unpack('N*', substr($data, $consumed, 20)));
$consumed += 20;

$rdata = array(
$rdata = [
'mname' => $mname,
'rname' => $rname,
'serial' => $serial,
'refresh' => $refresh,
'retry' => $retry,
'expire' => $expire,
'minimum' => $minimum
);
];
}
} elseif (Message::TYPE_OPT === $type) {
$rdata = array();
$rdata = [];
while (isset($data[$consumed + 4 - 1])) {
list($code, $length) = array_values(unpack('n*', substr($data, $consumed, 4)));
$value = (string) substr($data, $consumed + 4, $length);
Expand All @@ -254,11 +254,11 @@ private function parseRecord($data, $consumed)
$value = substr($data, $consumed + 2 + $tagLength, $rdLength - 2 - $tagLength);
$consumed += $rdLength;

$rdata = array(
$rdata = [
'flag' => $flag,
'tag' => $tag,
'value' => $value
);
];
}
}
} else {
Expand All @@ -269,25 +269,25 @@ private function parseRecord($data, $consumed)

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

return array(
return [
new Record($name, $type, $class, $ttl, $rdata),
$consumed
);
];
}

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

if ($labels === null) {
return array(null, null);
return [null, null];
}

// use escaped notation for each label part, then join using dots
return array(
return [
\implode(
'.',
\array_map(
Expand All @@ -298,7 +298,7 @@ function ($label) {
)
),
$consumed
);
];
}

/**
Expand All @@ -309,11 +309,11 @@ function ($label) {
*/
private function readLabels($data, $consumed, $compressionDepth = 127)
{
$labels = array();
$labels = [];

while (true) {
if (!isset($data[$consumed])) {
return array(null, null);
return [null, null];
}

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

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

if ($newLabels === null) {
return array(null, null);
return [null, null];
}

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

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

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

return array($labels, $consumed);
return [$labels, $consumed];
}
}
15 changes: 6 additions & 9 deletions src/Query/CachingExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,22 @@ public function __construct(ExecutorInterface $executor, CacheInterface $cache)
public function query(Query $query)
{
$id = $query->name . ':' . $query->type . ':' . $query->class;
$cache = $this->cache;
$that = $this;
$executor = $this->executor;

$pending = $cache->get($id);
return new Promise(function ($resolve, $reject) use ($query, $id, $cache, $executor, &$pending, $that) {
$pending = $this->cache->get($id);
return new Promise(function ($resolve, $reject) use ($query, $id, &$pending) {
$pending->then(
function ($message) use ($query, $id, $cache, $executor, &$pending, $that) {
function ($message) use ($query, $id, &$pending) {
// return cached response message on cache hit
if ($message !== null) {
return $message;
}

// perform DNS lookup if not already cached
return $pending = $executor->query($query)->then(
function (Message $message) use ($cache, $id, $that) {
return $pending = $this->executor->query($query)->then(
function (Message $message) use ($id) {
// DNS response message received => store in cache when not truncated and return
if (!$message->tc) {
$cache->set($id, $message, $that->ttl($message));
$this->cache->set($id, $message, $this->ttl($message));
}

return $message;
Expand Down
Loading

0 comments on commit e957c17

Please sign in to comment.