diff --git a/src/Prometheus/Storage/RedisClients/Predis.php b/src/Prometheus/Storage/RedisClients/Predis.php index 3dfe5a9..0da5fe0 100644 --- a/src/Prometheus/Storage/RedisClients/Predis.php +++ b/src/Prometheus/Storage/RedisClients/Predis.php @@ -83,7 +83,8 @@ public function setNx(string $key, mixed $value): void public function sMembers(string $key): array { - return $this->client->smembers($key); + /** @phpstan-ignore-next-line Predis can return null at runtime despite its array return type. */ + return $this->client->smembers($key) ?? []; } public function hGetAll(string $key): array|false diff --git a/tests/Test/Prometheus/Storage/PredisTest.php b/tests/Test/Prometheus/Storage/PredisTest.php index 82a63f0..9949055 100644 --- a/tests/Test/Prometheus/Storage/PredisTest.php +++ b/tests/Test/Prometheus/Storage/PredisTest.php @@ -58,4 +58,24 @@ public function itShouldNotClearWholeRedisOnFlush(): void self::equalTo(['not a prometheus metric key']) ); } + + /** + * @test + */ + public function itShouldReturnAnEmptyArrayWhenSMembersReturnsNull(): void + { + $client = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor() + ->addMethods(['smembers']) + ->getMock(); + + $client->expects(self::once()) + ->method('smembers') + ->with('missing-key') + ->willReturn(null); + + $predis = new \Prometheus\Storage\RedisClients\Predis($client); + + self::assertSame([], $predis->sMembers('missing-key')); + } }