Skip to content

Refactor Promise classes with generic types #24

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

Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ finder:
path:
- "src"

enabled:
- short_array_syntax
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure what happened, it looks like instead of rebase you added the commits from master as new commits in the branch. if you can fix it, cool, otherwise i can solve that before merging.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I had some personal stuff coming up last week and didn't get around checking this. Looks like my IDE messed up the rebase, I'm going to fix the issue. Thanks for your patience!

Copy link
Contributor

Choose a reason for hiding this comment

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

no worries, whenver you have time

Copy link
Contributor

Choose a reason for hiding this comment

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

@Radiergummi thank you so much for this change. Might you be in a position to rebase?
This would also really benefit our SDK Generator

Copy link
Contributor

Choose a reason for hiding this comment

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

@dbu thinking of forking @Radiergummi's changes, rebasing and submitting a new PR. Thoughts? Or should we give this more time?

Copy link
Contributor

Choose a reason for hiding this comment

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

glad if you want to do that. you can add the changes in a new commit and then the work of each contributor is still properly attributed.

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 <[email protected]>
*
* @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 <[email protected]>
* @author Márk Sági-Kazár <[email protected]>
*
* @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
6 changes: 3 additions & 3 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 <[email protected]>
*
* @template-covariant T
* @implements Promise<T>
*/
final class RejectedPromise implements Promise
{
Expand All @@ -14,9 +17,6 @@ final class RejectedPromise implements Promise
*/
private $exception;

/**
* @param \Exception $exception
*/
public function __construct(\Exception $exception)
{
$this->exception = $exception;
Expand Down