Skip to content

Commit

Permalink
Update docblock type annotations
Browse files Browse the repository at this point in the history
Updated docblocks with new syntax supported by PhpStorm and Psalm.
  • Loading branch information
trowski committed Jul 29, 2022
1 parent b80a258 commit b805042
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function now(): float
* Does NOT create an event callback, thus CAN NOT be marked as disabled or unreferenced.
* Use {@see EventLoop::defer()} if you need these features.
*
* @param \Closure $closure The callback to queue.
* @param \Closure(...):void $closure The callback to queue.
* @param mixed ...$args The callback arguments.
*/
public static function queue(\Closure $closure, mixed ...$args): void
Expand Down
7 changes: 4 additions & 3 deletions src/EventLoop/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public function isRunning(): bool;
* Does NOT create an event callback, thus CAN NOT be marked as disabled or unreferenced.
* Use {@see EventLoop::defer()} if you need these features.
*
* @param \Closure $closure The callback to queue.
* @param mixed ...$args The callback arguments.
* @param \Closure(...):void $closure The callback to queue.
* @param mixed ...$args The callback arguments.
*/
public function queue(\Closure $closure, mixed ...$args): void;

Expand Down Expand Up @@ -246,7 +246,8 @@ public function unreference(string $callbackId): string;
*
* Subsequent calls to this method will overwrite the previous handler.
*
* @param null|\Closure(\Throwable):void $errorHandler The callback to execute. `null` will clear the current handler.
* @param null|\Closure(\Throwable):void $errorHandler The callback to execute. `null` will clear the current
* handler.
*/
public function setErrorHandler(?\Closure $errorHandler): void;

Expand Down
6 changes: 3 additions & 3 deletions src/EventLoop/Driver/EvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

