Skip to content

Commit 4f3fc6c

Browse files
RadiergummiNdiritu
authored andcommitted
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.
1 parent cac94eb commit 4f3fc6c

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/FulfilledPromise.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66
* A promise already fulfilled.
77
*
88
* @author Joel Wurtz <joel.wurtz@gmail.com>
9+
*
10+
* @template-covariant T
11+
* @implements Promise<T>
912
*/
1013
final class FulfilledPromise implements Promise
1114
{
1215
/**
13-
* @var mixed
16+
* @var T
1417
*/
1518
private $result;
1619

20+
/**
21+
* @param T $result
22+
*/
1723
public function __construct($result)
1824
{
1925
$this->result = $result;

src/Promise.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*
1313
* @author Joel Wurtz <joel.wurtz@gmail.com>
1414
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
15+
*
16+
* @template-covariant T
1517
*/
1618
interface Promise
1719
{
@@ -36,10 +38,11 @@ interface Promise
3638
* If you do not care about one of the cases, you can set the corresponding callable to null
3739
* The callback will be called when the value arrived and never more than once.
3840
*
39-
* @param callable|null $onFulfilled called when a response will be available
40-
* @param callable|null $onRejected called when an exception occurs
41+
* @param callable(T): V|null $onFulfilled called when a response will be available
42+
* @param callable(\Exception): V|null $onRejected called when an exception occurs
4143
*
42-
* @return Promise a new resolved promise with value of the executed callback (onFulfilled / onRejected)
44+
* @return Promise<V> a new resolved promise with value of the executed callback (onFulfilled / onRejected)
45+
* @template V
4346
*/
4447
public function then(callable $onFulfilled = null, callable $onRejected = null);
4548

@@ -61,7 +64,7 @@ public function getState();
6164
*
6265
* @param bool $unwrap Whether to return resolved value / throw reason or not
6366
*
64-
* @return mixed Resolved value, null if $unwrap is set to false
67+
* @return T Resolved value, null if $unwrap is set to false
6568
*
6669
* @throws \Exception the rejection reason if $unwrap is set to true and the request failed
6770
*/

src/RejectedPromise.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* A rejected promise.
77
*
88
* @author Joel Wurtz <joel.wurtz@gmail.com>
9+
*
10+
* @template-covariant T
11+
* @implements Promise<T>
912
*/
1013
final class RejectedPromise implements Promise
1114
{

0 commit comments

Comments
 (0)