Skip to content

Commit c25665f

Browse files
authored
Merge pull request #27 from Ndiritu/Radiergummi/add-generic-annotations
Refactor Promise classes with generic types (fast-tracking #24)
2 parents cac94eb + 465dae0 commit c25665f

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
### Added
4+
5+
- Generic annotations
6+
37
## 1.1.0 - 2020-07-07
48

59
### Added

src/FulfilledPromise.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66
* A promise already fulfilled.
77
*
88
* @author Joel Wurtz <joel.wurtz@gmail.com>
9+
*
10+
* @template-covariant T
11+
*
12+
* @implements Promise<T>
913
*/
1014
final class FulfilledPromise implements Promise
1115
{
1216
/**
13-
* @var mixed
17+
* @var T
1418
*/
1519
private $result;
1620

21+
/**
22+
* @param T $result
23+
*/
1724
public function __construct($result)
1825
{
1926
$this->result = $result;

src/Promise.php

Lines changed: 8 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,12 @@ 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
43+
*
44+
* @return Promise<V> a new resolved promise with value of the executed callback (onFulfilled / onRejected)
4145
*
42-
* @return Promise a new resolved promise with value of the executed callback (onFulfilled / onRejected)
46+
* @template V
4347
*/
4448
public function then(callable $onFulfilled = null, callable $onRejected = null);
4549

@@ -61,7 +65,7 @@ public function getState();
6165
*
6266
* @param bool $unwrap Whether to return resolved value / throw reason or not
6367
*
64-
* @return mixed Resolved value, null if $unwrap is set to false
68+
* @return T Resolved value, null if $unwrap is set to false
6569
*
6670
* @throws \Exception the rejection reason if $unwrap is set to true and the request failed
6771
*/

src/RejectedPromise.php

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

0 commit comments

Comments
 (0)