diff --git a/.travis.yml b/.travis.yml index e9007358..10f12797 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,21 +1,17 @@ language: php php: - - 5.4 - - 5.5 - - 5.6 - 7.0 - 7.1 - 7.2 + - 7.3 - nightly # ignore errors, see below - - hhvm # ignore errors, see below # lock distro so new future defaults will not break the build dist: trusty matrix: allow_failures: - - php: hhvm - php: nightly install: diff --git a/README.md b/README.md index 8ebec879..728c42d1 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,10 @@ A lightweight implementation of > For the code of the current stable 2.x release, checkout the > [2.x branch](https://github.com/reactphp/promise/tree/2.x). +> The upcoming 3.0 release will be the way forward for this package. +> However we will still actively support 2.0 and 1.0 for those not yet +> on PHP 7+. + Table of Contents ----------------- diff --git a/composer.json b/composer.json index 23ca679b..02f958b6 100644 --- a/composer.json +++ b/composer.json @@ -6,10 +6,10 @@ {"name": "Jan Sorgalla", "email": "jsorgalla@gmail.com"} ], "require": { - "php": ">=5.4.0" + "php": ">=7.0.0" }, "require-dev": { - "phpunit/phpunit": "~4.8.35 || ~5.7 || ~6.4" + "phpunit/phpunit": "~6.4" }, "autoload": { "psr-4": { diff --git a/src/Exception/CompositeException.php b/src/Exception/CompositeException.php index 45af2430..1fdbab54 100644 --- a/src/Exception/CompositeException.php +++ b/src/Exception/CompositeException.php @@ -11,20 +11,20 @@ */ class CompositeException extends \Exception { - private $exceptions; + private $throwables; - public function __construct(array $exceptions, $message = '', $code = 0, $previous = null) + public function __construct(array $throwables, $message = '', $code = 0, $previous = null) { parent::__construct($message, $code, $previous); - $this->exceptions = $exceptions; + $this->throwables = $throwables; } /** - * @return \Throwable[]|\Exception[] + * @return \Throwable[] */ - public function getExceptions() + public function getThrowables() { - return $this->exceptions; + return $this->throwables; } } diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php index 2bfaf5b3..ecd09ba7 100644 --- a/src/FulfilledPromise.php +++ b/src/FulfilledPromise.php @@ -27,8 +27,6 @@ public function then(callable $onFulfilled = null, callable $onRejected = null) $resolve($onFulfilled($this->value)); } catch (\Throwable $exception) { $reject($exception); - } catch (\Exception $exception) { - $reject($exception); } }); }); @@ -45,8 +43,6 @@ public function done(callable $onFulfilled = null, callable $onRejected = null) $result = $onFulfilled($this->value); } catch (\Throwable $exception) { return fatalError($exception); - } catch (\Exception $exception) { - return fatalError($exception); } if ($result instanceof PromiseInterface) { diff --git a/src/Internal/CancellationQueue.php b/src/Internal/CancellationQueue.php index 4012aea5..af22c68e 100644 --- a/src/Internal/CancellationQueue.php +++ b/src/Internal/CancellationQueue.php @@ -43,7 +43,6 @@ private function drain() try { $cancellable->cancel(); } catch (\Throwable $exception) { - } catch (\Exception $exception) { } unset($this->queue[$i]); diff --git a/src/Internal/Queue.php b/src/Internal/Queue.php index bb8389f6..6b4bfe0b 100644 --- a/src/Internal/Queue.php +++ b/src/Internal/Queue.php @@ -26,7 +26,6 @@ private function drain() try { $task(); } catch (\Throwable $exception) { - } catch (\Exception $exception) { } unset($this->queue[$i]); diff --git a/src/Promise.php b/src/Promise.php index bfff877f..0d75bde3 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -205,8 +205,6 @@ function ($reason) { } } catch (\Throwable $e) { $this->reject($e); - } catch (\Exception $e) { - $this->reject($e); } } } diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php index 638f463a..bfa85655 100644 --- a/src/RejectedPromise.php +++ b/src/RejectedPromise.php @@ -6,18 +6,8 @@ final class RejectedPromise implements PromiseInterface { private $reason; - public function __construct($reason) + public function __construct(\Throwable $reason) { - if (!$reason instanceof \Throwable && !$reason instanceof \Exception) { - throw new \InvalidArgumentException( - sprintf( - 'A Promise must be rejected with a \Throwable or \Exception instance, got "%s" instead.', - is_object($reason) ? get_class($reason) : gettype($reason) - ) - - ); - } - $this->reason = $reason; } @@ -33,8 +23,6 @@ public function then(callable $onFulfilled = null, callable $onRejected = null) $resolve($onRejected($this->reason)); } catch (\Throwable $exception) { $reject($exception); - } catch (\Exception $exception) { - $reject($exception); } }); }); @@ -51,8 +39,6 @@ public function done(callable $onFulfilled = null, callable $onRejected = null) $result = $onRejected($this->reason); } catch (\Throwable $exception) { return fatalError($exception); - } catch (\Exception $exception) { - return fatalError($exception); } if ($result instanceof self) { diff --git a/src/functions.php b/src/functions.php index d6b03f6d..dea44bf0 100644 --- a/src/functions.php +++ b/src/functions.php @@ -53,10 +53,10 @@ function resolve($promiseOrValue = null) * throwing an exception. For example, it allows you to propagate a rejection with * the value of another promise. * - * @param mixed $promiseOrValue + * @param \Throwable $promiseOrValue * @return PromiseInterface */ -function reject($reason) +function reject(\Throwable $reason) { return new RejectedPromise($reason); } @@ -318,9 +318,6 @@ function fatalError($error) } catch (\Throwable $e) { \set_error_handler(null); \trigger_error($error, E_USER_ERROR); - } catch (\Exception $e) { - \set_error_handler(null); - \trigger_error($error, E_USER_ERROR); } } diff --git a/tests/FunctionRejectTest.php b/tests/FunctionRejectTest.php index 47bf2b44..887d3c34 100644 --- a/tests/FunctionRejectTest.php +++ b/tests/FunctionRejectTest.php @@ -18,13 +18,4 @@ public function shouldRejectAnException() reject($exception) ->then($this->expectCallableNever(), $mock); } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function shouldThrowWhenCalledWithANonException() - { - reject(1); - } } diff --git a/tests/FunctionalRejectTest.php b/tests/FunctionalRejectTest.php new file mode 100644 index 00000000..d303d7a4 --- /dev/null +++ b/tests/FunctionalRejectTest.php @@ -0,0 +1,37 @@ + [1]; + yield 'true' => [true]; + yield 'stdClass' => [new stdClass()]; + } + + /** + * @test + * @dataProvider nonThrowables + */ + public function shouldThrowWhenCalledWithANonException($input) + { + $errorCollector = new ErrorCollector(); + $errorCollector->start(); + + (new Promise(function ($_, $reject) use ($input) { + $reject($input); + }))->done($this->expectCallableNever()); + + $errors = $errorCollector->stop(); + + $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); + $this->assertContains( + 'TypeError: Argument 1 passed to React\Promise\reject() must implement interface Throwable', + $errors[0]['errstr'] + ); + } +} diff --git a/tests/PromiseTest/RejectTestTrait.php b/tests/PromiseTest/RejectTestTrait.php index ed9c6b11..9afd2be9 100644 --- a/tests/PromiseTest/RejectTestTrait.php +++ b/tests/PromiseTest/RejectTestTrait.php @@ -31,39 +31,6 @@ public function rejectShouldRejectWithAnException() $adapter->reject($exception); } - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function rejectShouldThrowWhenCalledWithAnImmediateValue() - { - $adapter = $this->getPromiseTestAdapter(); - - $adapter->reject(1); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function rejectShouldThrowWhenCalledWithAFulfilledPromise() - { - $adapter = $this->getPromiseTestAdapter(); - - $adapter->reject(Promise\resolve(1)); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function rejectShouldThrowWhenCalledWithARejectedPromise() - { - $adapter = $this->getPromiseTestAdapter(); - - $adapter->reject(Promise\reject(1)); - } - /** @test */ public function rejectShouldForwardReasonWhenCallbackIsNull() { diff --git a/tests/RejectedPromiseTest.php b/tests/RejectedPromiseTest.php index ddf018a5..8cffe12f 100644 --- a/tests/RejectedPromiseTest.php +++ b/tests/RejectedPromiseTest.php @@ -40,13 +40,4 @@ public function getPromiseTestAdapter(callable $canceller = null) }, ]); } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function shouldThrowExceptionIfConstructedWithANonException() - { - return new RejectedPromise('foo'); - } }