Skip to content

Commit cc61f04

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

14 files changed

+116
-137
lines changed

README.md

Lines changed: 8 additions & 8 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 7 additions & 11 deletions
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;
@@ -15,14 +16,9 @@
1516

1617
class Factory
1718
{
18-
/** @var LoopInterface */
19-
private $loop;
20-
21-
/** @var ConnectorInterface */
22-
private $connector;
23-
24-
/** @var ProtocolFactory */
25-
private $protocol;
19+
private LoopInterface $loop;
20+
private ConnectorInterface $connector;
21+
private ProtocolFactory $protocol;
2622

2723
/**
2824
* @param ?LoopInterface $loop
@@ -40,10 +36,10 @@ public function __construct(LoopInterface $loop = null, ConnectorInterface $conn
4036
* Create Redis client connected to address of given redis instance
4137
*
4238
* @param string $uri Redis server URI to connect to
43-
* @return \React\Promise\PromiseInterface<Client,\Exception> Promise that will
39+
* @return PromiseInterface<Client,\Exception> Promise that will
4440
* be fulfilled with `Client` on success or rejects with `\Exception` on error.
4541
*/
46-
public function createClient($uri)
42+
public function createClient(string $uri): PromiseInterface
4743
{
4844
// support `redis+unix://` scheme for Unix domain socket (UDS) paths
4945
if (preg_match('/^(redis\+unix:\/\/(?:[^:]*:[^@]*@)?)(.+?)?$/', $uri, $match)) {
@@ -184,7 +180,7 @@ function (\Exception $e) use ($redis, $uri) {
184180
* @param string $target
185181
* @return Client
186182
*/
187-
public function createLazyClient($target)
183+
public function createLazyClient($target): Client
188184
{
189185
return new LazyClient($target, $this, $this->loop);
190186
}

src/LazyClient.php

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,31 @@
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
{
15-
private $target;
16-
/** @var Factory */
17-
private $factory;
18-
private $closed = false;
19-
private $promise;
20-
21-
private $loop;
22-
private $idlePeriod = 60.0;
23-
private $idleTimer;
24-
private $pending = 0;
25-
26-
private $subscribed = [];
27-
private $psubscribed = [];
28-
29-
/**
30-
* @param $target
31-
*/
32-
public function __construct($target, Factory $factory, LoopInterface $loop)
17+
private string $target;
18+
private Factory $factory;
19+
private bool $closed = false;
20+
private ?PromiseInterface $promise = null;
21+
22+
private LoopInterface $loop;
23+
private float $idlePeriod = 60.0;
24+
private ?TimerInterface $idleTimer = null;
25+
private int $pending = 0;
26+
27+
private array $subscribed = [];
28+
private array $psubscribed = [];
29+
30+
public function __construct(string $target, Factory $factory, LoopInterface $loop)
3331
{
3432
$args = [];
3533
\parse_str((string) \parse_url($target, \PHP_URL_QUERY), $args);
@@ -42,7 +40,7 @@ public function __construct($target, Factory $factory, LoopInterface $loop)
4240
$this->loop = $loop;
4341
}
4442

45-
private function client()
43+
private function client(): PromiseInterface
4644
{
4745
if ($this->promise !== null) {
4846
return $this->promise;
@@ -71,16 +69,16 @@ private function client()
7169
});
7270

7371
// keep track of all channels and patterns this connection is subscribed to
74-
$redis->on('subscribe', function ($channel) {
72+
$redis->on('subscribe', function (string $channel) {
7573
$this->subscribed[$channel] = true;
7674
});
77-
$redis->on('psubscribe', function ($pattern) {
75+
$redis->on('psubscribe', function (string $pattern) {
7876
$this->psubscribed[$pattern] = true;
7977
});
80-
$redis->on('unsubscribe', function ($channel) {
78+
$redis->on('unsubscribe', function (string $channel) {
8179
unset($this->subscribed[$channel]);
8280
});
83-
$redis->on('punsubscribe', function ($pattern) {
81+
$redis->on('punsubscribe', function (string $pattern) {
8482
unset($this->psubscribed[$pattern]);
8583
});
8684

@@ -106,7 +104,7 @@ private function client()
106104
});
107105
}
108106

109-
public function __call($name, $args)
107+
public function __call(string $name, array $args): PromiseInterface
110108
{
111109
if ($this->closed) {
112110
return reject(new \RuntimeException(
@@ -122,15 +120,15 @@ function ($result) {
122120
$this->idle();
123121
return $result;
124122
},
125-
function ($error) {
123+
function (\Exception $error) {
126124
$this->idle();
127125
throw $error;
128126
}
129127
);
130128
});
131129
}
132130

133-
public function end()
131+
public function end(): void
134132
{
135133
if ($this->promise === null) {
136134
$this->close();
@@ -140,15 +138,15 @@ public function end()
140138
return;
141139
}
142140

143-
return $this->client()->then(function (Client $redis) {
141+
$this->client()->then(function (Client $redis) {
144142
$redis->on('close', function () {
145143
$this->close();
146144
});
147145
$redis->end();
148146
});
149147
}
150148

151-
public function close()
149+
public function close(): void
152150
{
153151
if ($this->closed) {
154152
return;
@@ -176,10 +174,7 @@ public function close()
176174
$this->removeAllListeners();
177175
}
178176

179-
/**
180-
* @internal
181-
*/
182-
public function awake()
177+
private function awake(): void
183178
{
184179
++$this->pending;
185180

@@ -189,10 +184,7 @@ public function awake()
189184
}
190185
}
191186

192-
/**
193-
* @internal
194-
*/
195-
public function idle()
187+
private function idle(): void
196188
{
197189
--$this->pending;
198190

src/StreamingClient.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@
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
{
21-
private $stream;
22-
private $parser;
23-
private $serializer;
24-
private $requests = [];
25-
private $ending = false;
26-
private $closed = false;
22+
private DuplexStreamInterface $stream;
23+
private ParserInterface $parser;
24+
private SerializerInterface $serializer;
25+
private array $requests = [];
26+
private bool $ending = false;
27+
private bool $closed = false;
2728

28-
private $subscribed = 0;
29-
private $psubscribed = 0;
29+
private int $subscribed = 0;
30+
private int $psubscribed = 0;
3031

3132
public function __construct(DuplexStreamInterface $stream, ParserInterface $parser = null, SerializerInterface $serializer = null)
3233
{
@@ -40,7 +41,7 @@ public function __construct(DuplexStreamInterface $stream, ParserInterface $pars
4041
}
4142
}
4243

43-
$stream->on('data', function($chunk) use ($parser) {
44+
$stream->on('data', function (string $chunk) use ($parser) {
4445
try {
4546
$models = $parser->pushIncoming($chunk);
4647
} catch (ParserException $error) {
@@ -71,7 +72,7 @@ public function __construct(DuplexStreamInterface $stream, ParserInterface $pars
7172
$this->serializer = $serializer;
7273
}
7374

74-
public function __call($name, $args)
75+
public function __call(string $name, array $args): PromiseInterface
7576
{
7677
$request = new Deferred();
7778
$promise = $request->promise();
@@ -102,7 +103,7 @@ public function __call($name, $args)
102103
}
103104

104105
if (in_array($name, $pubsubs)) {
105-
$promise->then(function ($array) {
106+
$promise->then(function (array $array) {
106107
$first = array_shift($array);
107108

108109
// (p)(un)subscribe messages are to be forwarded
@@ -120,7 +121,7 @@ public function __call($name, $args)
120121
return $promise;
121122
}
122123

123-
public function handleMessage(ModelInterface $message)
124+
public function handleMessage(ModelInterface $message): void
124125
{
125126
if (($this->subscribed !== 0 || $this->psubscribed !== 0) && $message instanceof MultiBulkReply) {
126127
$array = $message->getValueNative();
@@ -154,7 +155,7 @@ public function handleMessage(ModelInterface $message)
154155
}
155156
}
156157

157-
public function end()
158+
public function end(): void
158159
{
159160
$this->ending = true;
160161

@@ -163,7 +164,7 @@ public function end()
163164
}
164165
}
165166

166-
public function close()
167+
public function close(): void
167168
{
168169
if ($this->closed) {
169170
return;

0 commit comments

Comments
 (0)