From 4c8f13b2bce3d99eaef6dad81621458f89904770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Alfaiate?= Date: Mon, 2 Nov 2026 10:13:34 +0700 Subject: [PATCH] Add backward-compatible generic annotations --- CHANGELOG.md | 4 ++++ src/FulfilledPromise.php | 11 +++++++++-- src/Promise.php | 6 ++++-- src/RejectedPromise.php | 5 +++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5bf747..d061bef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +### Added + +- Backward-compatible generic annotations for resolved values. + ## 1.3.1 - 2024-03-15 - Made nullable parameter types explicit (PHP 8.4 compatibility) diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php index ed77d46..d50f14f 100644 --- a/src/FulfilledPromise.php +++ b/src/FulfilledPromise.php @@ -6,22 +6,29 @@ * A promise already fulfilled. * * @author Joel Wurtz + * + * @template-covariant T = mixed + * + * @implements Promise */ final class FulfilledPromise implements Promise { /** - * @var mixed + * @var T */ private $result; /** - * @param mixed $result + * @param T $result */ public function __construct($result) { $this->result = $result; } + /** + * @return ($onFulfilled is null ? self : Promise) + */ public function then(?callable $onFulfilled = null, ?callable $onRejected = null) { if (null === $onFulfilled) { diff --git a/src/Promise.php b/src/Promise.php index e240311..5d63e0a 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 = mixed */ interface Promise { @@ -39,7 +41,7 @@ interface Promise * @param callable|null $onFulfilled called when a response will be available * @param callable|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) */ public function then(?callable $onFulfilled = null, ?callable $onRejected = null); @@ -61,7 +63,7 @@ public function getState(); * * @param bool $unwrap Whether to return resolved value / throw reason or not * - * @return ($unwrap is true ? mixed : null) Resolved value, null if $unwrap is set to false + * @return ($unwrap is true ? T : null) Resolved value, null if $unwrap is set to false * * @throws \Throwable the rejection reason if $unwrap is set to true and the request failed */ diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php index 8c046ce..f6dfbff 100644 --- a/src/RejectedPromise.php +++ b/src/RejectedPromise.php @@ -6,6 +6,8 @@ * A rejected promise. * * @author Joel Wurtz + * + * @implements Promise */ final class RejectedPromise implements Promise { @@ -19,6 +21,9 @@ public function __construct(\Throwable $exception) $this->exception = $exception; } + /** + * @return ($onRejected is null ? self : Promise) + */ public function then(?callable $onFulfilled = null, ?callable $onRejected = null) { if (null === $onRejected) {