final class EvDriver extends AbstractDriver
{
/** @var \EvSignal[]|null */
/** @var array<string, \EvSignal>|null */
private static ?array $activeSignals = null;

public static function isSupported(): bool
Expand All @@ -24,7 +24,7 @@ public static function isSupported(): bool

private \EvLoop $handle;

/** @var \EvWatcher[] */
/** @var array<string, \EvWatcher> */
private array $events = [];

private readonly \Closure $ioCallback;
Expand All @@ -33,7 +33,7 @@ public static function isSupported(): bool

private readonly \Closure $signalCallback;

/** @var \EvSignal[] */
/** @var array<string, \EvSignal> */
private array $signals = [];

public function __construct()
Expand Down
6 changes: 4 additions & 2 deletions src/EventLoop/Driver/EventDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

final class EventDriver extends AbstractDriver
{
/** @var \Event[]|null */
/** @var array<string, \Event>|null */
private static ?array $activeSignals = null;

public static function isSupported(): bool
Expand All @@ -23,11 +23,13 @@ public static function isSupported(): bool
}

private \EventBase $handle;
/** @var \Event[] */
/** @var array<string, \Event> */
private array $events = [];
private readonly \Closure $ioCallback;
private readonly \Closure $timerCallback;
private readonly \Closure $signalCallback;

/** @var array<string, \Event> */
private array $signals = [];

public function __construct()
Expand Down
31 changes: 9 additions & 22 deletions src/EventLoop/Driver/StreamSelectDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@

final class StreamSelectDriver extends AbstractDriver
{
/** @var resource[]|object[] */
/** @var array<int, resource> */
private array $readStreams = [];

/** @var StreamReadableCallback[][] */
/** @var array<int, array<string, StreamReadableCallback>> */
private array $readCallbacks = [];

/** @var resource[]|object[] */
/** @var array<int, resource> */
private array $writeStreams = [];

/** @var StreamWritableCallback[][] */
/** @var array<int, array<string, StreamWritableCallback>> */
private array $writeCallbacks = [];

private readonly TimerQueue $timerQueue;

/** @var SignalCallback[][] */
/** @var array<int, array<string, SignalCallback>> */
private array $signalCallbacks = [];

/** @var \SplQueue<int> */
Expand All @@ -49,7 +49,7 @@ public function __construct()
$this->timerQueue = new TimerQueue();
$this->signalHandling = \extension_loaded("pcntl");

$this->streamSelectErrorHandler = function ($errno, $message) {
$this->streamSelectErrorHandler = function (int $errno, string $message): void {
// Casing changed in PHP 8 from 'unable' to 'Unable'
if (\stripos($message, "stream_select(): unable to select [4]: ") === 0) { // EINTR
$this->streamSelectIgnoreResult = true;
Expand Down Expand Up @@ -86,8 +86,6 @@ public function __destruct()
}

/**
* {@inheritdoc}
*
* @throws UnsupportedFeatureException If the pcntl extension is not available.
*/
public function onSignal(int $signal, \Closure $closure): string
Expand All @@ -99,9 +97,6 @@ public function onSignal(int $signal, \Closure $closure): string
return parent::onSignal($signal, $closure);
}

/**
* {@inheritdoc}
*/
public function getHandle(): mixed
{
return null;
Expand Down Expand Up @@ -144,9 +139,6 @@ protected function dispatch(bool $blocking): void
}
}

/**
* {@inheritdoc}
*/
protected function activate(array $callbacks): void
{
foreach ($callbacks as $callback) {
Expand Down Expand Up @@ -191,9 +183,6 @@ protected function activate(array $callbacks): void
}
}

/**
* {@inheritdoc}
*/
protected function deactivate(DriverCallback $callback): void
{
if ($callback instanceof StreamReadableCallback) {
Expand Down Expand Up @@ -232,8 +221,8 @@ protected function deactivate(DriverCallback $callback): void
}

/**
* @param resource[]|object[] $read
* @param resource[]|object[] $write
* @param array<int, resource> $read
* @param array<int, resource> $write
*/
private function selectStreams(array $read, array $write, float $timeout): void
{
Expand Down Expand Up @@ -283,9 +272,7 @@ private function selectStreams(array $read, array $write, float $timeout): void
}
}

\assert(\is_array($write)); // See https://github.com/vimeo/psalm/issues/3036

/** @var resource[]|object[]|null $except */
/** @var array<int, resource>|null $except */
if ($except) {
foreach ($except as $key => $socket) {
$write[$key] = $socket;
Expand Down
8 changes: 4 additions & 4 deletions src/EventLoop/Driver/TracingDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ final class TracingDriver implements Driver
{
private readonly Driver $driver;

/** @var true[] */
/** @var array<string, true> */
private array $enabledCallbacks = [];

/** @var true[] */
/** @var array<string, true> */
private array $unreferencedCallbacks = [];

/** @var string[] */
/** @var array<string, string> */
private array $creationTraces = [];

/** @var string[] */
/** @var array<string, string> */
private array $cancelTraces = [];

public function __construct(Driver $driver)
Expand Down
6 changes: 3 additions & 3 deletions src/EventLoop/Driver/UvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public static function isSupported(): bool

/** @var resource|\UVLoop A uv_loop resource created with uv_loop_new() */
private $handle;
/** @var resource[] */
/** @var array<string, resource> */
private array $events = [];
/** @var DriverCallback[][] */
/** @var array<int, array<array-key, DriverCallback>> */
private array $callbacks = [];
/** @var resource[] */
/** @var array<int, resource> */
private array $streams = [];
private readonly \Closure $ioCallback;
private readonly \Closure $timerCallback;
Expand Down
2 changes: 1 addition & 1 deletion src/EventLoop/FiberLocal.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private static function getFiberStorage(): \WeakMap
$fiber = \Fiber::getCurrent();

if ($fiber === null) {
$fiber = self::$mainFiber ??= new \Fiber(static function () {
$fiber = self::$mainFiber ??= new \Fiber(static function (): void {
// dummy fiber for main, as we need some object for the WeakMap
});
}
Expand Down
13 changes: 9 additions & 4 deletions src/EventLoop/Internal/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ abstract class AbstractDriver implements Driver
private \Fiber $callbackFiber;
private \Closure $errorCallback;

/** @var DriverCallback[] */
/** @var array<string, DriverCallback> */
private array $callbacks = [];

/** @var DriverCallback[] */
/** @var array<string, DriverCallback> */
private array $enableQueue = [];

/** @var DriverCallback[] */
/** @var array<string, DriverCallback> */
private array $enableDeferQueue = [];

/** @var null|\Closure(\Throwable) */
/** @var null|\Closure(\Throwable):void */
private ?\Closure $errorHandler = null;

/** @var null|\Closure():mixed */
private ?\Closure $interrupt = null;

private readonly \Closure $interruptCallback;
Expand Down Expand Up @@ -501,6 +503,9 @@ private function invokeCallbacks(): void
}
}

/**
* @param \Closure():mixed $interrupt
*/
private function setInterrupt(\Closure $interrupt): void
{
\assert($this->interrupt === null);
Expand Down
17 changes: 6 additions & 11 deletions src/EventLoop/Internal/DriverSuspension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ final class DriverSuspension implements Suspension

private ?\FiberError $fiberError = null;

private readonly \Closure $run;

private readonly \Closure $queue;

private readonly \Closure $interrupt;

private bool $pending = false;

private readonly \WeakReference $suspensions;
Expand All @@ -36,13 +30,14 @@ final class DriverSuspension implements Suspension
*
* @internal
*/
public function __construct(\Closure $run, \Closure $queue, \Closure $interrupt, \WeakMap $suspensions)
{
public function __construct(
private readonly \Closure $run,
private readonly \Closure $queue,
private readonly \Closure $interrupt,
\WeakMap $suspensions
) {
$fiber = \Fiber::getCurrent();

$this->run = $run;
$this->queue = $queue;
$this->interrupt = $interrupt;
$this->fiberRef = $fiber ? \WeakReference::create($fiber) : null;
$this->suspensions = \WeakReference::create($suspensions);
}
Expand Down
4 changes: 2 additions & 2 deletions src/EventLoop/Internal/TimerQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
*/
final class TimerQueue
{
/** @var TimerCallback[] */
/** @var array<int, TimerCallback> */
private array $callbacks = [];

/** @var int[] */
/** @var array<string, int> */
private array $pointers = [];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/EventLoop/InvalidCallbackError.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function invalidIdentifier(string $callbackId): self
/** @var string */
private readonly string $callbackId;

/** @var string[] */
/** @var array<string, string> */
private array $info = [];

/**
Expand Down
9 changes: 6 additions & 3 deletions src/EventLoop/Suspension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@
interface Suspension
{
/**
* @param T $value
*
* @return void
* @param T $value The value to return from the call to {@see suspend()}.
*/
public function resume(mixed $value = null): void;

/**
* Returns the value provided to {@see resume()} or throws the exception provided to {@see throw()}.
*
* @return T
*/
public function suspend(): mixed;

/**
* Throws the given exception from the call to {@see suspend()}.
*/
public function throw(\Throwable $throwable): void;
}

0 comments on commit b805042

Please sign in to comment.