1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 00:49:48 +01:00

Add more class and return types

More PHP 7.1 to 8 types added.
This commit is contained in:
Aaron Piotrowski 2020-09-24 22:17:13 -05:00
parent 8e4cc1bbd1
commit d48e6bd5d2
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
22 changed files with 123 additions and 139 deletions

View File

@ -29,7 +29,7 @@ interface CancellationToken
* *
* @return void * @return void
*/ */
public function unsubscribe(string $id); public function unsubscribe(string $id): void;
/** /**
* Returns whether cancellation has been requested yet. * Returns whether cancellation has been requested yet.
@ -45,5 +45,5 @@ interface CancellationToken
* *
* @throws CancelledException * @throws CancelledException
*/ */
public function throwIfRequested(); public function throwIfRequested(): void;
} }

View File

@ -40,8 +40,7 @@ use function Amp\Promise\rethrow;
*/ */
final class CancellationTokenSource final class CancellationTokenSource
{ {
/** @var CancellationToken */ private CancellationToken $token;
private $token;
/** @var callable|null */ /** @var callable|null */
private $onCancel; private $onCancel;
@ -51,14 +50,13 @@ final class CancellationTokenSource
$onCancel = null; $onCancel = null;
$this->token = new class($onCancel) implements CancellationToken { $this->token = new class($onCancel) implements CancellationToken {
/** @var string */ private string $nextId = "a";
private $nextId = "a";
/** @var callable[] */ /** @var callable[] */
private $callbacks = []; private array $callbacks = [];
/** @var \Throwable|null */ /** @var \Throwable|null */
private $exception; private ?\Throwable $exception = null;
/** /**
* @param mixed $onCancel * @param mixed $onCancel
@ -84,7 +82,7 @@ final class CancellationTokenSource
* *
* @return void * @return void
*/ */
private function invokeCallback(callable $callback) private function invokeCallback(callable $callback): void
{ {
// No type declaration to prevent exception outside the try! // No type declaration to prevent exception outside the try!
try { try {
@ -119,7 +117,7 @@ final class CancellationTokenSource
return $id; return $id;
} }
public function unsubscribe(string $id) public function unsubscribe(string $id): void
{ {
unset($this->callbacks[$id]); unset($this->callbacks[$id]);
} }
@ -129,7 +127,7 @@ final class CancellationTokenSource
return isset($this->exception); return isset($this->exception);
} }
public function throwIfRequested() public function throwIfRequested(): void
{ {
if (isset($this->exception)) { if (isset($this->exception)) {
throw $this->exception; throw $this->exception;

View File

@ -57,7 +57,7 @@ final class CombinedCancellationToken implements CancellationToken
} }
/** @inheritdoc */ /** @inheritdoc */
public function unsubscribe(string $id) public function unsubscribe(string $id): void
{ {
unset($this->callbacks[$id]); unset($this->callbacks[$id]);
} }
@ -75,7 +75,7 @@ final class CombinedCancellationToken implements CancellationToken
} }
/** @inheritdoc */ /** @inheritdoc */
public function throwIfRequested() public function throwIfRequested(): void
{ {
foreach ($this->tokens as list($token)) { foreach ($this->tokens as list($token)) {
$token->throwIfRequested(); $token->throwIfRequested();

View File

@ -63,7 +63,7 @@ final class Emitter
* *
* @return void * @return void
*/ */
public function complete() public function complete(): void
{ {
/** @psalm-suppress UndefinedInterfaceMethod */ /** @psalm-suppress UndefinedInterfaceMethod */
$this->emitter->complete(); $this->emitter->complete();
@ -76,7 +76,7 @@ final class Emitter
* *
* @return void * @return void
*/ */
public function fail(\Throwable $reason) public function fail(\Throwable $reason): void
{ {
/** @psalm-suppress UndefinedInterfaceMethod */ /** @psalm-suppress UndefinedInterfaceMethod */
$this->emitter->fail($reason); $this->emitter->fail($reason);

View File

@ -25,8 +25,7 @@ trait Placeholder
* mixed>|null)|callable(\Throwable|null, mixed): void */ * mixed>|null)|callable(\Throwable|null, mixed): void */
private $onResolved; private $onResolved;
/** @var null|array */ private ?array $resolutionTrace = null;
private ?array $resolutionTrace;
/** /**
* @inheritdoc * @inheritdoc

View File

@ -10,8 +10,7 @@ use Amp\Promise;
*/ */
final class PrivatePromise implements Promise final class PrivatePromise implements Promise
{ {
/** @var Promise */ private Promise $promise;
private $promise;
public function __construct(Promise $promise) public function __construct(Promise $promise)
{ {

View File

@ -31,7 +31,7 @@ trait Producer
private ?Deferred $waiting; private ?Deferred $waiting;
private ?array $resolutionTrace; private ?array $resolutionTrace = null;
/** /**
* {@inheritdoc} * {@inheritdoc}

View File

@ -18,7 +18,7 @@ final class Loop
/** /**
* @var Driver * @var Driver
*/ */
private static $driver; private static Driver $driver;
/** /**
* Disable construction as this is a static class. * Disable construction as this is a static class.
@ -35,7 +35,7 @@ final class Loop
* *
* @return void * @return void
*/ */
public static function set(Driver $driver) public static function set(Driver $driver): void
{ {
try { try {
self::$driver = new class extends Driver { self::$driver = new class extends Driver {
@ -77,7 +77,7 @@ final class Loop
* *
* @return void * @return void
*/ */
public static function run(callable $callback = null) public static function run(callable $callback = null): void
{ {
if ($callback) { if ($callback) {
self::$driver->defer($callback); self::$driver->defer($callback);
@ -94,7 +94,7 @@ final class Loop
* *
* @return void * @return void
*/ */
public static function stop() public static function stop(): void
{ {
self::$driver->stop(); self::$driver->stop();
} }
@ -246,7 +246,7 @@ final class Loop
* *
* @throws InvalidWatcherError If the watcher identifier is invalid. * @throws InvalidWatcherError If the watcher identifier is invalid.
*/ */
public static function enable(string $watcherId) public static function enable(string $watcherId): void
{ {
self::$driver->enable($watcherId); self::$driver->enable($watcherId);
} }
@ -264,7 +264,7 @@ final class Loop
* *
* @return void * @return void
*/ */
public static function disable(string $watcherId) public static function disable(string $watcherId): void
{ {
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) { if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
// Prior to PHP 7.2, self::$driver may be unset during destruct. // Prior to PHP 7.2, self::$driver may be unset during destruct.
@ -285,7 +285,7 @@ final class Loop
* *
* @return void * @return void
*/ */
public static function cancel(string $watcherId) public static function cancel(string $watcherId): void
{ {
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) { if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
// Prior to PHP 7.2, self::$driver may be unset during destruct. // Prior to PHP 7.2, self::$driver may be unset during destruct.
@ -308,7 +308,7 @@ final class Loop
* *
* @throws InvalidWatcherError If the watcher identifier is invalid. * @throws InvalidWatcherError If the watcher identifier is invalid.
*/ */
public static function reference(string $watcherId) public static function reference(string $watcherId): void
{ {
self::$driver->reference($watcherId); self::$driver->reference($watcherId);
} }
@ -323,7 +323,7 @@ final class Loop
* *
* @return void * @return void
*/ */
public static function unreference(string $watcherId) public static function unreference(string $watcherId): void
{ {
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) { if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
// Prior to PHP 7.2, self::$driver may be unset during destruct. // Prior to PHP 7.2, self::$driver may be unset during destruct.
@ -360,7 +360,7 @@ final class Loop
* *
* @return void * @return void
*/ */
public static function setState(string $key, $value) public static function setState(string $key, $value): void
{ {
self::$driver->setState($key, $value); self::$driver->setState($key, $value);
} }
@ -378,7 +378,7 @@ final class Loop
* *
* @return mixed The previously stored value or `null` if it doesn't exist. * @return mixed The previously stored value or `null` if it doesn't exist.
*/ */
public static function getState(string $key) public static function getState(string $key): mixed
{ {
return self::$driver->getState($key); return self::$driver->getState($key);
} }
@ -397,7 +397,7 @@ final class Loop
* *
* @return callable(\Throwable $error)|null The previous handler, `null` if there was none. * @return callable(\Throwable $error)|null The previous handler, `null` if there was none.
*/ */
public static function setErrorHandler(callable $callback = null) public static function setErrorHandler(callable $callback = null): ?callable
{ {
return self::$driver->setErrorHandler($callback); return self::$driver->setErrorHandler($callback);
} }

View File

@ -21,29 +21,28 @@ abstract class Driver implements \FiberScheduler
const MILLISEC_PER_SEC = 1000; const MILLISEC_PER_SEC = 1000;
const MICROSEC_PER_SEC = 1000000; const MICROSEC_PER_SEC = 1000000;
/** @var string */ /** @var string Next watcher ID. */
private $nextId = "a"; private string $nextId = "a";
/** @var Watcher[] */ /** @var Watcher[] */
private $watchers = []; private array $watchers = [];
/** @var Watcher[] */ /** @var Watcher[] */
private $enableQueue = []; private array $enableQueue = [];
/** @var Watcher[] */ /** @var Watcher[] */
private $deferQueue = []; private array $deferQueue = [];
/** @var Watcher[] */ /** @var Watcher[] */
private $nextTickQueue = []; private array $nextTickQueue = [];
/** @var callable(\Throwable):void|null */ /** @var callable(\Throwable):void|null */
private $errorHandler; private $errorHandler;
/** @var bool */ private bool $running = false;
private $running = false;
/** @var array */ /** @var mixed[] */
private $registry = []; private array $registry = [];
/** /**
* Run the event loop. * Run the event loop.

View File

@ -44,7 +44,7 @@ class DriverFactory
/** /**
* @return Driver|null * @return Driver|null
*/ */
private function createDriverFromEnv() private function createDriverFromEnv(): ?Driver
{ {
$driver = \getenv("AMP_LOOP_DRIVER"); $driver = \getenv("AMP_LOOP_DRIVER");

View File

@ -11,29 +11,33 @@ use function Amp\Promise\rethrow;
class EvDriver extends Driver class EvDriver extends Driver
{ {
/** @var \EvSignal[]|null */ /** @var \EvSignal[]|null */
private static $activeSignals; private static ?array $activeSignals = null;
public static function isSupported(): bool public static function isSupported(): bool
{ {
return \extension_loaded("ev"); return \extension_loaded("ev");
} }
/** @var \EvLoop */ private \EvLoop $handle;
private $handle;
/** @var \EvWatcher[] */ /** @var \EvWatcher[] */
private $events = []; private array $events = [];
private \Closure $ioCallback;
/** @var callable */ /** @var callable */
private $ioCallback;
/** @var callable */ private \Closure $timerCallback;
private $timerCallback;
/** @var callable */ private \Closure $signalCallback;
private $signalCallback;
/** @var \EvSignal[] */ /** @var \EvSignal[] */
private $signals = []; private array $signals = [];
/** @var int Internal timestamp for now. */ /** @var int Internal timestamp for now. */
private $now; private int $now;
/** @var int Loop time offset */ /** @var int Loop time offset */
private $nowOffset; private int $nowOffset;
public function __construct() public function __construct()
{ {
@ -51,7 +55,7 @@ class EvDriver extends Driver
* *
* @return void * @return void
*/ */
$this->ioCallback = function (\EvIO $event) { $this->ioCallback = function (\EvIO $event): void {
/** @var Watcher $watcher */ /** @var Watcher $watcher */
$watcher = $event->data; $watcher = $event->data;
@ -79,7 +83,7 @@ class EvDriver extends Driver
* *
* @return void * @return void
*/ */
$this->timerCallback = function (\EvTimer $event) { $this->timerCallback = function (\EvTimer $event): void {
/** @var Watcher $watcher */ /** @var Watcher $watcher */
$watcher = $event->data; $watcher = $event->data;
@ -116,7 +120,7 @@ class EvDriver extends Driver
* *
* @return void * @return void
*/ */
$this->signalCallback = function (\EvSignal $event) { $this->signalCallback = function (\EvSignal $event): void {
/** @var Watcher $watcher */ /** @var Watcher $watcher */
$watcher = $event->data; $watcher = $event->data;

View File

@ -11,31 +11,26 @@ use function Amp\Promise\rethrow;
class EventDriver extends Driver class EventDriver extends Driver
{ {
/** @var \Event[]|null */ /** @var \Event[]|null */
private static $activeSignals; private static ?array $activeSignals = null;
/** @var \EventBase */ private \EventBase $handle;
private $handle;
/** @var \Event[] */ /** @var \Event[] */
private $events = []; private array $events = [];
/** @var callable */ private \Closure $ioCallback;
private $ioCallback;
/** @var callable */ private \Closure $timerCallback;
private $timerCallback;
/** @var callable */ private \Closure $signalCallback;
private $signalCallback;
/** @var \Event[] */ private array $signals = [];
private $signals = [];
/** @var int Internal timestamp for now. */ /** @var int Internal timestamp for now. */
private $now; private int $now;
/** @var int Loop time offset */ /** @var int Loop time offset */
private $nowOffset; private int $nowOffset;
public function __construct() public function __construct()
{ {

View File

@ -10,10 +10,10 @@ use Amp\Loop\Watcher;
final class TimerQueue final class TimerQueue
{ {
/** @var TimerQueueEntry[] */ /** @var TimerQueueEntry[] */
private $data = []; private array $data = [];
/** @var int[] */ /** @var int[] */
private $pointers = []; private array $pointers = [];
/** /**
* Inserts the watcher into the queue. Time complexity: O(log(n)). * Inserts the watcher into the queue. Time complexity: O(log(n)).

View File

@ -12,11 +12,9 @@ final class TimerQueueEntry
{ {
use Struct; use Struct;
/** @var Watcher */ public Watcher $watcher;
public $watcher;
/** @var int */ public int $expiration;
public $expiration;
/** /**
* @param Watcher $watcher * @param Watcher $watcher

View File

@ -14,31 +14,29 @@ class NativeDriver extends Driver
use CallableMaker; use CallableMaker;
/** @var resource[] */ /** @var resource[] */
private $readStreams = []; private array $readStreams = [];
/** @var Watcher[][] */ /** @var Watcher[][] */
private $readWatchers = []; private array $readWatchers = [];
/** @var resource[] */ /** @var resource[] */
private $writeStreams = []; private array $writeStreams = [];
/** @var Watcher[][] */ /** @var Watcher[][] */
private $writeWatchers = []; private array $writeWatchers = [];
/** @var Internal\TimerQueue */ private Internal\TimerQueue $timerQueue;
private $timerQueue;
/** @var Watcher[][] */ /** @var Watcher[][] */
private $signalWatchers = []; private array $signalWatchers = [];
/** @var int Internal timestamp for now. */ /** @var int Internal timestamp for now. */
private $now; private int $now;
/** @var int Loop time offset */ /** @var int Loop time offset */
private $nowOffset; private int $nowOffset;
/** @var bool */ private bool $signalHandling;
private $signalHandling;
public function __construct() public function __construct()
{ {

View File

@ -6,28 +6,31 @@ use function Amp\Internal\formatStacktrace;
final class TracingDriver extends Driver final class TracingDriver extends Driver
{ {
/** @var Driver */ private Driver $driver;
private $driver;
/** @var true[] */ /** @var true[] */
private $enabledWatchers = []; private array $enabledWatchers = [];
/** @var true[] */ /** @var true[] */
private $unreferencedWatchers = []; private array $unreferencedWatchers = [];
/** @var string[] */ /** @var string[] */
private $creationTraces = []; private array $creationTraces = [];
/** @var string[] */ /** @var string[] */
private $cancelTraces = []; private array $cancelTraces = [];
public function __construct(Driver $driver) public function __construct(Driver $driver)
{ {
$this->driver = $driver; $this->driver = $driver;
} }
public function run() public function run(): void
{ {
$this->driver->run(); $this->driver->run();
} }
public function stop() public function stop(): void
{ {
$this->driver->stop(); $this->driver->stop();
} }
@ -111,7 +114,7 @@ final class TracingDriver extends Driver
} }
} }
public function cancel(string $watcherId) public function cancel(string $watcherId): void
{ {
$this->driver->cancel($watcherId); $this->driver->cancel($watcherId);
@ -122,13 +125,13 @@ final class TracingDriver extends Driver
unset($this->enabledWatchers[$watcherId], $this->unreferencedWatchers[$watcherId]); unset($this->enabledWatchers[$watcherId], $this->unreferencedWatchers[$watcherId]);
} }
public function disable(string $watcherId) public function disable(string $watcherId): void
{ {
$this->driver->disable($watcherId); $this->driver->disable($watcherId);
unset($this->enabledWatchers[$watcherId]); unset($this->enabledWatchers[$watcherId]);
} }
public function reference(string $watcherId) public function reference(string $watcherId): void
{ {
try { try {
$this->driver->reference($watcherId); $this->driver->reference($watcherId);
@ -141,13 +144,13 @@ final class TracingDriver extends Driver
} }
} }
public function unreference(string $watcherId) public function unreference(string $watcherId): void
{ {
$this->driver->unreference($watcherId); $this->driver->unreference($watcherId);
$this->unreferencedWatchers[$watcherId] = true; $this->unreferencedWatchers[$watcherId] = true;
} }
public function setErrorHandler(callable $callback = null) public function setErrorHandler(callable $callback = null): ?callable
{ {
return $this->driver->setErrorHandler($callback); return $this->driver->setErrorHandler($callback);
} }
@ -180,7 +183,7 @@ final class TracingDriver extends Driver
return $this->driver->getInfo(); return $this->driver->getInfo();
} }
public function __debugInfo() public function __debugInfo(): array
{ {
return $this->driver->__debugInfo(); return $this->driver->__debugInfo();
} }
@ -190,7 +193,7 @@ final class TracingDriver extends Driver
return $this->driver->now(); return $this->driver->now();
} }
protected function error(\Throwable $exception) protected function error(\Throwable $exception): void
{ {
$this->driver->error($exception); $this->driver->error($exception);
} }
@ -200,7 +203,7 @@ final class TracingDriver extends Driver
* *
* @return void * @return void
*/ */
protected function activate(array $watchers) protected function activate(array $watchers): void
{ {
// nothing to do in a decorator // nothing to do in a decorator
} }
@ -210,7 +213,7 @@ final class TracingDriver extends Driver
* *
* @return void * @return void
*/ */
protected function dispatch(bool $blocking) protected function dispatch(bool $blocking): void
{ {
// nothing to do in a decorator // nothing to do in a decorator
} }
@ -220,7 +223,7 @@ final class TracingDriver extends Driver
* *
* @return void * @return void
*/ */
protected function deactivate(Watcher $watcher) protected function deactivate(Watcher $watcher): void
{ {
// nothing to do in a decorator // nothing to do in a decorator
} }

View File

@ -9,26 +9,23 @@ use function Amp\Promise\rethrow;
class UvDriver extends Driver class UvDriver extends Driver
{ {
/** @var resource A uv_loop resource created with uv_loop_new() */ /** @var resource|\UVLoop A uv_loop resource created with uv_loop_new() */
private $handle; private $handle;
/** @var resource[] */ /** @var resource[] */
private $events = []; private array $events = [];
/** @var Watcher[][] */ /** @var Watcher[][] */
private $watchers = []; private array $watchers = [];
/** @var resource[] */ /** @var resource[] */
private $streams = []; private array $streams = [];
/** @var callable */ private \Closure $ioCallback;
private $ioCallback;
/** @var callable */ private \Closure $timerCallback;
private $timerCallback;
/** @var callable */ private \Closure $signalCallback;
private $signalCallback;
public function __construct() public function __construct()
{ {
@ -42,7 +39,7 @@ class UvDriver extends Driver
* *
* @return void * @return void
*/ */
$this->ioCallback = function ($event, $status, $events, $resource) { $this->ioCallback = function ($event, $status, $events, $resource): void {
$watchers = $this->watchers[(int) $event]; $watchers = $this->watchers[(int) $event];
switch ($status) { switch ($status) {
@ -91,7 +88,7 @@ class UvDriver extends Driver
* *
* @return void * @return void
*/ */
$this->timerCallback = function ($event) { $this->timerCallback = function ($event): void {
$watcher = $this->watchers[(int) $event][0]; $watcher = $this->watchers[(int) $event][0];
if ($watcher->type & Watcher::DELAY) { if ($watcher->type & Watcher::DELAY) {
@ -129,7 +126,7 @@ class UvDriver extends Driver
* *
* @return void * @return void
*/ */
$this->signalCallback = function ($event, $signo) { $this->signalCallback = function ($event, $signo): void {
$watcher = $this->watchers[(int) $event][0]; $watcher = $this->watchers[(int) $event][0];
try { try {

View File

@ -22,17 +22,13 @@ class Watcher
const REPEAT = 0b00010000; const REPEAT = 0b00010000;
const SIGNAL = 0b00100000; const SIGNAL = 0b00100000;
/** @var int */ public int $type;
public $type;
/** @var bool */ public bool $enabled = true;
public $enabled = true;
/** @var bool */ public bool $referenced = true;
public $referenced = true;
/** @var string */ public string $id;
public $id;
/** @var callable */ /** @var callable */
public $callback; public $callback;
@ -53,5 +49,5 @@ class Watcher
public $value; public $value;
/** @var int|null */ /** @var int|null */
public $expiration; public ?int $expiration = null;
} }

View File

@ -5,7 +5,7 @@ namespace Amp;
class MultiReasonException extends \Exception class MultiReasonException extends \Exception
{ {
/** @var \Throwable[] */ /** @var \Throwable[] */
private $reasons; private array $reasons;
/** /**
* @param \Throwable[] $reasons Array of exceptions rejecting the promise. * @param \Throwable[] $reasons Array of exceptions rejecting the promise.

View File

@ -34,7 +34,7 @@ final class NullCancellationToken implements CancellationToken
} }
/** @inheritdoc */ /** @inheritdoc */
public function unsubscribe(string $id) public function unsubscribe(string $id): void
{ {
// nothing to do // nothing to do
} }
@ -46,7 +46,7 @@ final class NullCancellationToken implements CancellationToken
} }
/** @inheritdoc */ /** @inheritdoc */
public function throwIfRequested() public function throwIfRequested(): void
{ {
// nothing to do // nothing to do
} }

View File

@ -14,7 +14,7 @@ trait Struct
* The minimum percentage [0-100] at which to recommend a similar property * The minimum percentage [0-100] at which to recommend a similar property
* name when generating error messages. * name when generating error messages.
*/ */
private $__propertySuggestThreshold = 70; private int $__propertySuggestThreshold = 70;
/** /**
* @param string $property * @param string $property

View File

@ -9,11 +9,9 @@ use function Amp\Internal\formatStacktrace;
*/ */
final class TimeoutCancellationToken implements CancellationToken final class TimeoutCancellationToken implements CancellationToken
{ {
/** @var string */ private string $watcher;
private $watcher;
/** @var CancellationToken */ private CancellationToken $token;
private $token;
/** /**
* @param int $timeout Milliseconds until cancellation is requested. * @param int $timeout Milliseconds until cancellation is requested.
@ -52,7 +50,7 @@ final class TimeoutCancellationToken implements CancellationToken
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function unsubscribe(string $id) public function unsubscribe(string $id): void
{ {
$this->token->unsubscribe($id); $this->token->unsubscribe($id);
} }
@ -68,7 +66,7 @@ final class TimeoutCancellationToken implements CancellationToken
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function throwIfRequested() public function throwIfRequested(): void
{ {
$this->token->throwIfRequested(); $this->token->throwIfRequested();
} }