Skip to content

Commit a57e4d4

Browse files
authored
Merge pull request #11 from clue-labs/php7.1
Require PHP 7.1+ and add type declarations
2 parents ab03f4d + 3711b0e commit a57e4d4

File tree

7 files changed

+42
-55
lines changed

7 files changed

+42
-55
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ jobs:
1717
- 7.3
1818
- 7.2
1919
- 7.1
20-
- 7.0
21-
- 5.6
22-
- 5.5
23-
- 5.4
24-
- 5.3
2520
steps:
2621
- uses: actions/checkout@v2
2722
- uses: shivammathur/setup-php@v2

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ Once the promise is fulfilled, this function will return whatever the promise
7575
resolved to.
7676

7777
Once the promise is rejected, this will throw whatever the promise rejected
78-
with. If the promise did not reject with an `Exception` or `Throwable` (PHP 7+),
79-
then this function will throw an `UnexpectedValueException` instead.
78+
with. If the promise did not reject with an `Exception` or `Throwable`, then
79+
this function will throw an `UnexpectedValueException` instead.
8080

8181
```php
8282
try {
@@ -222,9 +222,15 @@ $ composer require react/async:dev-main
222222
See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
223223

224224
This project aims to run on any platform and thus does not require any PHP
225-
extensions and supports running on legacy PHP 5.3 through current PHP 8+.
225+
extensions and supports running on PHP 7.1 through current PHP 8+.
226226
It's *highly recommended to use the latest supported PHP version* for this project.
227227

228+
We're committed to providing long-term support (LTS) options and to provide a
229+
smooth upgrade path. If you're using an older PHP version, you may use the
230+
[`2.x` branch](https://github.com/reactphp/async/tree/2.x) which provides a
231+
compatible API but does not take advantage of newer language features. You may
232+
target both versions at the same time to support a wider range of PHP versions.
233+
228234
## Tests
229235

230236
To run the test suite, you first need to clone this repo and then install all

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
}
2727
],
2828
"require": {
29-
"php": ">=5.3.2",
29+
"php": ">=7.1",
3030
"react/event-loop": "^1.2",
3131
"react/promise": "^2.8 || ^1.2.1"
3232
},
3333
"require-dev": {
34-
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
34+
"phpunit/phpunit": "^9.3 || ^7.5"
3535
},
3636
"autoload": {
3737
"files": [

phpunit.xml.legacy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<!-- PHPUnit configuration file with old format for PHPUnit 9.2 or older -->
44
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/4.8/phpunit.xsd"
5+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
66
bootstrap="vendor/autoload.php"
77
colors="true">
88
<testsuites>

src/functions.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
* resolved to.
2929
*
3030
* Once the promise is rejected, this will throw whatever the promise rejected
31-
* with. If the promise did not reject with an `Exception` or `Throwable` (PHP 7+),
32-
* then this function will throw an `UnexpectedValueException` instead.
31+
* with. If the promise did not reject with an `Exception` or `Throwable`, then
32+
* this function will throw an `UnexpectedValueException` instead.
3333
*
3434
* ```php
3535
* try {
@@ -45,7 +45,7 @@
4545
* @param PromiseInterface $promise
4646
* @return mixed returns whatever the promise resolves to
4747
* @throws \Exception when the promise is rejected with an `Exception`
48-
* @throws \Throwable when the promise is rejected with a `Throwable` (PHP 7+)
48+
* @throws \Throwable when the promise is rejected with a `Throwable`
4949
* @throws \UnexpectedValueException when the promise is rejected with an unexpected value (Promise API v1 or v2 only)
5050
*/
5151
function await(PromiseInterface $promise)
@@ -79,7 +79,7 @@ function ($error) use (&$exception, &$rejected, &$wait) {
7979

8080
if ($rejected) {
8181
// promise is rejected with an unexpected value (Promise API v1 or v2 only)
82-
if (!$exception instanceof \Exception && !$exception instanceof \Throwable) {
82+
if (!$exception instanceof \Throwable) {
8383
$exception = new \UnexpectedValueException(
8484
'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception))
8585
);
@@ -95,18 +95,18 @@ function ($error) use (&$exception, &$rejected, &$wait) {
9595
* @param array<callable():PromiseInterface<mixed,Exception>> $tasks
9696
* @return PromiseInterface<array<mixed>,Exception>
9797
*/
98-
function parallel(array $tasks)
98+
function parallel(array $tasks): PromiseInterface
9999
{
100-
$pending = array();
100+
$pending = [];
101101
$deferred = new Deferred(function () use (&$pending) {
102102
foreach ($pending as $promise) {
103103
if ($promise instanceof CancellablePromiseInterface) {
104104
$promise->cancel();
105105
}
106106
}
107-
$pending = array();
107+
$pending = [];
108108
});
109-
$results = array();
109+
$results = [];
110110
$errored = false;
111111

112112
$numTasks = count($tasks);
@@ -123,7 +123,7 @@ function parallel(array $tasks)
123123
$promise->cancel();
124124
}
125125
}
126-
$pending = array();
126+
$pending = [];
127127
};
128128

129129
foreach ($tasks as $i => $task) {
@@ -153,7 +153,7 @@ function parallel(array $tasks)
153153
* @param array<callable():PromiseInterface<mixed,Exception>> $tasks
154154
* @return PromiseInterface<array<mixed>,Exception>
155155
*/
156-
function series(array $tasks)
156+
function series(array $tasks): PromiseInterface
157157
{
158158
$pending = null;
159159
$deferred = new Deferred(function () use (&$pending) {
@@ -162,7 +162,7 @@ function series(array $tasks)
162162
}
163163
$pending = null;
164164
});
165-
$results = array();
165+
$results = [];
166166

167167
/** @var callable():void $next */
168168
$taskCallback = function ($result) use (&$results, &$next) {
@@ -193,7 +193,7 @@ function series(array $tasks)
193193
* @param array<callable(mixed=):PromiseInterface<mixed,Exception>> $tasks
194194
* @return PromiseInterface<mixed,Exception>
195195
*/
196-
function waterfall(array $tasks)
196+
function waterfall(array $tasks): PromiseInterface
197197
{
198198
$pending = null;
199199
$deferred = new Deferred(function () use (&$pending) {

tests/AwaitTest.php

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public function testAwaitThrowsExceptionWhenPromiseIsRejectedWithException()
1414
throw new \Exception('test');
1515
});
1616

17-
$this->setExpectedException('Exception', 'test');
17+
$this->expectException(\Exception::class);
18+
$this->expectExceptionMessage('test');
1819
React\Async\await($promise);
1920
}
2021

@@ -28,7 +29,8 @@ public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWith
2829
$reject(false);
2930
});
3031

31-
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type bool');
32+
$this->expectException(\UnexpectedValueException::class);
33+
$this->expectExceptionMessage('Promise rejected with unexpected value of type bool');
3234
React\Async\await($promise);
3335
}
3436

@@ -42,20 +44,20 @@ public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWith
4244
$reject(null);
4345
});
4446

45-
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type NULL');
47+
$this->expectException(\UnexpectedValueException::class);
48+
$this->expectExceptionMessage('Promise rejected with unexpected value of type NULL');
4649
React\Async\await($promise);
4750
}
4851

