Skip to content

Commit 4874a14

Browse files
committed
make Promise use template for exception
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.
1 parent 44a67cb commit 4874a14

File tree

5 files changed

+25
-32
lines changed

5 files changed

+25
-32
lines changed

.github/workflows/tests.yml

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ jobs:
6969
- name: Checkout code
7070
uses: actions/checkout@v3
7171

72+
- name: Remove phpspec
73+
run: composer remove --dev friends-of-phpspec/phpspec-code-coverage phpspec/phpspec
74+
7275
- name: PHPStan
7376
uses: OskarStark/[email protected]
7477
env:

CHANGELOG.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# Change Log
22

3-
## 1.2.1
3+
## 1.2.2 - unreleased
44

5-
### Added - 2023-11-08
5+
### Fixed
6+
7+
- Changed `Promise` to use a template for the exception class that is allowed.
8+
9+
## 1.2.1 - 2023-11-08
10+
11+
### Fixed
612

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

src/FulfilledPromise.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @template-covariant T
1111
*
12-
* @implements Promise<T>
12+
* @implements Promise<T, \Throwable>
1313
*/
1414
final class FulfilledPromise implements Promise
1515
{
@@ -26,9 +26,6 @@ public function __construct($result)
2626
$this->result = $result;
2727
}
2828

29-
/**
30-
* {@inheritdoc}
31-
*/
3229
public function then(callable $onFulfilled = null, callable $onRejected = null)
3330
{
3431
if (null === $onFulfilled) {
@@ -37,28 +34,22 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
3734

3835
try {
3936
return new self($onFulfilled($this->result));
40-
} catch (\Exception $e) {
37+
} catch (\Throwable $e) {
4138
return new RejectedPromise($e);
4239
}
4340
}
4441

45-
/**
46-
* {@inheritdoc}
47-
*/
4842
public function getState()
4943
{
5044
return Promise::FULFILLED;
5145
}
5246

53-
/**
54-
* {@inheritdoc}
55-
*/
5647
public function wait($unwrap = true)
5748
{
5849
if ($unwrap) {
5950
return $this->result;
6051
}
6152

62-
return;
53+
return null;
6354
}
6455
}

src/Promise.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* @author Márk Sági-Kazár <[email protected]>
1515
*
1616
* @template-covariant T
17+
*
18+
* @template E of \Throwable
1719
*/
1820
interface Promise
1921
{
@@ -38,10 +40,10 @@ interface Promise
3840
* If you do not care about one of the cases, you can set the corresponding callable to null
3941
* The callback will be called when the value arrived and never more than once.
4042
*
41-
* @param callable(T): V|null $onFulfilled called when a response will be available
42-
* @param callable(\Throwable): V|null $onRejected called when an exception occurs
43+
* @param callable(T): V|null $onFulfilled called when a response will be available
44+
* @param callable(E): V|null $onRejected called when an exception occurs
4345
*
44-
* @return Promise<V> a new resolved promise with value of the executed callback (onFulfilled / onRejected)
46+
* @return Promise<V, E> a new resolved promise with value of the executed callback (onFulfilled / onRejected)
4547
*
4648
* @template V
4749
*/
@@ -67,7 +69,7 @@ public function getState();
6769
*
6870
* @return ($unwrap is true ? T : null) Resolved value, null if $unwrap is set to false
6971
*
70-
* @throws \Exception the rejection reason if $unwrap is set to true and the request failed
72+
* @throws E the rejection reason if $unwrap is set to true and the request failed
7173
*/
7274
public function wait($unwrap = true);
7375
}

src/RejectedPromise.php

+5-14
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,20 @@
99
*
1010
* @template-covariant T
1111
*
12-
* @implements Promise<T>
12+
* @implements Promise<T, \Throwable>
1313
*/
1414
final class RejectedPromise implements Promise
1515
{
1616
/**
17-
* @var \Exception
17+
* @var \Throwable
1818
*/
1919
private $exception;
2020

21-
public function __construct(\Exception $exception)
21+
public function __construct(\Throwable $exception)
2222
{
2323
$this->exception = $exception;
2424
}
2525

26-
/**
27-
* {@inheritdoc}
28-
*/
2926
public function then(callable $onFulfilled = null, callable $onRejected = null)
3027
{
3128
if (null === $onRejected) {
@@ -34,28 +31,22 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
3431

3532
try {
3633
return new FulfilledPromise($onRejected($this->exception));
37-
} catch (\Exception $e) {
34+
} catch (\Throwable $e) {
3835
return new self($e);
3936
}
4037
}
4138

42-
/**
43-
* {@inheritdoc}
44-
*/
4539
public function getState()
4640
{
4741
return Promise::REJECTED;
4842
}
4943

50-
/**
51-
* {@inheritdoc}
52-
*/
5344
public function wait($unwrap = true)
5445
{
5546
if ($unwrap) {
5647
throw $this->exception;
5748
}
5849

59-
return;
50+
return null;
6051
}
6152
}

0 commit comments

Comments
 (0)