Skip to content

Commit 505cf5e

Browse files
committed
Require Throwable as rejection reason
Bump minimum required and tested PHP version to 7.0 as a result of requiring Throwables. Renamed the nesecary exceptions, methods and properties to match the new Throwable naming.
1 parent 04e5cfc commit 505cf5e

14 files changed

+53
-92
lines changed

.travis.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
language: php
22

33
php:
4-
- 5.4
5-
- 5.5
6-
- 5.6
74
- 7.0
85
- 7.1
96
- 7.2
7+
- 7.3
108
- nightly # ignore errors, see below
11-
- hhvm # ignore errors, see below
129

1310
# lock distro so new future defaults will not break the build
1411
dist: trusty
1512

1613
matrix:
1714
allow_failures:
18-
- php: hhvm
1915
- php: nightly
2016

2117
install:

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ A lightweight implementation of
1111
> For the code of the current stable 2.x release, checkout the
1212
> [2.x branch](https://github.com/reactphp/promise/tree/2.x).
1313
14+
> The upcoming 3.0 release will be the way forward for this package.
15+
> However we will still actively support 2.0 and 1.0 for those not yet
16+
> on PHP 7+.
17+
1418
Table of Contents
1519
-----------------
1620

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
{"name": "Jan Sorgalla", "email": "[email protected]"}
77
],
88
"require": {
9-
"php": ">=5.4.0"
9+
"php": ">=7.0.0"
1010
},
1111
"require-dev": {
12-
"phpunit/phpunit": "~4.8.35 || ~5.7 || ~6.4"
12+
"phpunit/phpunit": "~6.4"
1313
},
1414
"autoload": {
1515
"psr-4": {

src/Exception/CompositeException.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@
1111
*/
1212
class CompositeException extends \Exception
1313
{
14-
private $exceptions;
14+
private $throwables;
1515

16-
public function __construct(array $exceptions, $message = '', $code = 0, $previous = null)
16+
public function __construct(array $throwables, $message = '', $code = 0, $previous = null)
1717
{
1818
parent::__construct($message, $code, $previous);
1919

20-
$this->exceptions = $exceptions;
20+
$this->throwables = $throwables;
2121
}
2222

2323
/**
24-
* @return \Throwable[]|\Exception[]
24+
* @return \Throwable[]
2525
*/
26-
public function getExceptions()
26+
public function getThrowables()
2727
{
28-
return $this->exceptions;
28+
return $this->throwables;
2929
}
3030
}

src/FulfilledPromise.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
2727
$resolve($onFulfilled($this->value));
2828
} catch (\Throwable $exception) {
2929
$reject($exception);
30-
} catch (\Exception $exception) {
31-
$reject($exception);
3230
}
3331
});
3432
});
@@ -45,8 +43,6 @@ public function done(callable $onFulfilled = null, callable $onRejected = null)
4543
$result = $onFulfilled($this->value);
4644
} catch (\Throwable $exception) {
4745
return fatalError($exception);
48-
} catch (\Exception $exception) {
49-
return fatalError($exception);
5046
}
5147

5248
if ($result instanceof PromiseInterface) {

src/Internal/CancellationQueue.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ private function drain()
4343
try {
4444
$cancellable->cancel();
4545
} catch (\Throwable $exception) {
46-
} catch (\Exception $exception) {
4746
}
4847

4948
unset($this->queue[$i]);

src/Internal/Queue.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ private function drain()
2626
try {
2727
$task();
2828
} catch (\Throwable $exception) {
29-
} catch (\Exception $exception) {
3029
}
3130

3231
unset($this->queue[$i]);

src/Promise.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,6 @@ function ($reason) {
205205
}
206206
} catch (\Throwable $e) {
207207
$this->reject($e);
208-
} catch (\Exception $e) {
209-
$this->reject($e);
210208
}
211209
}
212210
}

src/RejectedPromise.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,8 @@ final class RejectedPromise implements PromiseInterface
66
{
77
private $reason;
88

9-
public function __construct($reason)
9+
public function __construct(\Throwable $reason)
1010
{
11-
if (!$reason instanceof \Throwable && !$reason instanceof \Exception) {
12-
throw new \InvalidArgumentException(
13-
sprintf(
14-
'A Promise must be rejected with a \Throwable or \Exception instance, got "%s" instead.',
15-
is_object($reason) ? get_class($reason) : gettype($reason)
16-
)
17-
18-
);
19-
}
20-
2111
$this->reason = $reason;
2212
}
2313

@@ -33,8 +23,6 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
3323
$resolve($onRejected($this->reason));
3424
} catch (\Throwable $exception) {
3525
$reject($exception);
36-
} catch (\Exception $exception) {
37-
$reject($exception);
3826
}
3927
});
4028
});
@@ -51,8 +39,6 @@ public function done(callable $onFulfilled = null, callable $onRejected = null)
5139
$result = $onRejected($this->reason);
5240
} catch (\Throwable $exception) {
5341
return fatalError($exception);
54-
} catch (\Exception $exception) {
55-
return fatalError($exception);
5642
}
5743

