Skip to content

Commit b84e117

Browse files
committed
Use Promise v3 template types
1 parent a39fa70 commit b84e117

File tree

8 files changed

+36
-29
lines changed

8 files changed

+36
-29
lines changed

.github/workflows/ci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ jobs:
2525
php-version: ${{ matrix.php }}
2626
coverage: xdebug
2727
ini-file: development
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2830
- run: composer install
2931
- run: docker run --net=host -d redis
3032
- run: REDIS_URI=localhost:6379 vendor/bin/phpunit --coverage-text --coverage-clover=clover.xml
@@ -57,5 +59,7 @@ jobs:
5759
with:
5860
php-version: ${{ matrix.php }}
5961
coverage: none
62+
env:
63+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6064
- run: composer install
6165
- run: vendor/bin/phpstan

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ given event loop instance.
421421

422422
#### __call()
423423

424-
The `__call(string $name, string[] $args): PromiseInterface<mixed,Exception>` method can be used to
424+
The `__call(string $name, string[] $args): PromiseInterface<mixed>` method can be used to
425425
invoke the given command.
426426

427427
This is a magic method that will be invoked when calling any Redis command on this instance.

composer.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
1717
"react/event-loop": "^1.2",
1818
"react/promise": "^3 || ^2.0 || ^1.1",
19-
"react/promise-timer": "^1.9",
19+
"react/promise-timer": "dev-template-types as 1.10.0",
2020
"react/socket": "^1.12"
2121
},
2222
"require-dev": {
@@ -29,5 +29,11 @@
2929
},
3030
"autoload-dev": {
3131
"psr-4": { "Clue\\Tests\\React\\Redis\\": "tests/" }
32+
},
33+
"repositories": {
34+
"clue": {
35+
"type": "vcs",
36+
"url": "https://github.com/clue-labs/promise-timer"
37+
}
3238
}
3339
}

phpstan.neon.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ parameters:
88

99
reportUnmatchedIgnoredErrors: false
1010
ignoreErrors:
11-
# ignore generic usage like `PromiseInterface<T>` until fixed upstream
11+
# ignore generic usage like `PromiseInterface<T>` for Promise v2/v1
1212
- '/^PHPDoc tag @return contains generic type React\\Promise\\PromiseInterface<.+> but interface React\\Promise\\PromiseInterface is not generic\.$/'
1313
# ignore undefined methods due to magic `__call()` method
1414
- '/^Call to an undefined method Clue\\React\\Redis\\RedisClient::.+\(\)\.$/'

src/Io/Factory.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(LoopInterface $loop = null, ConnectorInterface $conn
4444
* Create Redis client connected to address of given redis instance
4545
*
4646
* @param string $uri Redis server URI to connect to
47-
* @return PromiseInterface<StreamingClient,\Exception> Promise that will
47+
* @return PromiseInterface<StreamingClient> Promise that will
4848
* be fulfilled with `StreamingClient` on success or rejects with `\Exception` on error.
4949
*/
5050
public function createClient(string $uri): PromiseInterface
@@ -79,6 +79,8 @@ public function createClient(string $uri): PromiseInterface
7979
$authority = 'unix://' . substr($parts['path'], 1);
8080
unset($parts['path']);
8181
}
82+
83+
/** @var PromiseInterface<ConnectionInterface> $connecting */
8284
$connecting = $this->connector->connect($authority);
8385

