diff --git a/.github/workflows/code-quality.yaml b/.github/workflows/code-quality.yaml new file mode 100644 index 0000000..e23def5 --- /dev/null +++ b/.github/workflows/code-quality.yaml @@ -0,0 +1,39 @@ +name: Code Quality + +on: + push: + branches: + - master + pull_request: + +jobs: + code-quality: + name: Code Quality + runs-on: ubuntu-latest + + strategy: + matrix: + php: [ '7.2', '7.3' ] + + steps: + - name: 'Init repository' + uses: actions/checkout@v2 + + - name: 'Setup PHP' + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: xsl, swoole + tools: composer + + - name: "Install dependencies" + run: composer install + + - name: 'Tests unit' + run: composer tests-unit + + - name: 'Tests examples' + run: composer tests-examples + + - name: 'Static analysis' + run: composer static-analysis diff --git a/composer.json b/composer.json index dec075e..6309488 100644 --- a/composer.json +++ b/composer.json @@ -51,10 +51,10 @@ "bin-dir": "bin/" }, "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", - "code-style-check": "./bin/php-cs-fixer fix --dry-run --verbose", - "code-style-fix": "./bin/php-cs-fixer fix" + "tests-unit": "@php ./bin/phpunit --testsuite='Tornado Test Suite'", + "tests-examples": "@php ./bin/phpunit --testsuite='Tornado Examples'", + "static-analysis": "@php ./bin/phpstan analyse src tests --level=7 --no-progress -vvv", + "code-style-check": "@php ./bin/php-cs-fixer fix --dry-run --verbose", + "code-style-fix": "@php ./bin/php-cs-fixer fix" } } diff --git a/src/Adapter/Amp/EventLoop.php b/src/Adapter/Amp/EventLoop.php index c20bc7f..5668fa3 100644 --- a/src/Adapter/Amp/EventLoop.php +++ b/src/Adapter/Amp/EventLoop.php @@ -11,7 +11,7 @@ class EventLoop implements \M6Web\Tornado\EventLoop /** * {@inheritdoc} */ - public function wait(Promise $promise): mixed + public function wait(Promise $promise) { try { $result = \Amp\Promise\wait( diff --git a/src/Adapter/ReactPhp/EventLoop.php b/src/Adapter/ReactPhp/EventLoop.php index bd3e785..be89fa6 100644 --- a/src/Adapter/ReactPhp/EventLoop.php +++ b/src/Adapter/ReactPhp/EventLoop.php @@ -23,7 +23,7 @@ public function __construct(\React\EventLoop\LoopInterface $reactEventLoop) /** * {@inheritdoc} */ - public function wait(Promise $promise): mixed + public function wait(Promise $promise) { $value = null; $isRejected = false; diff --git a/src/Adapter/Swoole/EventLoop.php b/src/Adapter/Swoole/EventLoop.php index 8fe33a2..d21078d 100644 --- a/src/Adapter/Swoole/EventLoop.php +++ b/src/Adapter/Swoole/EventLoop.php @@ -57,10 +57,10 @@ public function __destruct() //Event::exit(); } - private function shiftCoroutine(): mixed + private function shiftCoroutine(): void { if (count($this->cids) === 0) { - return null; + return; } $cid = array_shift($this->cids); @@ -70,17 +70,14 @@ private function shiftCoroutine(): mixed } else { $this->shiftCoroutine(); } - - return $cid; } - private function pushCoroutine(): mixed + private function pushCoroutine(): void { $cid = Coroutine::getCid(); $this->cids[] = $cid; Coroutine::yield(); - return $cid; } private function createPromise(): DummyPromise @@ -144,7 +141,7 @@ private function isPending(DummyPromise $promise): bool /** * {@inheritdoc} */ - public function wait(Promise $promise): mixed + public function wait(Promise $promise) { $promise = DummyPromise::wrap($promise); diff --git a/src/Adapter/Swoole/Internal/DummyPromise.php b/src/Adapter/Swoole/Internal/DummyPromise.php index f0be824..5d3be27 100644 --- a/src/Adapter/Swoole/Internal/DummyPromise.php +++ b/src/Adapter/Swoole/Internal/DummyPromise.php @@ -8,9 +8,24 @@ final class DummyPromise implements Promise, Deferred { - private bool $isPending; - private mixed $value; - private ?Throwable $exception; + /** + * @var bool + */ + private $isPending; + + /** + * @var mixed + */ + private $value; + + /** + * @var Throwable|null + */ + private $exception; + + /** + * @var callable[] + */ private $callbacks; public function __construct(?callable $callback = null) @@ -43,6 +58,7 @@ public function resolve($value): void $this->isPending = false; $this->value = $value; + foreach ($this->callbacks as $callback) { ($callback)($this); } diff --git a/src/Adapter/Swoole/PromiseEventLoop.php b/src/Adapter/Swoole/PromiseEventLoop.php index 0351531..748ebc8 100644 --- a/src/Adapter/Swoole/PromiseEventLoop.php +++ b/src/Adapter/Swoole/PromiseEventLoop.php @@ -33,7 +33,7 @@ public function __construct() /** * {@inheritdoc} */ - public function wait(Promise $promise): mixed + public function wait(Promise $promise) { $value = null; $isRejected = false; diff --git a/src/Adapter/Swoole/YieldEventLoop.php b/src/Adapter/Swoole/YieldEventLoop.php index b6632ad..99c6300 100644 --- a/src/Adapter/Swoole/YieldEventLoop.php +++ b/src/Adapter/Swoole/YieldEventLoop.php @@ -23,7 +23,7 @@ public function __construct() /** * {@inheritdoc} */ - public function wait(Promise $promise): mixed + public function wait(Promise $promise) { $value = null; $this->async((static function () use ($promise, &$value): \Generator { diff --git a/src/Adapter/Tornado/EventLoop.php b/src/Adapter/Tornado/EventLoop.php index 753a4a3..546c5d3 100644 --- a/src/Adapter/Tornado/EventLoop.php +++ b/src/Adapter/Tornado/EventLoop.php @@ -26,7 +26,7 @@ public function __construct() /** * {@inheritdoc} */ - public function wait(Promise $promise): mixed + public function wait(Promise $promise) { $promiseIsPending = true; $finalAction = function () {throw new \Error('Impossible to resolve the promise, no more task to execute..'); }; diff --git a/src/Adapter/Tornado/SynchronousEventLoop.php b/src/Adapter/Tornado/SynchronousEventLoop.php index 91a7852..21cd4f8 100644 --- a/src/Adapter/Tornado/SynchronousEventLoop.php +++ b/src/Adapter/Tornado/SynchronousEventLoop.php @@ -13,7 +13,7 @@ class SynchronousEventLoop implements \M6Web\Tornado\EventLoop /** * {@inheritdoc} */ - public function wait(Promise $promise): mixed + public function wait(Promise $promise) { // If there are some uncaught exceptions, throw the first one. if ($throwable = reset($this->asyncThrowables)) { diff --git a/src/EventLoop.php b/src/EventLoop.php index 0f31af1..d93d742 100644 --- a/src/EventLoop.php +++ b/src/EventLoop.php @@ -7,8 +7,10 @@ interface EventLoop /** * Waits the resolution of a promise, and returns its value. * You should use this function once for your global result. + * + * @return mixed */ - public function wait(Promise $promise): mixed; + public function wait(Promise $promise); /** * Registers a generator in the event loop to execute it asynchronously.