Skip to content

make Promise use template for exception #32

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
wants to merge 1 commit into from
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
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ jobs:
- name: Checkout code
uses: actions/checkout@v3

- name: Remove phpspec
run: composer remove --dev friends-of-phpspec/phpspec-code-coverage phpspec/phpspec

- name: PHPStan
uses: OskarStark/[email protected]
env:
Expand Down
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Change Log

## 1.2.1
## 1.2.2 - unreleased

### Added - 2023-11-08
### Fixed

- Changed `Promise` to use a template for the exception class that is allowed.

## 1.2.1 - 2023-11-08

### Fixed

- Fixed PHPDoc for `wait()` and `then()`'s `onRejected` callable

Expand Down
15 changes: 3 additions & 12 deletions src/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @template-covariant T
*
* @implements Promise<T>
* @implements Promise<T, \Throwable>
*/
final class FulfilledPromise implements Promise
{
Expand All @@ -26,9 +26,6 @@ public function __construct($result)
$this->result = $result;
}

/**
* {@inheritdoc}
*/
public function then(callable $onFulfilled = null, callable $onRejected = null)
{
if (null === $onFulfilled) {
Expand All @@ -37,28 +34,22 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)

try {
return new self($onFulfilled($this->result));
} catch (\Exception $e) {
} catch (\Throwable $e) {
return new RejectedPromise($e);
}
}

/**
* {@inheritdoc}
*/
public function getState()
{
return Promise::FULFILLED;
}

/**
* {@inheritdoc}
*/
public function wait($unwrap = true)
{
if ($unwrap) {
return $this->result;
}

return;
return null;
}
}
10 changes: 6 additions & 4 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* @author Márk Sági-Kazár <[email protected]>
*
* @template-covariant T
*
* @template E of \Throwable
*/
interface Promise
{
Expand All @@ -38,10 +40,10 @@ 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(T): V|null $onFulfilled called when a response will be available
* @param callable(\Throwable): V|null $onRejected called when an exception occurs
* @param callable(T): V|null $onFulfilled called when a response will be available
* @param callable(E): 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<V, E> a new resolved promise with value of the executed callback (onFulfilled / onRejected)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we have T, V and E - how do we correctly specify this?

*
* @template V
*/
Expand All @@ -67,7 +69,7 @@ public function getState();
*
* @return ($unwrap is true ? T : null) Resolved value, null if $unwrap is set to false
*
* @throws \Exception the rejection reason if $unwrap is set to true and the request failed
* @throws E the rejection reason if $unwrap is set to true and the request failed
*/
public function wait($unwrap = true);
}
19 changes: 5 additions & 14 deletions src/RejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,20 @@
*
* @template-covariant T
*
* @implements Promise<T>
* @implements Promise<T, \Throwable>
*/
final class RejectedPromise implements Promise
{
/**
* @var \Exception
* @var \Throwable
*/
private $exception;

public function __construct(\Exception $exception)
public function __construct(\Throwable $exception)
{
$this->exception = $exception;
}

/**
* {@inheritdoc}
*/
public function then(callable $onFulfilled = null, callable $onRejected = null)
{
if (null === $onRejected) {
Expand All @@ -34,28 +31,22 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)

try {
return new FulfilledPromise($onRejected($this->exception));
} catch (\Exception $e) {
} catch (\Throwable $e) {
return new self($e);
}
}

/**
* {@inheritdoc}
*/
public function getState()
{
return Promise::REJECTED;
}

/**
* {@inheritdoc}
*/
public function wait($unwrap = true)
{
if ($unwrap) {
throw $this->exception;
}

return;
return null;
}
}