Skip to content

Commit 277e9a5

Browse files
authored
Merge pull request #235 from clue-labs/template-types
Use Promise v3 template types
2 parents 3b10f71 + 0057af2 commit 277e9a5

12 files changed

+51
-37
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"phpstan/phpstan": "1.10.16 || 1.4.10",
2323
"phpunit/phpunit": "^9.6 || ^7.5",
2424
"psr/container": "^2 || ^1",
25-
"react/promise-timer": "^1.9"
25+
"react/promise-timer": "^1.10"
2626
},
2727
"autoload": {
2828
"psr-4": {

src/AccessLogHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function __invoke(ServerRequestInterface $request, callable $next)
3737
$response = $next($request);
3838

3939
if ($response instanceof PromiseInterface) {
40+
/** @var PromiseInterface<ResponseInterface> $response */
4041
return $response->then(function (ResponseInterface $response) use ($request, $now) {
4142
$this->logWhenClosed($request, $response, $now);
4243
return $response;

src/App.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ private function handleRequest(ServerRequestInterface $request)
271271
return $response;
272272
}
273273

274+
/**
275+
* @return PromiseInterface<ResponseInterface>
276+
*/
274277
private function coroutine(\Generator $generator): PromiseInterface
275278
{
276279
$next = null;

src/ErrorHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ public function __invoke(ServerRequestInterface $request, callable $next)
4949
}
5050
}, function ($e) {
5151
// Promise rejected, always a `\Throwable` as of Promise v3
52-
assert($e instanceof \Throwable || !\method_exists(PromiseInterface::class, 'catch'));
52+
assert($e instanceof \Throwable || !\method_exists(PromiseInterface::class, 'catch')); // @phpstan-ignore-line
5353

5454
if ($e instanceof \Throwable) {
5555
return $this->errorInvalidException($e);
56-
} else {
56+
} else { // @phpstan-ignore-line
57+
// @phpstan-ignore-next-line
5758
return $this->errorInvalidResponse(\React\Promise\reject($e)); // @codeCoverageIgnore
5859
}
5960
});

src/Io/FiberHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ public function __invoke(ServerRequestInterface $request, callable $next)
4444

4545
// if the next request handler returns immediately, the fiber can terminate immediately without using a Deferred
4646
// if the next request handler suspends the fiber, we only reach this point after resuming the fiber, so the code below will have assigned a Deferred
47-
/** @var ?Deferred $deferred */
47+
/** @var ?Deferred<ResponseInterface> $deferred */
4848
if ($deferred !== null) {
49+
assert($response instanceof ResponseInterface);
4950
$deferred->resolve($response);
5051
}
5152

@@ -56,7 +57,7 @@ public function __invoke(ServerRequestInterface $request, callable $next)
5657
$fiber->start();
5758
if ($fiber->isTerminated()) {
5859
/** @throws void because fiber is known to have terminated successfully */
59-
/** @var ResponseInterface|PromiseInterface|\Generator */
60+
/** @var ResponseInterface|PromiseInterface<ResponseInterface>|\Generator */
6061
return $fiber->getReturn();
6162
}
6263

src/Io/SapiHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function run(callable $handler): void
3434
if ($response instanceof ResponseInterface) {
3535
$this->sendResponse($response);
3636
} elseif ($response instanceof PromiseInterface) {
37+
/** @var PromiseInterface<ResponseInterface> $response */
3738
$response->then(function (ResponseInterface $response): void {
3839
$this->sendResponse($response);
3940
});

tests/AppMiddlewareTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ public function testMiddlewareCallsNextReturnsDeferredResponseModifiedInMiddlewa
280280
$middleware = function (ServerRequestInterface $request, callable $next) {
281281
$promise = $next($request);
282282
assert($promise instanceof PromiseInterface);
283+
/** @var PromiseInterface<ResponseInterface> $promise */
283284

284285
return $promise->then(function (ResponseInterface $response) {
285286
return $response->withHeader('Content-Type', 'text/html');
@@ -303,7 +304,7 @@ public function testMiddlewareCallsNextReturnsDeferredResponseModifiedInMiddlewa
303304
$ref->setAccessible(true);
304305
$promise = $ref->invoke($app, $request);
305306

306-
/** @var PromiseInterface $promise */
307+
/** @var PromiseInterface<ResponseInterface> $promise */
307308
$this->assertInstanceOf(PromiseInterface::class, $promise);
308309

309310
$response = null;
@@ -350,7 +351,7 @@ public function testMiddlewareCallsNextReturnsCoroutineResponseModifiedInMiddlew
350351
$ref->setAccessible(true);
351352
$promise = $ref->invoke($app, $request);
352353

353-
/** @var PromiseInterface $promise */
354+
/** @var PromiseInterface<ResponseInterface> $promise */
354355
$this->assertInstanceOf(PromiseInterface::class, $promise);
355356

356357
$response = null;
@@ -680,7 +681,7 @@ public function testGlobalMiddlewareReturnsPromiseWhichResolvesWithResponseWitho
680681
$ref->setAccessible(true);
681682
$promise = $ref->invoke($app, $request);
682683

683-
/** @var PromiseInterface $promise */
684+
/** @var PromiseInterface<ResponseInterface> $promise */
684685
$this->assertInstanceOf(PromiseInterface::class, $promise);
685686

686687
$response = null;
@@ -720,7 +721,7 @@ public function testGlobalMiddlewareCallsNextReturnsPromiseWhichResolvesWithModi
720721
$ref->setAccessible(true);
721722
$promise = $ref->invoke($app, $request);
722723

723-
/** @var PromiseInterface $promise */
724+
/** @var PromiseInterface<ResponseInterface> $promise */
724725
$this->assertInstanceOf(PromiseInterface::class, $promise);
725726

726727
$response = null;
@@ -764,7 +765,7 @@ public function testGlobalMiddlewareCallsNextReturnsPromiseWhichResolvesWithModi
764765
$ref->setAccessible(true);
765766
$promise = $ref->invoke($app, $request);
766767

767-
/** @var PromiseInterface $promise */
768+
/** @var PromiseInterface<ResponseInterface> $promise */
768769
$this->assertInstanceOf(PromiseInterface::class, $promise);
769770

770771
$response = null;

tests/AppTest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ public function testHandleRequestWithMatchingRouteReturnsPromiseWhichFulfillsWit
10521052
$ref->setAccessible(true);
10531053
$promise = $ref->invoke($app, $request);
10541054

1055-
/** @var PromiseInterface $promise */
1055+
/** @var PromiseInterface<ResponseInterface> $promise */
10561056
$this->assertInstanceOf(PromiseInterface::class, $promise);
10571057

10581058
$response = null;
@@ -1082,7 +1082,7 @@ public function testHandleRequestWithMatchingRouteReturnsPendingPromiseWhenHandl
10821082
$ref->setAccessible(true);
10831083
$promise = $ref->invoke($app, $request);
10841084

1085-
/** @var PromiseInterface $promise */
1085+
/** @var PromiseInterface<ResponseInterface> $promise */
10861086
$this->assertInstanceOf(PromiseInterface::class, $promise);
10871087

10881088
$resolved = false;
@@ -1150,7 +1150,7 @@ public function testHandleRequestWithMatchingRouteReturnsPromiseWhichFulfillsWit
11501150
$ref->setAccessible(true);
11511151
$promise = $ref->invoke($app, $request);
11521152

1153-
/** @var PromiseInterface $promise */
1153+
/** @var PromiseInterface<ResponseInterface> $promise */
11541154
$this->assertInstanceOf(PromiseInterface::class, $promise);
11551155

11561156
$response = null;
@@ -1193,7 +1193,7 @@ public function testHandleRequestWithMatchingRouteReturnsPromiseWhichFulfillsWit
11931193
$ref->setAccessible(true);
11941194
$promise = $ref->invoke($app, $request);
11951195

1196-
/** @var PromiseInterface $promise */
1196+
/** @var PromiseInterface<ResponseInterface> $promise */
11971197
$this->assertInstanceOf(PromiseInterface::class, $promise);
11981198

11991199
$response = null;
@@ -1223,7 +1223,7 @@ public function testHandleRequestWithMatchingRouteReturnsPendingPromiseWhenHandl
12231223
$ref->setAccessible(true);
12241224
$promise = $ref->invoke($app, $request);
12251225

1226-
/** @var PromiseInterface $promise */
1226+
/** @var PromiseInterface<never> $promise */
12271227
$this->assertInstanceOf(PromiseInterface::class, $promise);
12281228

12291229
$resolved = false;
@@ -1285,7 +1285,7 @@ public function testHandleRequestWithMatchingRouteReturnsPromiseResolvingWithRes
12851285
$ref->setAccessible(true);
12861286
$promise = $ref->invoke($app, $request);
12871287

1288-
/** @var PromiseInterface $promise */
1288+
/** @var PromiseInterface<ResponseInterface> $promise */
12891289
$this->assertInstanceOf(PromiseInterface::class, $promise);
12901290

12911291
$response = null;
@@ -1384,7 +1384,7 @@ public function testHandleRequestWithMatchingRouteReturnsPromiseWhichFulfillsWit
13841384
$ref->setAccessible(true);
13851385
$promise = $ref->invoke($app, $request);
13861386

1387-
/** @var PromiseInterface $promise */
1387+
/** @var PromiseInterface<ResponseInterface> $promise */
13881388
$this->assertInstanceOf(PromiseInterface::class, $promise);
13891389

13901390
$response = null;
@@ -1412,7 +1412,7 @@ public function testHandleRequestWithMatchingRouteReturnsPromiseWhichFulfillsWit
14121412
$app = $this->createAppWithoutLogger();
14131413

14141414
$app->get('/users', function () {
1415-
return reject(null);
1415+
return reject(null); // @phpstan-ignore-line
14161416
});
14171417

14181418
$request = new ServerRequest('GET', 'http://localhost/users');
@@ -1422,7 +1422,7 @@ public function testHandleRequestWithMatchingRouteReturnsPromiseWhichFulfillsWit
14221422
$ref->setAccessible(true);
14231423
$promise = $ref->invoke($app, $request);
14241424

1425-
/** @var PromiseInterface $promise */
1425+
/** @var PromiseInterface<ResponseInterface> $promise */
14261426
$this->assertInstanceOf(PromiseInterface::class, $promise);
14271427

14281428
$response = null;
@@ -1457,7 +1457,7 @@ public function testHandleRequestWithMatchingRouteReturnsPromiseWhichFulfillsWit
14571457
$ref->setAccessible(true);
14581458
$promise = $ref->invoke($app, $request);
14591459

1460-
/** @var PromiseInterface $promise */
1460+
/** @var PromiseInterface<ResponseInterface> $promise */
14611461
$this->assertInstanceOf(PromiseInterface::class, $promise);
14621462

14631463
$response = null;
@@ -1523,7 +1523,7 @@ public function testHandleRequestWithMatchingRouteReturnsPromiseWhichFulfillsWit
15231523
$ref->setAccessible(true);
15241524
$promise = $ref->invoke($app, $request);
15251525

1526-
/** @var PromiseInterface $promise */
1526+
/** @var PromiseInterface<ResponseInterface> $promise */
15271527
$this->assertInstanceOf(PromiseInterface::class, $promise);
15281528

15291529
$response = null;
@@ -1593,7 +1593,7 @@ public function testHandleRequestWithMatchingRouteReturnsPromiseWhichFulfillsWit
15931593
$ref->setAccessible(true);
15941594
$promise = $ref->invoke($app, $request);
15951595

1596-
/** @var PromiseInterface $promise */
1596+
/** @var PromiseInterface<ResponseInterface> $promise */
15971597
$this->assertInstanceOf(PromiseInterface::class, $promise);
15981598

15991599
$response = null;
@@ -1632,7 +1632,7 @@ public function testHandleRequestWithMatchingRouteReturnsPromiseWhichFulfillsWit
16321632
$ref->setAccessible(true);
16331633
$promise = $ref->invoke($app, $request);
16341634

1635-
/** @var PromiseInterface $promise */
1635+
/** @var PromiseInterface<ResponseInterface> $promise */
16361636
$this->assertInstanceOf(PromiseInterface::class, $promise);
16371637

16381638
$response = null;
@@ -1889,7 +1889,7 @@ public function testHandleRequestWithMatchingRouteReturnsPromiseWhichFulfillsWit
18891889
$ref->setAccessible(true);
18901890
$promise = $ref->invoke($app, $request);
18911891

1892-
/** @var PromiseInterface $promise */
1892+
/** @var PromiseInterface<ResponseInterface> $promise */
18931893
$this->assertInstanceOf(PromiseInterface::class, $promise);
18941894

18951895
$response = null;
@@ -1924,7 +1924,7 @@ public function testHandleRequestWithMatchingRouteReturnsInternalServerErrorResp
19241924
$ref->setAccessible(true);
19251925
$promise = $ref->invoke($app, $request);
19261926

1927-
/** @var PromiseInterface $promise */
1927+
/** @var PromiseInterface<ResponseInterface> $promise */
19281928
$this->assertInstanceOf(PromiseInterface::class, $promise);
19291929

19301930
$response = null;

tests/ErrorHandlerTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testInvokeWithHandlerReturningPromiseResolvingWithResponseReturn
3434

3535
$promise = $handler($request, function () use ($response) { return resolve($response); });
3636

37-
/** @var PromiseInterface $promise */
37+
/** @var PromiseInterface<ResponseInterface> $promise */
3838
$this->assertInstanceOf(PromiseInterface::class, $promise);
3939

4040
$ret = null;
@@ -106,7 +106,7 @@ public function testInvokeWithHandlerReturningGeneratorYieldingRejectedPromiseIn
106106
$this->assertInstanceOf(\Generator::class, $generator);
107107
$promise = $generator->current();
108108

109-
/** @var PromiseInterface $promise */
109+
/** @var PromiseInterface<never> $promise */
110110
$this->assertInstanceOf(PromiseInterface::class, $promise);
111111
$e = null;
112112
$promise->then(null, function ($reason) use (&$e) {
@@ -146,7 +146,7 @@ public function testInvokeWithHandlerReturningPromiseRejectingWithExceptionRetur
146146
return reject(new \RuntimeException());
147147
});
148148

149-
/** @var PromiseInterface $promise */
149+
/** @var PromiseInterface<ResponseInterface> $promise */
150150
$this->assertInstanceOf(PromiseInterface::class, $promise);
151151

152152
$response = null;
@@ -216,7 +216,7 @@ public function testInvokeWithHandlerReturningGeneratorYieldingPromiseRejectingW
216216
$this->assertInstanceOf(\Generator::class, $generator);
217217
$promise = $generator->current();
218218

219-
/** @var PromiseInterface $promise */
219+
/** @var PromiseInterface<never> $promise */
220220
$this->assertInstanceOf(PromiseInterface::class, $promise);
221221
$e = null;
222222
$promise->then(null, function ($reason) use (&$e) {
@@ -258,7 +258,7 @@ public function testInvokeWithHandlerReturningPromiseResolvingWithNullReturnsPro
258258
return resolve(null);
259259
});
260260

261-
/** @var PromiseInterface $promise */
261+
/** @var PromiseInterface<ResponseInterface> $promise */
262262
$this->assertInstanceOf(PromiseInterface::class, $promise);
263263

264264
$response = null;
@@ -282,10 +282,10 @@ public function testInvokeWithHandlerReturningPromiseRejectingWithNullReturnsPro
282282
$request = new ServerRequest('GET', 'http://example.com/');
283283

284284
$promise = $handler($request, function () {
285-
return reject(null);
285+
return reject(null); // @phpstan-ignore-line
286286
});
287287

288-
/** @var PromiseInterface $promise */
288+
/** @var PromiseInterface<ResponseInterface> $promise */
289289
$this->assertInstanceOf(PromiseInterface::class, $promise);
290290

291291
$response = null;

tests/Io/FiberHandlerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testInvokeWithHandlerReturningPromiseResolvingWithResponseReturn
3535

3636
$promise = $handler($request, function () use ($response) { return resolve($response); });
3737

38-
/** @var PromiseInterface $promise */
38+
/** @var PromiseInterface<Response> $promise */
3939
$this->assertInstanceOf(PromiseInterface::class, $promise);
4040

4141
$ret = null;
@@ -157,7 +157,7 @@ public function testInvokeWithHandlerReturningResponseAfterAwaitingPendingPromis
157157
return await($deferred->promise());
158158
});
159159

160-
/** @var PromiseInterface $promise */
160+
/** @var PromiseInterface<Response> $promise */
161161
$this->assertInstanceOf(PromiseInterface::class, $promise);
162162

163163
$ret = null;

0 commit comments

Comments
 (0)