Skip to content

Commit

Permalink
chore: update phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
e-zannelli committed Feb 25, 2022
1 parent a9546b7 commit 5e9dee5
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 23 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"ext-curl": "^7.3|^8.0",
"react/event-loop": "^1.0",
"react/promise": "^2.7",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan": "^1.0",
"symfony/http-client": "^4.3",
"psr/http-factory": "^1.0",
"http-interop/http-factory-guzzle": "^1.0"
Expand All @@ -51,7 +51,7 @@
"scripts": {
"tests-unit": "./bin/phpunit --testsuite='Tornado Test Suite'",
"tests-examples": "./bin/phpunit --testsuite='Tornado Examples'",
"static-analysis": "./bin/phpstan analyse src tests --level=7 --no-progress -vvv",
"static-analysis": "./bin/phpstan analyse src tests --level=8 --no-progress -vvv",
"code-style-check": "./bin/php-cs-fixer fix --dry-run --verbose",
"code-style-fix": "./bin/php-cs-fixer fix"
}
Expand Down
15 changes: 12 additions & 3 deletions src/Adapter/Amp/Internal/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
/**
* @internal
* ⚠️ You must NOT rely on this internal implementation
*
* @template TValue
*/
class Deferred implements \M6Web\Tornado\Deferred
{
/** @var \Amp\Deferred */
/** @var \Amp\Deferred<TValue> */
private $ampDeferred;

/** @var PromiseWrapper */
/** @var PromiseWrapper<TValue> */
private $promise;

/**
* @param \Amp\Deferred<TValue> $ampDeferred
* @param PromiseWrapper<TValue> $promise
*/
public function __construct(\Amp\Deferred $ampDeferred, PromiseWrapper $promise)
{
$this->ampDeferred = $ampDeferred;
Expand All @@ -30,13 +36,16 @@ public function getPromise(): Promise
return $this->promise;
}

/**
* @return PromiseWrapper<TValue>
*/
public function getPromiseWrapper(): PromiseWrapper
{
return $this->promise;
}

/**
* {@inheritdoc}
* @param TValue $value
*/
public function resolve($value): void
{
Expand Down
24 changes: 23 additions & 1 deletion src/Adapter/Amp/Internal/PromiseWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
/**
* @internal
* ⚠️ You must NOT rely on this internal implementation
*
* @template TValue
*
* @implements Promise<TValue>
*/
class PromiseWrapper implements Promise
{
/** @var \Amp\Promise */
/** @var \Amp\Promise<TValue> */
private $ampPromise;

/** @var bool */
Expand All @@ -24,6 +28,11 @@ private function __construct()
{
}

/**
* @param \Amp\Promise<TValue> $ampPromise
*
* @return static<TValue>
*/
public static function createUnhandled(\Amp\Promise $ampPromise, FailingPromiseCollection $failingPromiseCollection): self
{
$promiseWrapper = new self();
Expand All @@ -40,6 +49,11 @@ function (?\Throwable $reason, $value) use ($promiseWrapper, $failingPromiseColl
return $promiseWrapper;
}

/**
* @param \Amp\Promise<TValue> $ampPromise
*
* @return static<TValue>
*/
public static function createHandled(\Amp\Promise $ampPromise): self
{
$promiseWrapper = new self();
Expand All @@ -49,11 +63,19 @@ public static function createHandled(\Amp\Promise $ampPromise): self
return $promiseWrapper;
}

/**
* @return \Amp\Promise<TValue>
*/
public function getAmpPromise(): \Amp\Promise
{
return $this->ampPromise;
}

/**
* @param Promise<TValue> $promise
*
* @return static<TValue>
*/
public static function toHandledPromise(Promise $promise, FailingPromiseCollection $failingPromiseCollection): self
{
assert($promise instanceof self, new \Error('Input promise was not created by this adapter.'));
Expand Down
8 changes: 7 additions & 1 deletion src/Adapter/Common/Internal/FailingPromiseCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
*/
class FailingPromiseCollection
{
/**
* @param Tornado\Promise<mixed> $promise
*/
public function watchFailingPromise(Tornado\Promise $promise, \Throwable $throwable): void
{
$this->registeredThrowables->attach($promise, $throwable);
}

/**
* @param Tornado\Promise<mixed> $promise
*/
public function unwatchPromise(Tornado\Promise $promise): void
{
$this->registeredThrowables->detach($promise);
Expand All @@ -35,6 +41,6 @@ public function __construct()
$this->registeredThrowables = new \SplObjectStorage();
}

/** @var \SplObjectStorage */
/** @var \SplObjectStorage<Tornado\Promise<mixed>, \Throwable> */
private $registeredThrowables;
}
12 changes: 6 additions & 6 deletions src/Adapter/Guzzle/CurlMultiClientWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ final class CurlMultiClientWrapper implements GuzzleClientWrapper
/**
* CurlMultiClientWrapper constructor.
*
* @param array $clientConfig configuration for \GuzzleHttp\Client, check corresponding documentation.
* 'handler' configuration will be ignored since built in this wrapper
* @param array $curlMultiOptions options for \GuzzleHttp\Handler\CurlMultiHandler, check corresponding documentation.
* Default value for 'select_timeout' is 0
* @param array $middlewareStack set of name => handler to push on the top of created \GuzzleHttp\HandlerStack,
* check corresponding documentation
* @param array<string, mixed> $clientConfig configuration for \GuzzleHttp\Client, check corresponding documentation.
* 'handler' configuration will be ignored since built in this wrapper
* @param array<string, mixed> $curlMultiOptions options for \GuzzleHttp\Handler\CurlMultiHandler, check corresponding documentation.
* Default value for 'select_timeout' is 0
* @param array<string, callable> $middlewareStack set of name => handler to push on the top of created \GuzzleHttp\HandlerStack,
* check corresponding documentation
*/
public function __construct(array $clientConfig = [], array $curlMultiOptions = [], array $middlewareStack = [])
{
Expand Down
3 changes: 3 additions & 0 deletions src/Adapter/Guzzle/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ private function http2fallback(RequestInterface $request): RequestInterface
return $request;
}

/**
* @return \Generator<int, Promise<null>>
*/
private function guzzleEventLoop(): \Generator
{
do {
Expand Down
8 changes: 8 additions & 0 deletions src/Adapter/ReactPhp/Internal/PromiseWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
/**
* @internal
* ⚠️ You must NOT rely on this internal implementation
*
* @template TValue
* @implements Promise<TValue>
*/
class PromiseWrapper implements Promise
{
Expand Down Expand Up @@ -55,6 +58,11 @@ public function getReactPromise(): \React\Promise\PromiseInterface
return $this->reactPromise;
}

/**
* @param Promise<TValue> $promise
*
* @return static<TValue>
*/
public static function toHandledPromise(Promise $promise, FailingPromiseCollection $failingPromiseCollection): self
{
assert($promise instanceof self, new \Error('Input promise was not created by this adapter.'));
Expand Down
3 changes: 3 additions & 0 deletions src/Adapter/Symfony/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public function sendRequest(RequestInterface $request): Promise
return $deferred->getPromise();
}

/**
* @return \Generator<int, Promise<null>>
*/
private function symfonyEventLoop(): \Generator
{
do {
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/Tornado/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EventLoop implements \M6Web\Tornado\EventLoop
/** @var Internal\StreamEventLoop */
private $streamLoop;

/** @var Internal\Task[] */
/** @var Internal\Task<mixed>[] */
private $tasks = [];

/** @var FailingPromiseCollection */
Expand Down
16 changes: 15 additions & 1 deletion src/Adapter/Tornado/Internal/PendingPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
/**
* @internal
* ⚠️ You must NOT rely on this internal implementation
* @template TValue
*
* @implements Promise<TValue>
*/
class PendingPromise implements Promise, Deferred
{
/** @var mixed */
/** @var TValue */
private $value;
/** @var \Throwable */
private $throwable;
Expand Down Expand Up @@ -46,6 +49,11 @@ public static function createHandled(): self
return $promiseWrapper;
}

/**
* @param Promise<TValue> $promise
*
* @return static<TValue>
*/
public static function toHandledPromise(Promise $promise): self
{
assert($promise instanceof self, new \Error('Input promise was not created by this adapter.'));
Expand All @@ -58,6 +66,9 @@ public static function toHandledPromise(Promise $promise): self
return $promise;
}

/**
* @param TValue $value
*/
public function resolve($value): void
{
$this->settle();
Expand All @@ -78,6 +89,9 @@ public function reject(\Throwable $throwable): void
$this->triggerCallbacks();
}

/**
* @return Promise<TValue>
*/
public function getPromise(): Promise
{
return $this;
Expand Down
15 changes: 13 additions & 2 deletions src/Adapter/Tornado/Internal/StreamEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace M6Web\Tornado\Adapter\Tornado\Internal;

use M6Web\Tornado\EventLoop;
use M6Web\Tornado\Promise;

/**
* @internal
Expand All @@ -13,11 +14,13 @@ class StreamEventLoop
private $readStreams = [];
/** @var resource[] */
private $writeStreams = [];
/** @var PendingPromise[] */
/** @var PendingPromise<resource>[] */
private $pendingPromises = [];

/**
* @param resource $stream
*
* @return PendingPromise<resource>
*/
public function readable(EventLoop $eventLoop, $stream): PendingPromise
{
Expand All @@ -26,12 +29,17 @@ public function readable(EventLoop $eventLoop, $stream): PendingPromise

/**
* @param resource $stream
*
* @return PendingPromise<resource>
*/
public function writable(EventLoop $eventLoop, $stream): PendingPromise
{
return $this->recordStream($eventLoop, $stream, $this->writeStreams);
}

/**
* @return \Generator<int, Promise<null>>
*/
private function internalLoop(EventLoop $eventLoop): \Generator
{
$except = null;
Expand Down Expand Up @@ -65,7 +73,10 @@ private function internalLoop(EventLoop $eventLoop): \Generator
}

/**
* @param resource $stream
* @param resource $stream
* @param array<resource> $streamsList
*
* @return PendingPromise<resource>
*/
private function recordStream(EventLoop $eventLoop, $stream, array &$streamsList): PendingPromise
{
Expand Down
18 changes: 16 additions & 2 deletions src/Adapter/Tornado/Internal/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,42 @@

namespace M6Web\Tornado\Adapter\Tornado\Internal;

use M6Web\Tornado\Promise;

/**
* @internal
* ⚠️ You must NOT rely on this internal implementation
*
* @template TValue
*/
class Task
{
/** @var \Generator */
/** @var \Generator<int, Promise> */
private $generator;
/** @var PendingPromise */
/** @var PendingPromise<TValue> */
private $promise;

/**
* @param \Generator<int, Promise> $generator
* @param PendingPromise<TValue> $pendingPromise
*/
public function __construct(\Generator $generator, PendingPromise $pendingPromise)
{
$this->generator = $generator;
$this->promise = $pendingPromise;
}

/**
* @return PendingPromise<TValue>
*/
public function getPromise(): PendingPromise
{
return $this->promise;
}

/**
* @return \Generator<int, Promise>
*/
public function getGenerator(): \Generator
{
return $this->generator;
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/Tornado/SynchronousEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function deferred(): Deferred
$deferred = new class() implements Deferred {
/** @var SynchronousEventLoop */
public $eventLoop;
/** @var ?Promise */
/** @var ?Promise<mixed> */
private $promise = null;

public function getPromise(): Promise
Expand Down
4 changes: 4 additions & 0 deletions src/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function async(\Generator $generator): Promise;

/**
* Creates a promise that will be resolved with an array of all sub-promises results.
*
* @return Promise<array>
*/
public function promiseAll(Promise ...$promises): Promise;

Expand All @@ -43,6 +45,8 @@ public function promiseAll(Promise ...$promises): Promise;
*
* @param \Traversable<TKey, TValue>|array<TKey, TValue> $traversable Input elements
* @param callable(TValue, TKey): \Generator<int, Promise, mixed, mixed> $function
*
* @return Promise<array>
*/
public function promiseForeach($traversable, callable $function): Promise;

Expand Down
Loading

0 comments on commit 5e9dee5

Please sign in to comment.