From dbb2364e06f50b764582992b9fe73c770b632888 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Mon, 7 Aug 2023 11:29:29 +0200 Subject: [PATCH] Add support for react/promise v3 With v3 come a whole list of [changes](https://github.com/reactphp/promise/releases/tag/v3.0.0) including end of promise chain detection, the removal of the `CancellablePromiseInterface` interface, and type templating. --- composer.json | 2 +- phpstan.neon.dist | 8 ++++++++ src/React/Promise.php | 6 +++--- test/Rx/Functional/Promise/FromPromiseTest.php | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 phpstan.neon.dist diff --git a/composer.json b/composer.json index df3eb4bf..79921091 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ ], "require": { "php": ">=7.0.0", - "react/promise": "~2.2" + "react/promise": "^3 || ~2.2" }, "require-dev": { "satooshi/php-coveralls": "~1.0", diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 00000000..18ad3d5e --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,8 @@ +parameters: + level: max + + paths: + - test/types/ + + fileExtensions: + - php \ No newline at end of file diff --git a/src/React/Promise.php b/src/React/Promise.php index c8efacfc..bd384191 100644 --- a/src/React/Promise.php +++ b/src/React/Promise.php @@ -2,7 +2,6 @@ namespace Rx\React; -use React\Promise\CancellablePromiseInterface; use React\Promise\Promise as ReactPromise; use React\Promise\PromiseInterface; use Rx\Disposable\CallbackDisposable; @@ -11,6 +10,7 @@ use Rx\Observable\AnonymousObservable; use Rx\Subject\AsyncSubject; use React\Promise\Deferred; +use Throwable; final class Promise { @@ -32,7 +32,7 @@ public static function resolved($value): ReactPromise public static function rejected($exception): ReactPromise { $d = new Deferred(); - $d->reject($exception); + $d->reject($exception instanceof Throwable ? $exception : new RejectedPromiseException($exception)); return $d->promise(); } @@ -94,7 +94,7 @@ function ($error) use ($subject) { $disp = $subject->subscribe($observer); return new CallbackDisposable(function () use ($p, $disp) { $disp->dispose(); - if ($p instanceof CancellablePromiseInterface) { + if (\method_exists($p, 'cancel')) { $p->cancel(); } }); diff --git a/test/Rx/Functional/Promise/FromPromiseTest.php b/test/Rx/Functional/Promise/FromPromiseTest.php index a3be98da..16483c8e 100644 --- a/test/Rx/Functional/Promise/FromPromiseTest.php +++ b/test/Rx/Functional/Promise/FromPromiseTest.php @@ -40,7 +40,7 @@ function () { */ public function from_promise_failure() { - $p = \React\Promise\reject('error'); + $p = \React\Promise\reject(new RejectedPromiseException('error')); $source = Observable::fromPromise($p);