Skip to content

Refactor Promise classes with generic types (fast-tracking https://github.com/php-http/promise/pull/24) #27

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

Merged
merged 3 commits into from
Oct 24, 2023
Merged
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
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
9 changes: 8 additions & 1 deletion src/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
* 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 T $result
*/
public function __construct($result)
{
$this->result = $result;
Expand Down
12 changes: 8 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,12 @@ 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
*
* @return Promise<V> 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)
* @template V
*/
public function then(callable $onFulfilled = null, callable $onRejected = null);

Expand All @@ -61,7 +65,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
4 changes: 4 additions & 0 deletions src/RejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* A rejected promise.
*
* @author Joel Wurtz <[email protected]>
*
* @template-covariant T
*
* @implements Promise<T>
*/
final class RejectedPromise implements Promise
{
Expand Down