Skip to content

Commit 965415f

Browse files
authored
Format code (#5172)
1 parent 59dc1c9 commit 965415f

File tree

9 files changed

+51
-32
lines changed

9 files changed

+51
-32
lines changed

src/Exception/InvalidRedisProxyException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
namespace Hyperf\Redis\Exception;
1313

14-
class InvalidRedisProxyException extends \RuntimeException
14+
use RuntimeException;
15+
16+
class InvalidRedisProxyException extends RuntimeException
1517
{
1618
}

src/Exception/RedisNotFoundException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
namespace Hyperf\Redis\Exception;
1313

14-
class RedisNotFoundException extends \RuntimeException
14+
use RuntimeException;
15+
16+
class RedisNotFoundException extends RuntimeException
1517
{
1618
}

src/Pool/RedisPool.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Hyperf\Redis\Frequency;
1818
use Hyperf\Redis\RedisConnection;
1919
use Hyperf\Utils\Arr;
20+
use InvalidArgumentException;
2021
use Psr\Container\ContainerInterface;
2122

2223
class RedisPool extends Pool
@@ -28,7 +29,7 @@ public function __construct(ContainerInterface $container, protected string $nam
2829
$config = $container->get(ConfigInterface::class);
2930
$key = sprintf('redis.%s', $this->name);
3031
if (! $config->has($key)) {
31-
throw new \InvalidArgumentException(sprintf('config[%s] is not exist!', $key));
32+
throw new InvalidArgumentException(sprintf('config[%s] is not exist!', $key));
3233
}
3334

3435
$this->config = $config->get($key);

src/RedisConnection.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
use Hyperf\Pool\Exception\ConnectionException;
1919
use Hyperf\Redis\Exception\InvalidRedisConnectionException;
2020
use Psr\Container\ContainerInterface;
21+
use Redis;
22+
use RedisCluster;
23+
use RedisException;
24+
use RedisSentinel;
25+
use Throwable;
2126

2227
/**
2328
* @method bool select(int $db)
@@ -26,7 +31,7 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
2631
{
2732
use ScanCaller;
2833

29-
protected \Redis|\RedisCluster|null $connection = null;
34+
protected Redis|RedisCluster|null $connection = null;
3035

3136
protected array $config = [
3237
'host' => 'localhost',
@@ -73,7 +78,7 @@ public function __call($name, $arguments)
7378
{
7479
try {
7580
$result = $this->connection->{$name}(...$arguments);
76-
} catch (\Throwable $exception) {
81+
} catch (Throwable $exception) {
7782
$result = $this->retry($name, $arguments, $exception);
7883
}
7984

@@ -94,7 +99,7 @@ public function getActiveConnection()
9499
}
95100

96101
/**
97-
* @throws \RedisException
102+
* @throws RedisException
98103
* @throws ConnectionException
99104
*/
100105
public function reconnect(): bool
@@ -117,7 +122,7 @@ public function reconnect(): bool
117122
$redis->setOption($name, $value);
118123
}
119124

120-
if ($redis instanceof \Redis && isset($auth) && $auth !== '') {
125+
if ($redis instanceof Redis && isset($auth) && $auth !== '') {
121126
$redis->auth($auth);
122127
}
123128

@@ -154,7 +159,7 @@ public function setDatabase(?int $database): void
154159
$this->database = $database;
155160
}
156161

157-
protected function createRedisCluster(): \RedisCluster
162+
protected function createRedisCluster(): RedisCluster
158163
{
159164
try {
160165
$parameters = [];
@@ -170,23 +175,23 @@ protected function createRedisCluster(): \RedisCluster
170175
$parameters[] = $this->config['cluster']['context'];
171176
}
172177

173-
$redis = new \RedisCluster(...$parameters);
174-
} catch (\Throwable $e) {
178+
$redis = new RedisCluster(...$parameters);
179+
} catch (Throwable $e) {
175180
throw new ConnectionException('Connection reconnect failed ' . $e->getMessage());
176181
}
177182

178183
return $redis;
179184
}
180185