49-
/**
50-
* @requires PHP 7
51-
*/
5252
public function testAwaitThrowsErrorWhenPromiseIsRejectedWithError()
5353
{
5454
$promise = new Promise(function ($_, $reject) {
5555
throw new \Error('Test', 42);
5656
});
5757

58-
$this->setExpectedException('Error', 'Test', 42);
58+
$this->expectException(\Error::class);
59+
$this->expectExceptionMessage('Test');
60+
$this->expectExceptionCode(42);
5961
React\Async\await($promise);
6062
}
6163

@@ -84,8 +86,8 @@ public function testAwaitReturnsValueWhenPromiseIsFulfilledEvenWhenOtherTimerSto
8486

8587
public function testAwaitShouldNotCreateAnyGarbageReferencesForResolvedPromise()
8688
{
87-
if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) {
88-
$this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+');
89+
if (class_exists('React\Promise\When')) {
90+
$this->markTestSkipped('Not supported on legacy Promise v1 API');
8991
}
9092

9193
gc_collect_cycles();
@@ -126,8 +128,8 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWi
126128
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
127129
}
128130

129-
if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) {
130-
$this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+');
131+
if (class_exists('React\Promise\When')) {
132+
$this->markTestSkipped('Not supported on legacy Promise v1 API');
131133
}
132134

133135
gc_collect_cycles();
@@ -144,21 +146,4 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWi
144146

145147
$this->assertEquals(0, gc_collect_cycles());
146148
}
147-
148-
public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
149-
{
150-
if (method_exists($this, 'expectException')) {
151-
// PHPUnit 5+
152-
$this->expectException($exception);
153-
if ($exceptionMessage !== '') {
154-
$this->expectExceptionMessage($exceptionMessage);
155-
}
156-
if ($exceptionCode !== null) {
157-
$this->expectExceptionCode($exceptionCode);
158-
}
159-
} else {
160-
// legacy PHPUnit 4
161-
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
162-
}
163-
}
164149
}

tests/TestCase.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace React\Tests\Async;
44

5+
use PHPUnit\Framework\MockObject\MockBuilder;
56
use PHPUnit\Framework\TestCase as BaseTestCase;
67

78
class TestCase extends BaseTestCase
@@ -39,12 +40,12 @@ protected function expectCallableNever()
3940

4041
protected function createCallableMock()
4142
{
42-
if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'addMethods')) {
43+
if (method_exists(MockBuilder::class, 'addMethods')) {
4344
// PHPUnit 9+
44-
return $this->getMockBuilder('stdClass')->addMethods(array('__invoke'))->getMock();
45+
return $this->getMockBuilder(\stdClass::class)->addMethods(['__invoke'])->getMock();
4546
} else {
46-
// legacy PHPUnit 4 - PHPUnit 8
47-
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
47+
// PHPUnit < 9
48+
return $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock();
4849
}
4950
}
5051
}

0 commit comments

Comments
 (0)