Skip to content

Commit

Permalink
make Promise use template for exception
Browse files Browse the repository at this point in the history
in httplug, we have documentation text that formulates that only
specific exceptions may be used for the callback. the change in 1.2.0 to
allow any Throwable was incorrect.
  • Loading branch information
dbu committed Jan 3, 2024
1 parent 44a67cb commit 4874a14
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
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)
*
* @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;
}
}

0 comments on commit 4874a14

Please sign in to comment.