181-
protected function retry($name, $arguments, \Throwable $exception)
186+
protected function retry($name, $arguments, Throwable $exception)
182187
{
183188
$logger = $this->container->get(StdoutLoggerInterface::class);
184189
$logger->warning('Redis::__call failed, because ' . $exception->getMessage());
185190

186191
try {
187192
$this->reconnect();
188193
$result = $this->connection->{$name}(...$arguments);
189-
} catch (\Throwable $exception) {
194+
} catch (Throwable $exception) {
190195
$this->lastUseTime = 0.0;
191196
throw $exception;
192197
}
@@ -197,7 +202,7 @@ protected function retry($name, $arguments, \Throwable $exception)
197202
/**
198203
* @throws ConnectionException
199204
*/
200-
protected function createRedisSentinel(): \Redis
205+
protected function createRedisSentinel(): Redis
201206
{
202207
try {
203208
$nodes = $this->config['sentinel']['nodes'] ?? [];
@@ -214,7 +219,7 @@ protected function createRedisSentinel(): \Redis
214219
foreach ($nodes as $node) {
215220
try {
216221
[$sentinelHost, $sentinelPort] = explode(':', $node);
217-
$sentinel = new \RedisSentinel(
222+
$sentinel = new RedisSentinel(
218223
$sentinelHost,
219224
intval($sentinelPort),
220225
$timeout,
@@ -228,7 +233,7 @@ protected function createRedisSentinel(): \Redis
228233
[$host, $port] = $masterInfo;
229234
break;
230235
}
231-
} catch (\Throwable $exception) {
236+
} catch (Throwable $exception) {
232237
$logger = $this->container->get(StdoutLoggerInterface::class);
233238
$logger->warning('Redis sentinel connection failed, caused by ' . $exception->getMessage());
234239
continue;
@@ -246,7 +251,7 @@ protected function createRedisSentinel(): \Redis
246251
'retry_interval' => $retryInterval,
247252
'read_timeout' => $readTimeout,
248253
]);
249-
} catch (\Throwable $e) {
254+
} catch (Throwable $e) {
250255
throw new ConnectionException('Connection reconnect failed ' . $e->getMessage());
251256
}
252257

@@ -255,9 +260,9 @@ protected function createRedisSentinel(): \Redis
255260

256261
/**
257262
* @throws ConnectionException
258-
* @throws \RedisException
263+
* @throws RedisException
259264
*/
260-
protected function createRedis(array $config): \Redis
265+
protected function createRedis(array $config): Redis
261266
{
262267
$parameters = [
263268
$config['host'] ?? '',
@@ -272,7 +277,7 @@ protected function createRedis(array $config): \Redis
272277
$parameters[] = $config['context'];
273278
}
274279

275-
$redis = new \Redis();
280+
$redis = new Redis();
276281
if (! $redis->connect(...$parameters)) {
277282
throw new ConnectionException('Connection reconnect failed.');
278283
}

tests/Lua/EvalTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use HyperfTest\Redis\Stub\HGetAllMultipleStub;
1818
use Mockery;
1919
use PHPUnit\Framework\TestCase;
20+
use Redis;
2021

2122
/**
2223
* @internal
@@ -27,7 +28,7 @@ class EvalTest extends TestCase
2728
protected function tearDown(): void
2829
{
2930
$container = ContainerStub::mockContainer();
30-
$redis = $container->get(\Redis::class);
31+
$redis = $container->get(Redis::class);
3132
$redis->flushDB();
3233

3334
Mockery::close();
@@ -41,7 +42,7 @@ public function testEvalShaButNotExists()
4142
$this->assertSame('NOSCRIPT No matching script[HyperfTest\\Redis\\Stub\\HGetAllMultipleStub]. Use EVAL instead.', $message);
4243
});
4344

44-
$redis = $container->get(\Redis::class);
45+
$redis = $container->get(Redis::class);
4546
$redis->hMSet('{hash}:1', ['id' => 1, 'name' => $name1 = 'Hyperf']);
4647
$redis->hMSet('{hash}:2', ['id' => 2, 'name' => $name2 = Str::random(16)]);
4748

tests/Lua/HashTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use HyperfTest\Redis\Stub\ContainerStub;
1818
use Mockery;
1919
use PHPUnit\Framework\TestCase;
20+
use Redis;
2021

2122
/**
2223
* @internal
@@ -27,7 +28,7 @@ class HashTest extends TestCase
2728
protected function tearDown(): void
2829
{
2930
$container = ContainerStub::mockContainer();
30-
$redis = $container->get(\Redis::class);
31+
$redis = $container->get(Redis::class);
3132
$redis->flushDB();
3233

3334
Mockery::close();
@@ -36,7 +37,7 @@ protected function tearDown(): void
3637
public function testEvalHGetAllMultiple()
3738
{
3839
$container = ContainerStub::mockContainer();
39-
$redis = $container->get(\Redis::class);
40+
$redis = $container->get(Redis::class);
4041
$redis->hMSet('{hash}:1', ['id' => 1, 'name' => $name1 = 'Hyperf']);
4142
$redis->hMSet('{hash}:2', ['id' => 2, 'name' => $name2 = Str::random(16)]);
4243
$redis->hMSet('{hash}:3', ['id' => 3, 'name' => $name3 = uniqid()]);
@@ -52,7 +53,7 @@ public function testEvalHGetAllMultiple()
5253
public function testEvalHIncrByFloatIfExists()
5354
{
5455
$container = ContainerStub::mockContainer();
55-
$redis = $container->get(\Redis::class);
56+
$redis = $container->get(Redis::class);
5657
$redis->hMSet('{hash}:1', ['id' => 1, 'name' => 'Hyperf', 'incr' => 0]);
5758

5859
$script = new HIncrByFloatIfExists($container);

tests/RedisTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
use HyperfTest\Redis\Stub\RedisPoolStub;
3131
use Mockery;
3232
use PHPUnit\Framework\TestCase;
33+
use RedisCluster;
34+
use RedisSentinel;
35+
use ReflectionClass;
36+
use Throwable;
3337

3438
/**
3539
* @internal
@@ -46,7 +50,7 @@ protected function tearDown(): void
4650
public function testRedisConnect()
4751
{
4852
$redis = new \Redis();
49-
$class = new \ReflectionClass($redis);
53+
$class = new ReflectionClass($redis);
5054
$params = $class->getMethod('connect')->getParameters();
5155
[$host, $port, $timeout, $retryInterval] = $params;
5256
$this->assertSame('host', $host->getName());
@@ -83,7 +87,7 @@ public function testHasAlreadyBeenBoundToAnotherCoroutine()
8387
{
8488
$chan = new Chan(1);
8589
$redis = $this->getRedis();
86-
$ref = new \ReflectionClass($redis);
90+
$ref = new ReflectionClass($redis);
8791
$method = $ref->getMethod('getConnection');
8892
$method->setAccessible(true);
8993

@@ -134,7 +138,7 @@ public function testRedisReuseAfterThrowable()
134138
$redis = new Redis($factory);
135139
try {
136140
$redis->set('xxxx', 'yyyy');
137-
} catch (\Throwable $exception) {
141+
} catch (Throwable $exception) {
138142
$this->assertSame('Get connection failed.', $exception->getMessage());
139143
}
140144

@@ -144,7 +148,7 @@ public function testRedisReuseAfterThrowable()
144148

145149
public function testRedisClusterConstructor()
146150
{
147-
$ref = new \ReflectionClass(\RedisCluster::class);
151+
$ref = new ReflectionClass(RedisCluster::class);
148152
$method = $ref->getMethod('__construct');
149153
$names = [
150154
'name', 'seeds', 'timeout', 'read_timeout', 'persistent', 'auth',
@@ -169,7 +173,7 @@ public function testNewRedisCluster()
169173
$redis = new RedisProxy($factory, 'cluster1');
170174
$redis->get('test');
171175
$this->assertTrue(false);
172-
} catch (\Throwable $exception) {
176+
} catch (Throwable $exception) {
173177
$this->assertInstanceOf(ConnectionException::class, $exception);
174178
$this->assertStringNotContainsString('RedisCluster::__construct() expects parameter', $exception->getMessage());
175179
}
@@ -187,7 +191,7 @@ public function testShuffleNodes()
187191

188192
public function testRedisSentinelParams()
189193
{
190-
$rel = new \ReflectionClass(\RedisSentinel::class);
194+
$rel = new ReflectionClass(RedisSentinel::class);
191195
$method = $rel->getMethod('__construct');
192196
$count = count($method->getParameters());
193197
if ($count === 6) {

tests/Stub/RedisConnectionFailedStub.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
*/
1212
namespace HyperfTest\Redis\Stub;
1313

14+
use Exception;
15+
1416
class RedisConnectionFailedStub extends RedisConnectionStub
1517
{
1618
public function getConnection()
1719
{
18-
throw new \Exception('Get connection failed.');
20+
throw new Exception('Get connection failed.');
1921
}
2022
}

tests/Stub/RedisPoolStub.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Hyperf\Contract\ConnectionInterface;
1515
use Hyperf\Contract\StdoutLoggerInterface;
1616
use Hyperf\Redis\Pool\RedisPool;
17+
use Throwable;
1718

1819
class RedisPoolStub extends RedisPool
1920
{
@@ -22,7 +23,7 @@ public function flushAll()
2223
while ($conn = $this->channel->pop(0.001)) {
2324
try {
2425
$conn->close();
25-
} catch (\Throwable $exception) {
26+
} catch (Throwable $exception) {
2627
if ($this->container->has(StdoutLoggerInterface::class) && $logger = $this->container->get(StdoutLoggerInterface::class)) {
2728
$logger->error((string) $exception);
2829
}

0 commit comments

Comments
 (0)