Skip to content
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

Enforce Throwable rejection reason and require PHP 7+ as a consequence #138

Merged
merged 1 commit into from
May 9, 2019
Merged
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
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
12 changes: 6 additions & 6 deletions src/Exception/CompositeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
WyriHaximus marked this conversation as resolved.
Show resolved Hide resolved
*/
public function getExceptions()
public function getThrowables()
{
return $this->exceptions;
return $this->throwables;
}
}
4 changes: 0 additions & 4 deletions src/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
});
Expand All @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/Internal/CancellationQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ private function drain()
try {
$cancellable->cancel();
} catch (\Throwable $exception) {
} catch (\Exception $exception) {
}

unset($this->queue[$i]);
Expand Down
1 change: 0 additions & 1 deletion src/Internal/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ private function drain()
try {
$task();
} catch (\Throwable $exception) {
} catch (\Exception $exception) {
}

unset($this->queue[$i]);
Expand Down
2 changes: 0 additions & 2 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ function ($reason) {
}
} catch (\Throwable $e) {
$this->reject($e);
} catch (\Exception $e) {
$this->reject($e);
}
}
}
16 changes: 1 addition & 15 deletions src/RejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
});
});
Expand All @@ -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) {
Expand Down
7 changes: 2 additions & 5 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
9 changes: 0 additions & 9 deletions tests/FunctionRejectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,4 @@ public function shouldRejectAnException()
reject($exception)
->then($this->expectCallableNever(), $mock);
}

/**
* @test
* @expectedException \InvalidArgumentException
*/
public function shouldThrowWhenCalledWithANonException()
{
reject(1);
}
}
37 changes: 37 additions & 0 deletions tests/FunctionalRejectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace React\Promise;

use stdClass;

class FunctionalRejectTest extends TestCase
{
public function nonThrowables()
{
yield '1' => [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']
);
}
}
33 changes: 0 additions & 33 deletions tests/PromiseTest/RejectTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
9 changes: 0 additions & 9 deletions tests/RejectedPromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,4 @@ public function getPromiseTestAdapter(callable $canceller = null)
},
]);
}

/**
* @test
* @expectedException \InvalidArgumentException
*/
public function shouldThrowExceptionIfConstructedWithANonException()
{
return new RejectedPromise('foo');
}
}