8486
$deferred = new Deferred(function ($_, $reject) use ($connecting, $uri) {
@@ -100,7 +102,7 @@ public function createClient(string $uri): PromiseInterface
100102

101103
$promise = $connecting->then(function (ConnectionInterface $stream) {
102104
return new StreamingClient($stream, $this->protocol->createResponseParser(), $this->protocol->createSerializer());
103-
}, function (\Exception $e) use ($uri) {
105+
}, function (\Throwable $e) use ($uri) {
104106
throw new \RuntimeException(
105107
'Connection to ' . $uri . ' failed: ' . $e->getMessage(),
106108
$e->getCode(),
@@ -175,7 +177,7 @@ function (\Exception $e) use ($redis, $uri) {
175177
return $deferred->promise();
176178
}
177179

178-
return timeout($deferred->promise(), $timeout, $this->loop)->then(null, function ($e) use ($uri) {
180+
return timeout($deferred->promise(), $timeout, $this->loop)->then(null, function (\Throwable $e) use ($uri) {
179181
if ($e instanceof TimeoutException) {
180182
throw new \RuntimeException(
181183
'Connection to ' . $uri . ' timed out after ' . $e->getTimeout() . ' seconds (ETIMEDOUT)',

src/Io/StreamingClient.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class StreamingClient extends EventEmitter
2424
/** @var SerializerInterface */
2525
private $serializer;
2626

27-
/** @var Deferred[] */
27+
/** @var Deferred<mixed>[] */
2828
private $requests = [];
2929

3030
/** @var bool */
@@ -71,7 +71,10 @@ public function __construct(DuplexStreamInterface $stream, ParserInterface $pars
7171
$this->serializer = $serializer;
7272
}
7373

74-
/** @param string[] $args */
74+
/**
75+
* @param string[] $args
76+
* @return PromiseInterface<mixed>
77+
*/
7578
public function __call(string $name, array $args): PromiseInterface
7679
{
7780
$request = new Deferred();

src/RedisClient.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class RedisClient extends EventEmitter
3737
/** @var bool */
3838
private $closed = false;
3939

40-
/** @var ?PromiseInterface */
40+
/** @var ?PromiseInterface<StreamingClient> */
4141
private $promise = null;
4242

4343
/** @var LoopInterface */
@@ -76,6 +76,9 @@ public function __construct($url, ConnectorInterface $connector = null, LoopInte
7676
$this->factory = new Factory($this->loop, $connector);
7777
}
7878

79+
/**
80+
* @return PromiseInterface<StreamingClient>
81+
*/
7982
private function client(): PromiseInterface
8083
{
8184
if ($this->promise !== null) {
@@ -132,7 +135,9 @@ private function client(): PromiseInterface
132135
);
133136

134137
return $redis;
135-
}, function (\Exception $e) {
138+
}, function (\Throwable $e) {
139+
assert($e instanceof \Exception);
140+
136141
// connection failed => discard connection attempt
137142
$this->promise = null;
138143

@@ -148,7 +153,7 @@ private function client(): PromiseInterface
148153
*
149154
* @param string $name
150155
* @param string[] $args
151-
* @return PromiseInterface Promise<mixed,Exception>
156+
* @return PromiseInterface<mixed>
152157
*/
153158
public function __call(string $name, array $args): PromiseInterface
154159
{

tests/TestCase.php

+5-18
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,17 @@ protected function createCallableMock(): MockObject
4848
}
4949
}
5050

51-
protected function expectPromiseResolve(PromiseInterface $promise): PromiseInterface
51+
/**
52+
* @param PromiseInterface<mixed> $promise
53+
*/
54+
protected function expectPromiseResolve(PromiseInterface $promise): void
5255
{
5356
$this->assertInstanceOf(PromiseInterface::class, $promise);
5457

55-
$promise->then(null, function(\Exception $error) {
58+
$promise->then(null, function(\Throwable $error) {
5659
$this->assertNull($error);
5760
$this->fail('promise rejected');
5861
});
5962
$promise->then($this->expectCallableOnce(), $this->expectCallableNever());
60-
61-
return $promise;
62-
}
63-
64-
protected function expectPromiseReject(PromiseInterface $promise): PromiseInterface
65-
{
66-
$this->assertInstanceOf(PromiseInterface::class, $promise);
67-
68-
$promise->then(function($value) {
69-
$this->assertNull($value);
70-
$this->fail('promise resolved');
71-
});
72-
73-
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
74-
75-
return $promise;
7663
}
7764
}

0 commit comments

Comments
 (0)