From 4028cd9cdfc9329b455e1186af7423dbf5747de6 Mon Sep 17 00:00:00 2001 From: Moritz Friedrich Date: Thu, 31 Aug 2023 15:55:08 +0200 Subject: [PATCH 1/4] Refactor Promise classes with generic types This commit refactors the Promise interface and concrete classes to use generic types. This provides better support for static analysis tools, enforcing type safety and improving code readability. --- src/FulfilledPromise.php | 7 +++++-- src/Promise.php | 11 +++++++---- src/RejectedPromise.php | 3 +++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php index f60f686..def54a8 100644 --- a/src/FulfilledPromise.php +++ b/src/FulfilledPromise.php @@ -6,16 +6,19 @@ * A promise already fulfilled. * * @author Joel Wurtz + * + * @template-covariant T + * @implements Promise */ final class FulfilledPromise implements Promise { /** - * @var mixed + * @var T */ private $result; /** - * @param $result + * @param T $result */ public function __construct($result) { diff --git a/src/Promise.php b/src/Promise.php index 3258ed0..146faba 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -12,6 +12,8 @@ * * @author Joel Wurtz * @author Márk Sági-Kazár + * + * @template-covariant T */ interface Promise { @@ -36,10 +38,11 @@ interface Promise * If you do not care about one of the cases, you can set the corresponding callable to null * The callback will be called when the value arrived and never more than once. * - * @param callable|null $onFulfilled called when a response will be available - * @param callable|null $onRejected called when an exception occurs + * @param callable(T): V|null $onFulfilled called when a response will be available + * @param callable(\Exception): V|null $onRejected called when an exception occurs * - * @return Promise a new resolved promise with value of the executed callback (onFulfilled / onRejected) + * @return Promise a new resolved promise with value of the executed callback (onFulfilled / onRejected) + * @template V */ public function then(callable $onFulfilled = null, callable $onRejected = null); @@ -61,7 +64,7 @@ public function getState(); * * @param bool $unwrap Whether to return resolved value / throw reason or not * - * @return mixed Resolved value, null if $unwrap is set to false + * @return T Resolved value, null if $unwrap is set to false * * @throws \Exception the rejection reason if $unwrap is set to true and the request failed */ diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php index e396a40..4054cd5 100644 --- a/src/RejectedPromise.php +++ b/src/RejectedPromise.php @@ -6,6 +6,9 @@ * A rejected promise. * * @author Joel Wurtz + * + * @template-covariant T + * @implements Promise */ final class RejectedPromise implements Promise { From 26b195f2aaaccc146e13d87c0270908a452a678c Mon Sep 17 00:00:00 2001 From: Moritz Friedrich Date: Thu, 31 Aug 2023 16:22:34 +0200 Subject: [PATCH 2/4] Added generics to changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 479deb8..e5312d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +### Added + +- Generic annotations + ## 1.1.0 - 2020-07-07 ### Added From d723b901e881c2ac6d92b6cb47f1928046600915 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sat, 2 Sep 2023 09:54:19 +0200 Subject: [PATCH 3/4] cleanup configuration of cs --- .styleci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.styleci.yml b/.styleci.yml index 296cab4..56775ca 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -6,5 +6,3 @@ finder: path: - "src" -enabled: - - short_array_syntax From 69683533045fbee215d01cc374dfc192fb231540 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 2 Sep 2023 07:55:09 +0000 Subject: [PATCH 4/4] Apply fixes from StyleCI --- src/RejectedPromise.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php index 4054cd5..ad7bdf5 100644 --- a/src/RejectedPromise.php +++ b/src/RejectedPromise.php @@ -17,9 +17,6 @@ final class RejectedPromise implements Promise */ private $exception; - /** - * @param \Exception $exception - */ public function __construct(\Exception $exception) { $this->exception = $exception;