From 4f3fc6c496e5b5758b5c601077437241f4f397f3 Mon Sep 17 00:00:00 2001 From: Moritz Friedrich Date: Thu, 31 Aug 2023 15:55:08 +0200 Subject: [PATCH 1/3] 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 | 8 +++++++- src/Promise.php | 11 +++++++---- src/RejectedPromise.php | 3 +++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php index b0e1574..def54a8 100644 --- a/src/FulfilledPromise.php +++ b/src/FulfilledPromise.php @@ -6,14 +6,20 @@ * A promise already fulfilled. * * @author Joel Wurtz + * + * @template-covariant T + * @implements Promise */ final class FulfilledPromise implements Promise { /** - * @var mixed + * @var T */ private $result; + /** + * @param T $result + */ public function __construct($result) { $this->result = $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 058d0fd..ad7bdf5 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 9e1e4d96cc1b1465cbc0dcde0de3639995ca8ec3 Mon Sep 17 00:00:00 2001 From: Moritz Friedrich Date: Thu, 31 Aug 2023 16:22:34 +0200 Subject: [PATCH 2/3] 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 465dae0e3c5f4478c8704906523a1a96a29d15ea Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Mon, 23 Oct 2023 10:51:27 +0300 Subject: [PATCH 3/3] Fix style-ci issues --- src/FulfilledPromise.php | 1 + src/Promise.php | 3 ++- src/RejectedPromise.php | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php index def54a8..dce0431 100644 --- a/src/FulfilledPromise.php +++ b/src/FulfilledPromise.php @@ -8,6 +8,7 @@ * @author Joel Wurtz * * @template-covariant T + * * @implements Promise */ final class FulfilledPromise implements Promise diff --git a/src/Promise.php b/src/Promise.php index 146faba..9383726 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -38,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(T): V|null $onFulfilled called when a response will be available + * @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) + * * @template V */ public function then(callable $onFulfilled = null, callable $onRejected = null); diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php index ad7bdf5..2cbefec 100644 --- a/src/RejectedPromise.php +++ b/src/RejectedPromise.php @@ -8,6 +8,7 @@ * @author Joel Wurtz * * @template-covariant T + * * @implements Promise */ final class RejectedPromise implements Promise