Skip to content

Commit ff8eb66

Browse files
committed
Add type definitions for all APIs and tests
1 parent 73b9460 commit ff8eb66

14 files changed

+135
-93
lines changed

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ $redis = $factory->createLazyClient('localhost:6379');
8181
$redis->set('greeting', 'Hello world');
8282
$redis->append('greeting', '!');
8383

84-
$redis->get('greeting')->then(function ($greeting) {
84+
$redis->get('greeting')->then(function (string $greeting) {
8585
// Hello world!
8686
echo $greeting . PHP_EOL;
8787
});
8888

89-
$redis->incr('invocation')->then(function ($n) {
89+
$redis->incr('invocation')->then(function (int $n) {
9090
echo 'This is invocation #' . $n . PHP_EOL;
9191
});
9292

@@ -184,7 +184,7 @@ subscribe to a channel and then receive incoming PubSub `message` events:
184184
$channel = 'user';
185185
$redis->subscribe($channel);
186186

187-
$redis->on('message', function ($channel, $payload) {
187+
$redis->on('message', function (string $channel, string $payload) {
188188
// pubsub message received on given $channel
189189
var_dump($channel, json_decode($payload));
190190
});
@@ -208,7 +208,7 @@ all incoming PubSub messages with the `pmessage` event:
208208
$pattern = 'user.*';
209209
$redis->psubscribe($pattern);
210210

211-
$redis->on('pmessage', function ($pattern, $channel, $payload) {
211+
$redis->on('pmessage', function (string $pattern, string $channel, string $payload) {
212212
// pubsub message received matching given $pattern
213213
var_dump($channel, json_decode($payload));
214214
});
@@ -248,16 +248,16 @@ Additionally, can listen for the following PubSub events to get notifications
248248
about subscribed/unsubscribed channels and patterns:
249249

250250
```php
251-
$redis->on('subscribe', function ($channel, $total) {
251+
$redis->on('subscribe', function (string $channel, int $total) {
252252
// subscribed to given $channel
253253
});
254-
$redis->on('psubscribe', function ($pattern, $total) {
254+
$redis->on('psubscribe', function (string $pattern, int $total) {
255255
// subscribed to matching given $pattern
256256
});
257-
$redis->on('unsubscribe', function ($channel, $total) {
257+
$redis->on('unsubscribe', function (string $channel, int $total) {
258258
// unsubscribed from given $channel
259259
});
260-
$redis->on('punsubscribe', function ($pattern, $total) {
260+
$redis->on('punsubscribe', function (string $pattern, int $total) {
261261
// unsubscribed from matching given $pattern
262262
});
263263
```

examples/incr.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
$redis->incr('test');
1212

13-
$redis->get('test')->then(function ($result) {
13+
$redis->get('test')->then(function (string $result) {
1414
var_dump($result);
1515
}, function (Exception $e) {
1616
echo 'Error: ' . $e->getMessage() . PHP_EOL;

examples/publish.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
$channel = $argv[1] ?? 'channel';
1212
$message = $argv[2] ?? 'message';
1313

14-
$redis->publish($channel, $message)->then(function ($received) {
14+
$redis->publish($channel, $message)->then(function (int $received) {
1515
echo 'Successfully published. Received by ' . $received . PHP_EOL;
1616
}, function (Exception $e) {
1717
echo 'Unable to publish: ' . $e->getMessage() . PHP_EOL;

examples/subscribe.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
echo 'Unable to subscribe: ' . $e->getMessage() . PHP_EOL;
2020
});
2121

22-
$redis->on('message', function ($channel, $message) {
22+
$redis->on('message', function (string $channel, string $message) {
2323
echo 'Message on ' . $channel . ': ' . $message . PHP_EOL;
2424
});
2525

2626
// automatically re-subscribe to channel on connection issues
27-
$redis->on('unsubscribe', function ($channel) use ($redis) {
27+
$redis->on('unsubscribe', function (string $channel) use ($redis) {
2828
echo 'Unsubscribed from ' . $channel . PHP_EOL;
2929

30-
Loop::addPeriodicTimer(2.0, function ($timer) use ($redis, $channel){
30+
Loop::addPeriodicTimer(2.0, function (React\EventLoop\TimerInterface $timer) use ($redis, $channel){
3131
$redis->subscribe($channel)->then(function () use ($timer) {
3232
echo 'Now subscribed again' . PHP_EOL;
3333
Loop::cancelTimer($timer);

src/Client.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ interface Client extends EventEmitterInterface
3131
* @param string[] $args
3232
* @return PromiseInterface Promise<mixed,Exception>
3333
*/
34-
public function __call($name, $args);
34+
public function __call(string $name, array $args): PromiseInterface;
3535

3636
/**
3737
* end connection once all pending requests have been replied to
@@ -40,7 +40,7 @@ public function __call($name, $args);
4040
* @uses self::close() once all replies have been received
4141
* @see self::close() for closing the connection immediately
4242
*/
43-
public function end();
43+
public function end(): void;
4444

4545
/**
4646
* close connection immediately
@@ -50,5 +50,5 @@ public function end();
5050
* @return void
5151
* @see self::end() for closing the connection once the client is idle
5252
*/
53-
public function close();
53+
public function close(): void;
5454
}

src/Factory.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use React\EventLoop\Loop;
77
use React\EventLoop\LoopInterface;
88
use React\Promise\Deferred;
9+
use React\Promise\PromiseInterface;
910
use React\Promise\Timer\TimeoutException;
1011
use React\Socket\ConnectionInterface;
1112
use React\Socket\Connector;
@@ -40,10 +41,10 @@ public function __construct(LoopInterface $loop = null, ConnectorInterface $conn
4041
* Create Redis client connected to address of given redis instance
4142
*
4243
* @param string $uri Redis server URI to connect to
43-
* @return \React\Promise\PromiseInterface<Client,\Exception> Promise that will
44+
* @return PromiseInterface<Client,\Exception> Promise that will
4445
* be fulfilled with `Client` on success or rejects with `\Exception` on error.
4546
*/
46-
public function createClient($uri)
47+
public function createClient(string $uri): PromiseInterface
4748
{
4849
// support `redis+unix://` scheme for Unix domain socket (UDS) paths
4950
if (preg_match('/^(redis\+unix:\/\/(?:[^:]*:[^@]*@)?)(.+?)?$/', $uri, $match)) {
@@ -184,7 +185,7 @@ function (\Exception $e) use ($redis, $uri) {
184185
* @param string $target
185186
* @return Client
186187
*/
187-
public function createLazyClient($target)
188+
public function createLazyClient($target): Client
188189
{
189190
return new LazyClient($target, $this, $this->loop);
190191
}

src/LazyClient.php

+34-25
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,48 @@
33
namespace Clue\React\Redis;
44

55
use Evenement\EventEmitter;
6-
use React\Stream\Util;
76
use React\EventLoop\LoopInterface;
7+
use React\EventLoop\TimerInterface;
8+
use React\Promise\PromiseInterface;
9+
use React\Stream\Util;
810
use function React\Promise\reject;
911

1012
/**
1113
* @internal
1214
*/
1315
class LazyClient extends EventEmitter implements Client
1416
{
17+
/** @var string */
1518
private $target;
19+
1620
/** @var Factory */
1721
private $factory;
22+
23+
/** @var bool */
1824
private $closed = false;
19-
private $promise;
2025

26+
/** @var ?PromiseInterface */
27+
private $promise = null;
28+
29+
/** @var LoopInterface */
2130
private $loop;
31+
32+
/** @var float */
2233
private $idlePeriod = 60.0;
23-
private $idleTimer;
34+
35+
/** @var ?TimerInterface */
36+
private $idleTimer = null;
37+
38+
/** @var int */
2439
private $pending = 0;
2540

41+
/** @var array<string,bool> */
2642
private $subscribed = [];
43+
44+
/** @var array<string,bool> */
2745
private $psubscribed = [];
2846

29-
/**
30-
* @param $target
31-
*/
32-
public function __construct($target, Factory $factory, LoopInterface $loop)
47+
public function __construct(string $target, Factory $factory, LoopInterface $loop)
3348
{
3449
$args = [];
3550
\parse_str((string) \parse_url($target, \PHP_URL_QUERY), $args);
@@ -42,7 +57,7 @@ public function __construct($target, Factory $factory, LoopInterface $loop)
4257
$this->loop = $loop;
4358
}
4459

45-
private function client()
60+
private function client(): PromiseInterface
4661
{
4762
if ($this->promise !== null) {
4863
return $this->promise;
@@ -71,16 +86,16 @@ private function client()
7186
});
7287

7388
// keep track of all channels and patterns this connection is subscribed to
74-
$redis->on('subscribe', function ($channel) {
89+
$redis->on('subscribe', function (string $channel) {
7590
$this->subscribed[$channel] = true;
7691
});
77-
$redis->on('psubscribe', function ($pattern) {
92+
$redis->on('psubscribe', function (string $pattern) {
7893
$this->psubscribed[$pattern] = true;
7994
});
80-
$redis->on('unsubscribe', function ($channel) {
95+
$redis->on('unsubscribe', function (string $channel) {
8196
unset($this->subscribed[$channel]);
8297
});
83-
$redis->on('punsubscribe', function ($pattern) {
98+
$redis->on('punsubscribe', function (string $pattern) {
8499
unset($this->psubscribed[$pattern]);
85100
});
86101

@@ -106,7 +121,7 @@ private function client()
106121
});
107122
}
108123

109-
public function __call($name, $args)
124+
public function __call(string $name, array $args): PromiseInterface
110125
{
111126
if ($this->closed) {
112127
return reject(new \RuntimeException(
@@ -122,15 +137,15 @@ function ($result) {
122137
$this->idle();
123138
return $result;
124139
},
125-
function ($error) {
140+
function (\Exception $error) {
126141
$this->idle();
127142
throw $error;
128143
}
129144
);
130145
});
131146
}
132147

133-
public function end()
148+
public function end(): void
134149
{
135150
if ($this->promise === null) {
136151
$this->close();
@@ -140,15 +155,15 @@ public function end()
140155
return;
141156
}
142157

143-
return $this->client()->then(function (Client $redis) {
158+
$this->client()->then(function (Client $redis) {
144159
$redis->on('close', function () {
145160
$this->close();
146161
});
147162
$redis->end();
148163
});
149164
}
150165

151-
public function close()
166+
public function close(): void
152167
{
153168
if ($this->closed) {
154169
return;
@@ -176,10 +191,7 @@ public function close()
176191
$this->removeAllListeners();
177192
}
178193

179-
/**
180-
* @internal
181-
*/
182-
public function awake()
194+
private function awake(): void
183195
{
184196
++$this->pending;
185197

@@ -189,10 +201,7 @@ public function awake()
189201
}
190202
}
191203

192-
/**
193-
* @internal
194-
*/
195-
public function idle()
204+
private function idle(): void
196205
{
197206
--$this->pending;
198207

src/StreamingClient.php

+22-7
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,37 @@
1111
use Clue\Redis\Protocol\Serializer\SerializerInterface;
1212
use Evenement\EventEmitter;
1313
use React\Promise\Deferred;
14+
use React\Promise\PromiseInterface;
1415
use React\Stream\DuplexStreamInterface;
1516

1617
/**
1718
* @internal
1819
*/
1920
class StreamingClient extends EventEmitter implements Client
2021
{
22+
/** @var DuplexStreamInterface */
2123
private $stream;
24+
25+
/** @var ParserInterface */
2226
private $parser;
27+
28+
/** @var SerializerInterface */
2329
private $serializer;
30+
31+
/** @var Deferred[] */
2432
private $requests = [];
33+
34+
/** @var bool */
2535
private $ending = false;
36+
37+
/** @var bool */
2638
private $closed = false;
2739

40+
/** @var int */
2841
private $subscribed = 0;
29-
private $psubscribed = 0;
42+
43+
/** @var int */
44+
private int $psubscribed = 0;
3045

3146
public function __construct(DuplexStreamInterface $stream, ParserInterface $parser = null, SerializerInterface $serializer = null)
3247
{
@@ -40,7 +55,7 @@ public function __construct(DuplexStreamInterface $stream, ParserInterface $pars
4055
}
4156
}
4257

43-
$stream->on('data', function($chunk) use ($parser) {
58+
$stream->on('data', function (string $chunk) use ($parser) {
4459
try {
4560
$models = $parser->pushIncoming($chunk);
4661
} catch (ParserException $error) {
@@ -71,7 +86,7 @@ public function __construct(DuplexStreamInterface $stream, ParserInterface $pars
7186
$this->serializer = $serializer;
7287
}
7388

74-
public function __call($name, $args)
89+
public function __call(string $name, array $args): PromiseInterface
7590
{
7691
$request = new Deferred();
7792
$promise = $request->promise();
@@ -102,7 +117,7 @@ public function __call($name, $args)
102117
}
103118

104119
if (in_array($name, $pubsubs)) {
105-
$promise->then(function ($array) {
120+
$promise->then(function (array $array) {
106121
$first = array_shift($array);
107122

108123
// (p)(un)subscribe messages are to be forwarded
@@ -120,7 +135,7 @@ public function __call($name, $args)
120135
return $promise;
121136
}
122137

123-
public function handleMessage(ModelInterface $message)
138+
public function handleMessage(ModelInterface $message): void
124139
{
125140
if (($this->subscribed !== 0 || $this->psubscribed !== 0) && $message instanceof MultiBulkReply) {
126141
$array = $message->getValueNative();
@@ -154,7 +169,7 @@ public function handleMessage(ModelInterface $message)
154169
}
155170
}
156171

157-
public function end()
172+
public function end(): void
158173
{
159174
$this->ending = true;
160175

@@ -163,7 +178,7 @@ public function end()
163178
}
164179
}
165180

166-
public function close()
181+
public function close(): void
167182
{
168183
if ($this->closed) {
169184
return;

0 commit comments

Comments
 (0)