Skip to content

Commit 12052f0

Browse files
committed
Add documentation for parameter types and return types
1 parent 7097b88 commit 12052f0

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

Diff for: README.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ A trivial implementation of timeouts for `Promise`s, built on top of [ReactPHP](
1818
* [reject()](#reject)
1919
* [Reject cancellation](#reject-cancellation)
2020
* [TimeoutException](#timeoutexception)
21+
* [getTimeout()](#gettimeout)
2122
* [Install](#install)
2223
* [Tests](#tests)
2324
* [License](#license)
@@ -51,8 +52,8 @@ Timer\timeout(…);
5152

5253
### timeout()
5354

54-
The `timeout(PromiseInterface $promise, $time, LoopInterface $loop = null)` function
55-
can be used to *cancel* operations that take *too long*.
55+
The `timeout(PromiseInterface<mixed, Exception|mixed> $promise, float $time, ?LoopInterface $loop = null): PromiseInterface<mixed, TimeoutException|Exception|mixed>` function can be used to
56+
*cancel* operations that take *too long*.
5657
You need to pass in an input `$promise` that represents a pending operation and timeout parameters.
5758
It returns a new `Promise` with the following resolution behavior:
5859

@@ -284,8 +285,8 @@ For more details on the promise primitives, please refer to the
284285

285286
### resolve()
286287

287-
The `resolve($time, LoopInterface $loop = null)` function can be used to create a new Promise that
288-
resolves in `$time` seconds with the `$time` as the fulfillment value.
288+
The `resolve(float $time, ?LoopInterface $loop = null): PromiseInterface<float, RuntimeException>` function can be used to
289+
create a new Promise that resolves in `$time` seconds with the `$time` as the fulfillment value.
289290

290291
```php
291292
React\Promise\Timer\resolve(1.5)->then(function ($time) {
@@ -318,8 +319,8 @@ This will abort the timer and *reject* with a `RuntimeException`.
318319

319320
### reject()
320321

321-
The `reject($time, LoopInterface $loop = null)` function can be used to create a new Promise
322-
which rejects in `$time` seconds with a `TimeoutException`.
322+
The `reject(float $time, ?LoopInterface $loop = null): PromiseInterface<void, TimeoutException|RuntimeException>` function can be used to
323+
create a new Promise which rejects in `$time` seconds with a `TimeoutException`.
323324

324325
```php
325326
React\Promise\Timer\reject(2.0)->then(null, function (React\Promise\Timer\TimeoutException $e) {
@@ -357,7 +358,11 @@ This will abort the timer and *reject* with a `RuntimeException`.
357358

358359
The `TimeoutException` extends PHP's built-in `RuntimeException`.
359360

360-
The `getTimeout()` method can be used to get the timeout value in seconds.
361+
362+
#### getTimeout()
363+
364+
The `getTimeout(): float` method can be used to
365+
get the timeout value in seconds.
361366

362367
## Install
363368

Diff for: src/TimeoutException.php

+10
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66

77
class TimeoutException extends RuntimeException
88
{
9+
/** @var float */
910
private $timeout;
1011

12+
/**
13+
* @param float $timeout
14+
* @param ?string $message
15+
* @param ?int $code
16+
* @param null|\Exception|\Throwable $previous
17+
*/
1118
public function __construct($timeout, $message = null, $code = null, $previous = null)
1219
{
1320
parent::__construct($message, $code, $previous);
1421

1522
$this->timeout = $timeout;
1623
}
1724

25+
/**
26+
* @return float
27+
*/
1828
public function getTimeout()
1929
{
2030
return $this->timeout;

Diff for: src/functions.php

+17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
use React\Promise\Promise;
99
use React\Promise\PromiseInterface;
1010

11+
/**
12+
* @param PromiseInterface<mixed, \Exception|mixed> $promise
13+
* @param float $time
14+
* @param ?LoopInterface $loop
15+
* @return PromiseInterface<mixed, TimeoutException|\Exception|mixed>
16+
*/
1117
function timeout(PromiseInterface $promise, $time, LoopInterface $loop = null)
1218
{
1319
// cancelling this promise will only try to cancel the input promise,
@@ -61,12 +67,18 @@ function timeout(PromiseInterface $promise, $time, LoopInterface $loop = null)
6167
}, $canceller);
6268
}
6369

70+
/**
71+
* @param float $time
72+
* @param ?LoopInterface $loop
73+
* @return PromiseInterface<float, \RuntimeException>
74+
*/
6475
function resolve($time, LoopInterface $loop = null)
6576
{
6677
if ($loop === null) {
6778
$loop = Loop::get();
6879
}
6980

81+
$timer = null;
7082
return new Promise(function ($resolve) use ($loop, $time, &$timer) {
7183
// resolve the promise when the timer fires in $time seconds
7284
$timer = $loop->addTimer($time, function () use ($time, $resolve) {
@@ -82,6 +94,11 @@ function resolve($time, LoopInterface $loop = null)
8294
});
8395
}
8496

97+
/**
98+
* @param float $time
99+
* @param LoopInterface $loop
100+
* @return PromiseInterface<void, TimeoutException|\RuntimeException>
101+
*/
85102
function reject($time, LoopInterface $loop = null)
86103
{
87104
return resolve($time, $loop)->then(function ($time) {

0 commit comments

Comments
 (0)