Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Promise classes with generic types #24

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

### Added

- Generic annotations

## 1.1.0 - 2020-07-07

### Added
Expand Down
7 changes: 5 additions & 2 deletions src/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
* A promise already fulfilled.
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
*
* @template-covariant T
* @implements Promise<T>
*/
final class FulfilledPromise implements Promise
{
/**
* @var mixed
* @var T
*/
private $result;

/**
* @param $result
* @param T $result
*/
public function __construct($result)
{
Expand Down
11 changes: 7 additions & 4 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*
* @template-covariant T
*/
interface Promise
{
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't V also need to be declared in the class annotations?

Copy link
Contributor Author

@Radiergummi Radiergummi Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, V is determined by whatever then returns, which is possibly not related to the promise's original T, so there's an additional @template V on the method comment.
In TypeScript, it would be:

interface Promise<T> {
    then<V>(onFulfilled: (value: T) => V | Promise<V>): Promise<V>;
}

*
* @return Promise a new resolved promise with value of the executed callback (onFulfilled / onRejected)
* @return Promise<V> a new resolved promise with value of the executed callback (onFulfilled / onRejected)
* @template V
*/
public function then(callable $onFulfilled = null, callable $onRejected = null);

Expand All @@ -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
*/
Expand Down
3 changes: 3 additions & 0 deletions src/RejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* A rejected promise.
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
*
* @template-covariant T
* @implements Promise<T>
*/
final class RejectedPromise implements Promise
{
Expand Down
Loading