5844
if ($result instanceof self) {

src/functions.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ function resolve($promiseOrValue = null)
5353
* throwing an exception. For example, it allows you to propagate a rejection with
5454
* the value of another promise.
5555
*
56-
* @param mixed $promiseOrValue
56+
* @param \Throwable $promiseOrValue
5757
* @return PromiseInterface
5858
*/
59-
function reject($reason)
59+
function reject(\Throwable $reason)
6060
{
6161
return new RejectedPromise($reason);
6262
}
@@ -318,9 +318,6 @@ function fatalError($error)
318318
} catch (\Throwable $e) {
319319
\set_error_handler(null);
320320
\trigger_error($error, E_USER_ERROR);
321-
} catch (\Exception $e) {
322-
\set_error_handler(null);
323-
\trigger_error($error, E_USER_ERROR);
324321
}
325322
}
326323

tests/FunctionRejectTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,4 @@ public function shouldRejectAnException()
1818
reject($exception)
1919
->then($this->expectCallableNever(), $mock);
2020
}
21-
22-
/**
23-
* @test
24-
* @expectedException \InvalidArgumentException
25-
*/
26-
public function shouldThrowWhenCalledWithANonException()
27-
{
28-
reject(1);
29-
}
3021
}

tests/FunctionalRejectTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace React\Promise;
4+
5+
use stdClass;
6+
7+
class FunctionalRejectTest extends TestCase
8+
{
9+
public function nonThrowables()
10+
{
11+
yield '1' => [1];
12+
yield 'true' => [true];
13+
yield 'stdClass' => [new stdClass()];
14+
}
15+
16+
/**
17+
* @test
18+
* @dataProvider nonThrowables
19+
*/
20+
public function shouldThrowWhenCalledWithANonException($input)
21+
{
22+
$errorCollector = new ErrorCollector();
23+
$errorCollector->start();
24+
25+
(new Promise(function ($_, $reject) use ($input) {
26+
$reject($input);
27+
}))->done($this->expectCallableNever());
28+
29+
$errors = $errorCollector->stop();
30+
31+
$this->assertEquals(E_USER_ERROR, $errors[0]['errno']);
32+
$this->assertContains(
33+
'TypeError: Argument 1 passed to React\Promise\reject() must implement interface Throwable',
34+
$errors[0]['errstr']
35+
);
36+
}
37+
}

tests/PromiseTest/RejectTestTrait.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,6 @@ public function rejectShouldRejectWithAnException()
3131
$adapter->reject($exception);
3232
}
3333

34-
/**
35-
* @test
36-
* @expectedException \InvalidArgumentException
37-
*/
38-
public function rejectShouldThrowWhenCalledWithAnImmediateValue()
39-
{
40-
$adapter = $this->getPromiseTestAdapter();
41-
42-
$adapter->reject(1);
43-
}
44-
45-
/**
46-
* @test
47-
* @expectedException \InvalidArgumentException
48-
*/
49-
public function rejectShouldThrowWhenCalledWithAFulfilledPromise()
50-
{
51-
$adapter = $this->getPromiseTestAdapter();
52-
53-
$adapter->reject(Promise\resolve(1));
54-
}
55-
56-
/**
57-
* @test
58-
* @expectedException \InvalidArgumentException
59-
*/
60-
public function rejectShouldThrowWhenCalledWithARejectedPromise()
61-
{
62-
$adapter = $this->getPromiseTestAdapter();
63-
64-
$adapter->reject(Promise\reject(1));
65-
}
66-
6734
/** @test */
6835
public function rejectShouldForwardReasonWhenCallbackIsNull()
6936
{

tests/RejectedPromiseTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,4 @@ public function getPromiseTestAdapter(callable $canceller = null)
4040
},
4141
]);
4242
}
43-
44-
/**
45-
* @test
46-
* @expectedException \InvalidArgumentException
47-
*/
48-
public function shouldThrowExceptionIfConstructedWithANonException()
49-
{
50-
return new RejectedPromise('foo');
51-
}
5243
}

0 commit comments

Comments
 (0)