Skip to content

Commit

Permalink
Add native types to public API
Browse files Browse the repository at this point in the history
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
  • Loading branch information
WyriHaximus committed Jun 14, 2024
1 parent 30c5e43 commit 1e20620
Show file tree
Hide file tree
Showing 20 changed files with 71 additions and 59 deletions.
9 changes: 6 additions & 3 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class Config
* @return self
* @codeCoverageIgnore
*/
public static function loadSystemConfigBlocking()
public static function loadSystemConfigBlocking(): self
{
// Use WMIC output on Windows
if (DIRECTORY_SEPARATOR === '\\') {
Expand Down Expand Up @@ -71,7 +71,7 @@ public static function loadSystemConfigBlocking()
* @return self
* @throws RuntimeException if the path can not be loaded (does not exist)
*/
public static function loadResolvConfBlocking($path = null)
public static function loadResolvConfBlocking(?string $path = null): self
{
if ($path === null) {
$path = '/etc/resolv.conf';
Expand Down Expand Up @@ -122,7 +122,7 @@ public static function loadResolvConfBlocking($path = null)
* @return self
* @link https://ss64.com/nt/wmic.html
*/
public static function loadWmicBlocking($command = null)
public static function loadWmicBlocking(?string $command = null): self
{
$contents = shell_exec($command === null ? 'wmic NICCONFIG get "DNSServerSearchOrder" /format:CSV' : $command);
preg_match_all('/(?<=[{;,"])([\da-f.:]{4,})(?=[};,"])/i', $contents, $matches);
Expand All @@ -133,5 +133,8 @@ public static function loadWmicBlocking($command = null)
return $config;
}

/**
* @var array<string>
*/
public $nameservers = [];
}
14 changes: 7 additions & 7 deletions src/Config/HostsFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class HostsFile
* @return string
* @codeCoverageIgnore
*/
public static function getDefaultPath()
public static function getDefaultPath(): string
{
// use static path for all Unix-based systems
if (DIRECTORY_SEPARATOR !== '\\') {
Expand Down Expand Up @@ -59,7 +59,7 @@ public static function getDefaultPath()
* @return self
* @throws RuntimeException if the path can not be loaded (does not exist)
*/
public static function loadFromPathBlocking($path = null)
public static function loadFromPathBlocking(?string $path = null): self
{
if ($path === null) {
$path = self::getDefaultPath();
Expand All @@ -80,7 +80,7 @@ public static function loadFromPathBlocking($path = null)
*
* @param string $contents
*/
public function __construct($contents)
public function __construct(string $contents)
{
// remove all comments from the contents
$contents = preg_replace('/[ \t]*#.*/', '', strtolower($contents));
Expand All @@ -92,9 +92,9 @@ public function __construct($contents)
* Returns all IPs for the given hostname
*
* @param string $name
* @return string[]
* @return array<string>
*/
public function getIpsForHost($name)
public function getIpsForHost(string $name): array
{
$name = strtolower($name);

Expand All @@ -121,9 +121,9 @@ public function getIpsForHost($name)
* Returns all hostnames for the given IPv4 or IPv6 address
*
* @param string $ip
* @return string[]
* @return array<string>
*/
public function getHostsForIp($ip)
public function getHostsForIp(string $ip): array
{
// check binary representation of IP to avoid string case and short notation
$ip = @inet_pton($ip);
Expand Down
17 changes: 8 additions & 9 deletions src/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ final class Message

/**
* Creates a new request message for the given query
*
* @param Query $query
* @return self
*/
public static function createRequestForQuery(Query $query)
public static function createRequestForQuery(Query $query): self
{
$request = new Message();
$request->id = self::generateId();
Expand All @@ -99,11 +98,11 @@ public static function createRequestForQuery(Query $query)
/**
* Creates a new response message for the given query with the given answer records
*
* @param Query $query
* @param Record[] $answers
* @param Query $query
* @param array<Record> $answers
* @return self
*/
public static function createResponseWithAnswersForQuery(Query $query, array $answers)
public static function createResponseWithAnswersForQuery(Query $query, array $answers): self
{
$response = new Message();
$response->id = self::generateId();
Expand Down Expand Up @@ -199,22 +198,22 @@ private static function generateId()
* ];
* ```
*
* @var Query[]
* @var array<Query>
*/
public $questions = [];

/**
* @var Record[]
* @var array<Record>
*/
public $answers = [];

/**
* @var Record[]
* @var array<Record>
*/
public $authority = [];

/**
* @var Record[]
* @var array<Record>
*/
public $additional = [];
}
14 changes: 7 additions & 7 deletions src/Model/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@ final class Record
* considered a BC break. See the format definition of known types above
* for more details.
*
* @var string|string[]|array
* @var string|array<string>
*/
public $data;

/**
* @param string $name
* @param int $type
* @param int $class
* @param int $ttl
* @param string|string[]|array $data
* @param string $name
* @param int $type
* @param int $class
* @param int $ttl
* @param string|array<string> $data
*/
public function __construct($name, $type, $class, $ttl, $data)
public function __construct(string $name, int $type, int $class, int $ttl, $data)
{
$this->name = $name;
$this->type = $type;
Expand Down
6 changes: 1 addition & 5 deletions src/Protocol/BinaryDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

final class BinaryDumper
{
/**
* @param Message $message
* @return string
*/
public function toBinary(Message $message)
public function toBinary(Message $message): string
{
$data = '';

Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class Parser
* @throws InvalidArgumentException
* @return Message
*/
public function parseMessage($data)
public function parseMessage(string $data): Message
{
$message = $this->parse($data, 0);
if ($message === null) {
Expand Down
3 changes: 2 additions & 1 deletion src/Query/CachingExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use React\Cache\CacheInterface;
use React\Dns\Model\Message;
use React\Promise\Promise;
use React\Promise\PromiseInterface;

final class CachingExecutor implements ExecutorInterface
{
Expand All @@ -24,7 +25,7 @@ public function __construct(ExecutorInterface $executor, CacheInterface $cache)
$this->cache = $cache;
}

public function query(Query $query)
public function query(Query $query): PromiseInterface
{
$id = $query->name . ':' . $query->type . ':' . $query->class;

Expand Down
3 changes: 2 additions & 1 deletion src/Query/CoopExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace React\Dns\Query;

use React\Promise\Promise;
use React\Promise\PromiseInterface;

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

public function query(Query $query)
public function query(Query $query): PromiseInterface
{
$key = $this->serializeQueryToIdentity($query);
if (isset($this->pending[$key])) {
Expand Down
4 changes: 3 additions & 1 deletion src/Query/ExecutorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace React\Dns\Query;

use React\Promise\PromiseInterface;

interface ExecutorInterface
{
/**
Expand Down Expand Up @@ -39,5 +41,5 @@ interface ExecutorInterface
* @return \React\Promise\PromiseInterface<\React\Dns\Model\Message>
* resolves with response message on success or rejects with an Exception on error
*/
public function query(Query $query);
public function query(Query $query): PromiseInterface;
}
3 changes: 2 additions & 1 deletion src/Query/FallbackExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace React\Dns\Query;

use React\Promise\Promise;
use React\Promise\PromiseInterface;

final class FallbackExecutor implements ExecutorInterface
{
Expand All @@ -15,7 +16,7 @@ public function __construct(ExecutorInterface $executor, ExecutorInterface $fall
$this->fallback = $fallback;
}

public function query(Query $query)
public function query(Query $query): PromiseInterface
{
$cancelled = false;
$promise = $this->executor->query($query);
Expand Down
3 changes: 2 additions & 1 deletion src/Query/HostsFileExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use React\Dns\Config\HostsFile;
use React\Dns\Model\Message;
use React\Dns\Model\Record;
use React\Promise\PromiseInterface;
use function React\Promise\resolve;

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

public function query(Query $query)
public function query(Query $query): PromiseInterface
{
if ($query->class === Message::CLASS_IN && ($query->type === Message::TYPE_A || $query->type === Message::TYPE_AAAA)) {
// forward lookup for type A or AAAA
Expand Down
4 changes: 2 additions & 2 deletions src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class Query
* @param int $type query type, see Message::TYPE_* constants
* @param int $class query class, see Message::CLASS_IN constant
*/
public function __construct($name, $type, $class)
public function __construct(string $name, int $type, int $class)
{
$this->name = $name;
$this->type = $type;
Expand All @@ -51,7 +51,7 @@ public function __construct($name, $type, $class)
* @return string "example.com (A)" or "example.com (CLASS0 TYPE1234)"
* @link https://tools.ietf.org/html/rfc3597
*/
public function describe()
public function describe(): string
{
$class = $this->class !== Message::CLASS_IN ? 'CLASS' . $this->class . ' ' : '';

Expand Down
6 changes: 3 additions & 3 deletions src/Query/RetryExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ final class RetryExecutor implements ExecutorInterface
private $executor;
private $retries;

public function __construct(ExecutorInterface $executor, $retries = 2)
public function __construct(ExecutorInterface $executor, int $retries = 2)
{
$this->executor = $executor;
$this->retries = $retries;
}

public function query(Query $query)
public function query(Query $query): PromiseInterface
{
return $this->tryQuery($query, $this->retries);
}

public function tryQuery(Query $query, $retries)
public function tryQuery(Query $query, int $retries): PromiseInterface
{
$deferred = new Deferred(function () use (&$promise) {
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
Expand Down
3 changes: 2 additions & 1 deletion src/Query/SelectiveTransportExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace React\Dns\Query;

use React\Promise\Promise;
use React\Promise\PromiseInterface;

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

public function query(Query $query)
public function query(Query $query): PromiseInterface
{
$pending = $this->datagramExecutor->query($query);

Expand Down
5 changes: 3 additions & 2 deletions src/Query/TcpTransportExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use function React\Promise\reject;

/**
Expand Down Expand Up @@ -135,7 +136,7 @@ class TcpTransportExecutor implements ExecutorInterface
* @param string $nameserver
* @param ?LoopInterface $loop
*/
public function __construct($nameserver, ?LoopInterface $loop = null)
public function __construct(string $nameserver, ?LoopInterface $loop = null)
{
if (\strpos($nameserver, '[') === false && \substr_count($nameserver, ':') >= 2 && \strpos($nameserver, '://') === false) {
// several colons, but not enclosed in square brackets => enclose IPv6 address in square brackets
Expand All @@ -153,7 +154,7 @@ public function __construct($nameserver, ?LoopInterface $loop = null)
$this->dumper = new BinaryDumper();
}

public function query(Query $query)
public function query(Query $query): PromiseInterface
{
$request = Message::createRequestForQuery($query);

Expand Down
5 changes: 3 additions & 2 deletions src/Query/TimeoutExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise\Promise;
use React\Promise\PromiseInterface;

final class TimeoutExecutor implements ExecutorInterface
{
private $executor;
private $loop;
private $timeout;

public function __construct(ExecutorInterface $executor, $timeout, ?LoopInterface $loop = null)
public function __construct(ExecutorInterface $executor, float $timeout, ?LoopInterface $loop = null)
{
$this->executor = $executor;
$this->loop = $loop ?: Loop::get();
$this->timeout = $timeout;
}

public function query(Query $query)
public function query(Query $query): PromiseInterface
{
$promise = $this->executor->query($query);

Expand Down
Loading

0 comments on commit 1e20620

Please sign in to comment.