Skip to content

Commit e2a9ed9

Browse files
committed
minox fixes
1 parent f98d381 commit e2a9ed9

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

src/Prometheus/Storage/Redis.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Redis implements Adapter
8080
/**
8181
* @var RedisSentinel
8282
*/
83-
private $sentinel;
83+
private $sentinel = null;
8484

8585
/**
8686
* @var boolean
@@ -96,17 +96,18 @@ public function __construct(array $options = [])
9696
$this->options = [...self::$defaultOptions, ...$options];
9797
$this->options['sentinel'] = [...self::$defaultOptions['sentinel'] ?? [], ...$options['sentinel'] ?? []];
9898
$this->redis = new \Redis();
99-
if(isset($this->options['sentinel']) && boolval($this->options['sentinel']['enable'])){
99+
if (boolval($this->options['sentinel']['enable'])) {
100100
$options['sentinel']['host'] = $options['sentinel']['host'] ?? $options['host'];
101101
$this->sentinel = new RedisSentinel($options['sentinel']);
102102
}
103103
}
104104

105105
/**
106106
* Sentinels discoverMaster
107+
* @return void
107108
*/
108-
public function updateSentinelPrimary()
109-
{
109+
public function updateSentinelPrimary(): void
110+
{
110111
$master = $this->sentinel->getMaster();
111112

112113
if (is_array($master)) {
@@ -118,8 +119,9 @@ public function updateSentinelPrimary()
118119
/**
119120
* @return \RedisSentinel
120121
*/
121-
public function getRedisSentinel() : \RedisSentinel {
122-
return $this->sentinel->getRedisSentinel();
122+
public function getRedisSentinel(): \RedisSentinel
123+
{
124+
return $this->sentinel->getSentinel();
123125
}
124126

125127
/**
@@ -130,7 +132,7 @@ public function getRedisSentinel() : \RedisSentinel {
130132
*/
131133
public static function fromExistingConnection(\Redis $redis, \RedisSentinel $redisSentinel = null): self
132134
{
133-
if($redisSentinel) {
135+
if (isset($redisSentinel)) {
134136
RedisSentinel::fromExistingConnection($redisSentinel);
135137
}
136138

@@ -281,8 +283,8 @@ private function ensureOpenConnection(): void
281283
if ($this->connectionInitialized === true) {
282284
return;
283285
}
284-
285-
if ($this->sentinel) {
286+
287+
if ($this->sentinel !== null && boolval($this->options['sentinel']['enable'])) {
286288
$reconnect = $this->options['sentinel']['reconnect'];
287289
$retries = 0;
288290
while ($retries <= $reconnect) {
@@ -333,7 +335,7 @@ private function ensureOpenConnection(): void
333335
* @throws StorageException
334336
*/
335337
private function connectToServer(): void
336-
{
338+
{
337339
$connection_successful = false;
338340
if ($this->options['persistent_connections'] !== false) {
339341
$connection_successful = $this->redis->pconnect(
@@ -344,7 +346,7 @@ private function connectToServer(): void
344346
} else {
345347
try {
346348
$connection_successful = $this->redis->connect($this->options['host'], (int) $this->options['port'], (float) $this->options['timeout']);
347-
} catch(\RedisException $ex){
349+
} catch (\RedisException $ex) {
348350
throw new StorageException(
349351
sprintf("Can't connect to Redis server. %s", $ex->getMessage()),
350352
$ex->getCode()

src/Prometheus/Storage/RedisSentinel.php

+10-13
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@ class RedisSentinel
3333
'password' => '', // phpredis sentinel auth password
3434
'ssl' => null,
3535
];
36-
36+
3737
/**
3838
* @param \RedisSentinel $redisSentinel
3939
* @return self
4040
* @throws StorageException
4141
*/
42-
public static function fromExistingConnection(\RedisSentinel $redisSentinel) : self {
42+
public static function fromExistingConnection(\RedisSentinel $redisSentinel): self
43+
{
4344
$sentinel = new self();
4445
$sentinel->sentinel = $redisSentinel;
4546
$sentinel->getMaster();
4647
return $sentinel;
47-
}
48+
}
4849

4950
/**
5051
* Redis constructor.
@@ -53,26 +54,21 @@ public static function fromExistingConnection(\RedisSentinel $redisSentinel) : s
5354
public function __construct(array $options = [])
5455
{
5556
$this->options = [...self::$defaultOptions, ...$options];
56-
$this->sentinel = $this->connectToSentinel($this->options);
57+
$this->sentinel = $this->initSentinel();
5758
}
5859

5960
/**
6061
* {@inheritdoc}
61-
* @param mixed[] $config
6262
* @return mixed[]|bool
6363
* @throws StorageException|\RedisException
6464
*/
6565
public function getMaster(): array|bool
6666
{
6767
$service = $this->options['service'];
68-
69-
try {
70-
if(!$this->sentinel) {
71-
$this->sentinel = $this->connectToSentinel($this->options);
72-
}
7368

69+
try {
7470
$master = $this->sentinel->master($service);
75-
} catch (\RedisException $e){
71+
} catch (\RedisException $e) {
7672
throw new StorageException(
7773
sprintf("Can't connect to RedisSentinel server. %s", $e->getMessage()),
7874
$e->getCode(),
@@ -102,7 +98,7 @@ protected function isValidMaster(array|bool $master): bool
10298
* @return \RedisSentinel
10399
* @throws StorageException
104100
*/
105-
private function connectToSentinel(): \RedisSentinel
101+
private function initSentinel(): \RedisSentinel
106102
{
107103
$host = $this->options['host'] ?? '';
108104
$port = $this->options['port'] ?? 26379;
@@ -155,7 +151,8 @@ private function connectToSentinel(): \RedisSentinel
155151
return new \RedisSentinel($host, $port, $timeout, $persistent, $retryInterval, $readTimeout);
156152
}
157153

158-
public function getSentinel() : \RedisSentinel {
154+
public function getSentinel(): \RedisSentinel
155+
{
159156
return $this->sentinel;
160157
}
161158
}

tests/Test/Prometheus/Storage/RedisSentinelTest.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function setUp(): void
3030
*/
3131
public function itShouldThrowAnExceptionOnConnectionFailureWithRedisSentinelNotEnabled(): void
3232
{
33-
$redis = new Redis(['host' => '/dev/null', 'sentinel' => ['host'=>'/dev/null']]);
33+
$redis = new Redis(['host' => '/dev/null', 'sentinel' => ['host' => '/dev/null']]);
3434

3535
$this->expectException(StorageException::class);
3636
$this->expectExceptionMessage("Can't connect to Redis server");
@@ -44,7 +44,7 @@ public function itShouldThrowAnExceptionOnConnectionFailureWithRedisSentinelNotE
4444
*/
4545
public function itShouldThrowAnExceptionOnConnectionFailure(): void
4646
{
47-
$redis = new Redis(['host' => '/dev/null', 'sentinel' => ['host'=>'/dev/null', 'enable' => true]]);
47+
$redis = new Redis(['host' => '/dev/null', 'sentinel' => ['host' => '/dev/null', 'enable' => true]]);
4848

4949
$this->expectException(StorageException::class);
5050
$this->expectExceptionMessage("Can't connect to RedisSentinel server");
@@ -59,6 +59,7 @@ public function itShouldThrowAnExceptionOnConnectionFailure(): void
5959
public function itShouldThrowExceptionWhenInjectedRedisIsNotConnected(): void
6060
{
6161
$connection = new \Redis();
62+
// @phpstan-ignore arguments.count
6263
$sentinel = new \RedisSentinel();
6364

6465
self::expectException(StorageException::class);
@@ -72,7 +73,7 @@ public function itShouldThrowExceptionWhenInjectedRedisIsNotConnected(): void
7273
*/
7374
public function itShouldThrowAnExceptionOnPrimaryFailure(): void
7475
{
75-
$redis = new Redis(['host' => '/dev/null', 'sentinel' => ['host'=>'/dev/null', 'enable' => true, 'service' => 'dummy']]);
76+
$redis = new Redis(['host' => '/dev/null', 'sentinel' => ['host' => '/dev/null', 'enable' => true, 'service' => 'dummy']]);
7677

7778
$this->expectException(StorageException::class);
7879
$this->expectExceptionMessage("Can't connect to RedisSentinel server");
@@ -86,10 +87,10 @@ public function itShouldThrowAnExceptionOnPrimaryFailure(): void
8687
*/
8788
public function itShouldGetMaster(): void
8889
{
89-
$redis = new Redis(['host' => '/dev/null',
90-
'sentinel' => ['host'=>REDIS_SENTINEL_HOST, 'enable' => true, 'service' => 'myprimary']
90+
$redis = new Redis(['host' => '/dev/null',
91+
'sentinel' => ['host' => REDIS_SENTINEL_HOST, 'enable' => true, 'service' => 'myprimary']
9192
]);
92-
93+
9394
$redis->collect();
9495
$redis->wipeStorage();
9596
$this->expectNotToPerformAssertions();

0 commit comments

Comments
 (0)