1
0
mirror of https://github.com/danog/amp.git synced 2024-12-02 17:37:50 +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
*/
public function unsubscribe(string $id);
public function unsubscribe(string $id): void;
/**
* Returns whether cancellation has been requested yet.
@ -45,5 +45,5 @@ interface CancellationToken
*
* @throws CancelledException
*/
public function throwIfRequested();
public function throwIfRequested(): void;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -18,7 +18,7 @@ final class Loop
/**
* @var Driver
*/
private static $driver;
private static Driver $driver;
/**
* Disable construction as this is a static class.
@ -35,7 +35,7 @@ final class Loop
*
* @return void
*/
public static function set(Driver $driver)
public static function set(Driver $driver): void
{
try {
self::$driver = new class extends Driver {
@ -77,7 +77,7 @@ final class Loop
*
* @return void
*/
public static function run(callable $callback = null)
public static function run(callable $callback = null): void
{
if ($callback) {
self::$driver->defer($callback);
@ -94,7 +94,7 @@ final class Loop
*
* @return void
*/
public static function stop()
public static function stop(): void
{
self::$driver->stop();
}
@ -246,7 +246,7 @@ final class Loop
*
* @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);
}
@ -264,7 +264,7 @@ final class Loop
*
* @return void
*/
public static function disable(string $watcherId)
public static function disable(string $watcherId): void
{
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
// Prior to PHP 7.2, self::$driver may be unset during destruct.
@ -285,7 +285,7 @@ final class Loop
*
* @return void
*/
public static function cancel(string $watcherId)
public static function cancel(string $watcherId): void
{
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
// 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.
*/
public static function reference(string $watcherId)
public static function reference(string $watcherId): void
{
self::$driver->reference($watcherId);
}
@ -323,7 +323,7 @@ final class Loop
*
* @return void
*/
public static function unreference(string $watcherId)
public static function unreference(string $watcherId): void
{
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
// Prior to PHP 7.2, self::$driver may be unset during destruct.
@ -360,7 +360,7 @@ final class Loop
*
* @return void
*/
public static function setState(string $key, $value)
public static function setState(string $key, $value): void
{
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.
*/
public static function getState(string $key)
public static function getState(string $key): mixed
{
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.
*/
public static function setErrorHandler(callable $callback = null)
public static function setErrorHandler(callable $callback = null): ?callable
{
return self::$driver->setErrorHandler($callback);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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