Skip to content

Commit 6e33521

Browse files
committed
Improve type definitions and update to PHPStan level max
1 parent 1fc247d commit 6e33521

File tree

10 files changed

+126
-156
lines changed

10 files changed

+126
-156
lines changed

examples/cli.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
$params = explode(' ', $line);
2727
$method = array_shift($params);
28-
$promise = call_user_func_array([$redis, $method], $params);
28+
29+
assert(is_callable([$redis, $method]));
30+
$promise = $redis->$method(...$params);
2931

3032
// special method such as end() / close() called
3133
if (!$promise instanceof React\Promise\PromiseInterface) {

phpstan.neon.dist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 3
2+
level: max
33

44
paths:
55
- examples/
@@ -13,3 +13,5 @@ parameters:
1313
# ignore undefined methods due to magic `__call()` method
1414
- '/^Call to an undefined method Clue\\React\\Redis\\RedisClient::.+\(\)\.$/'
1515
- '/^Call to an undefined method Clue\\React\\Redis\\Io\\StreamingClient::.+\(\)\.$/'
16+
# ignore incomplete type information for mocks in legacy PHPUnit 7.5
17+
- '/^Parameter #\d+ .+ of .+ expects .+, PHPUnit\\Framework\\MockObject\\MockObject given\.$/'

src/Io/Factory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function createClient(string $uri): PromiseInterface
7575
if ($parts['scheme'] === 'rediss') {
7676
$authority = 'tls://' . $authority;
7777
} elseif ($parts['scheme'] === 'redis+unix') {
78+
assert(isset($parts['path']));
7879
$authority = 'unix://' . substr($parts['path'], 1);
7980
unset($parts['path']);
8081
}
@@ -107,7 +108,7 @@ public function createClient(string $uri): PromiseInterface
107108

108109
// use `?password=secret` query or `user:secret@host` password form URL
109110
if (isset($args['password']) || isset($parts['pass'])) {
110-
$pass = $args['password'] ?? rawurldecode($parts['pass']);
111+
$pass = $args['password'] ?? rawurldecode($parts['pass']); // @phpstan-ignore-line
111112
$promise = $promise->then(function (StreamingClient $redis) use ($pass, $uri) {
112113
return $redis->auth($pass)->then(
113114
function () use ($redis) {
@@ -135,7 +136,7 @@ function (\Exception $e) use ($redis, $uri) {
135136

136137
// use `?db=1` query or `/1` path (skip first slash)
137138
if (isset($args['db']) || (isset($parts['path']) && $parts['path'] !== '/')) {
138-
$db = $args['db'] ?? substr($parts['path'], 1);
139+
$db = $args['db'] ?? substr($parts['path'], 1); // @phpstan-ignore-line
139140
$promise = $promise->then(function (StreamingClient $redis) use ($db, $uri) {
140141
return $redis->select($db)->then(
141142
function () use ($redis) {

src/Io/StreamingClient.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Clue\React\Redis\Io;
44

5-
use Clue\Redis\Protocol\Factory as ProtocolFactory;
65
use Clue\Redis\Protocol\Model\ErrorReply;
76
use Clue\Redis\Protocol\Model\ModelInterface;
87
use Clue\Redis\Protocol\Model\MultiBulkReply;
@@ -22,9 +21,6 @@ class StreamingClient extends EventEmitter
2221
/** @var DuplexStreamInterface */
2322
private $stream;
2423

25-
/** @var ParserInterface */
26-
private $parser;
27-
2824
/** @var SerializerInterface */
2925
private $serializer;
3026

@@ -43,18 +39,8 @@ class StreamingClient extends EventEmitter
4339
/** @var int */
4440
private $psubscribed = 0;
4541

46-
public function __construct(DuplexStreamInterface $stream, ParserInterface $parser = null, SerializerInterface $serializer = null)
42+
public function __construct(DuplexStreamInterface $stream, ParserInterface $parser, SerializerInterface $serializer)
4743
{
48-
if ($parser === null || $serializer === null) {
49-
$factory = new ProtocolFactory();
50-
if ($parser === null) {
51-
$parser = $factory->createResponseParser();
52-
}
53-
if ($serializer === null) {
54-
$serializer = $factory->createSerializer();
55-
}
56-
}
57-
5844
$stream->on('data', function (string $chunk) use ($parser) {
5945
try {
6046
$models = $parser->pushIncoming($chunk);
@@ -82,10 +68,10 @@ public function __construct(DuplexStreamInterface $stream, ParserInterface $pars
8268
$stream->on('close', [$this, 'close']);
8369

8470
$this->stream = $stream;
85-
$this->parser = $parser;
8671
$this->serializer = $serializer;
8772
}
8873

74+
/** @param string[] $args */
8975
public function __call(string $name, array $args): PromiseInterface
9076
{
9177
$request = new Deferred();
@@ -139,6 +125,7 @@ public function handleMessage(ModelInterface $message): void
139125
{
140126
if (($this->subscribed !== 0 || $this->psubscribed !== 0) && $message instanceof MultiBulkReply) {
141127
$array = $message->getValueNative();
128+
assert(\is_array($array));
142129
$first = array_shift($array);
143130

144131
// pub/sub messages are to be forwarded and should not be processed as request responses

src/RedisClient.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public function __call(string $name, array $args): PromiseInterface
161161

162162
return $this->client()->then(function (StreamingClient $redis) use ($name, $args) {
163163
$this->awake();
164+
assert(\is_callable([$redis, $name])); // @phpstan-ignore-next-line
164165
return \call_user_func_array([$redis, $name], $args)->then(
165166
function ($result) {
166167
$this->idle();
@@ -252,6 +253,7 @@ private function idle(): void
252253

253254
if ($this->pending < 1 && $this->idlePeriod >= 0 && !$this->subscribed && !$this->psubscribed && $this->promise !== null) {
254255
$this->idleTimer = $this->loop->addTimer($this->idlePeriod, function () {
256+
assert($this->promise instanceof PromiseInterface);
255257
$this->promise->then(function (StreamingClient $redis) {
256258
$redis->close();
257259
});

tests/FunctionalTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function setUp(): void
2626
$this->loop = new StreamSelectLoop();
2727
}
2828

29-
public function testPing()
29+
public function testPing(): void
3030
{
3131
$redis = new RedisClient($this->uri, null, $this->loop);
3232

@@ -38,7 +38,7 @@ public function testPing()
3838
$this->assertEquals('PONG', $ret);
3939
}
4040

41-
public function testPingLazy()
41+
public function testPingLazy(): void
4242
{
4343
$redis = new RedisClient($this->uri, null, $this->loop);
4444

@@ -53,7 +53,7 @@ public function testPingLazy()
5353
/**
5454
* @doesNotPerformAssertions
5555
*/
56-
public function testPingLazyWillNotBlockLoop()
56+
public function testPingLazyWillNotBlockLoop(): void
5757
{
5858
$redis = new RedisClient($this->uri, null, $this->loop);
5959

@@ -65,7 +65,7 @@ public function testPingLazyWillNotBlockLoop()
6565
/**
6666
* @doesNotPerformAssertions
6767
*/
68-
public function testLazyClientWithoutCommandsWillNotBlockLoop()
68+
public function testLazyClientWithoutCommandsWillNotBlockLoop(): void
6969
{
7070
$redis = new RedisClient($this->uri, null, $this->loop);
7171

@@ -74,7 +74,7 @@ public function testLazyClientWithoutCommandsWillNotBlockLoop()
7474
unset($redis);
7575
}
7676

77-
public function testMgetIsNotInterpretedAsSubMessage()
77+
public function testMgetIsNotInterpretedAsSubMessage(): void
7878
{
7979
$redis = new RedisClient($this->uri, null, $this->loop);
8080

@@ -86,7 +86,7 @@ public function testMgetIsNotInterpretedAsSubMessage()
8686
await($promise, $this->loop);
8787
}
8888

89-
public function testPipeline()
89+
public function testPipeline(): void
9090
{
9191
$redis = new RedisClient($this->uri, null, $this->loop);
9292

@@ -98,7 +98,7 @@ public function testPipeline()
9898
await($promise, $this->loop);
9999
}
100100

101-
public function testInvalidCommand()
101+
public function testInvalidCommand(): void
102102
{
103103
$redis = new RedisClient($this->uri, null, $this->loop);
104104
$promise = $redis->doesnotexist(1, 2, 3);
@@ -112,7 +112,7 @@ public function testInvalidCommand()
112112
await($promise, $this->loop);
113113
}
114114

115-
public function testMultiExecEmpty()
115+
public function testMultiExecEmpty(): void
116116
{
117117
$redis = new RedisClient($this->uri, null, $this->loop);
118118
$redis->multi()->then($this->expectCallableOnceWith('OK'));
@@ -121,7 +121,7 @@ public function testMultiExecEmpty()
121121
await($promise, $this->loop);
122122
}
123123

124-
public function testMultiExecQueuedExecHasValues()
124+
public function testMultiExecQueuedExecHasValues(): void
125125
{
126126
$redis = new RedisClient($this->uri, null, $this->loop);
127127

@@ -135,7 +135,7 @@ public function testMultiExecQueuedExecHasValues()
135135
await($promise, $this->loop);
136136
}
137137

138-
public function testPubSub()
138+
public function testPubSub(): void
139139
{
140140
$consumer = new RedisClient($this->uri, null, $this->loop);
141141
$producer = new RedisClient($this->uri, null, $this->loop);
@@ -156,7 +156,7 @@ public function testPubSub()
156156
await($deferred->promise(), $this->loop, 0.1);
157157
}
158158

159-
public function testClose()
159+
public function testClose(): void
160160
{
161161
$redis = new RedisClient($this->uri, null, $this->loop);
162162

@@ -167,7 +167,7 @@ public function testClose()
167167
$redis->get('willBeRejectedRightAway')->then(null, $this->expectCallableOnce());
168168
}
169169

170-
public function testCloseLazy()
170+
public function testCloseLazy(): void
171171
{
172172
$redis = new RedisClient($this->uri, null, $this->loop);
173173

0 commit comments

Comments
 (0)