From a50d1129eb7e0d38fb11ee02d98c9a7081e63445 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Thu, 28 May 2026 08:41:29 +0300 Subject: [PATCH] fix: handle null Predis set members Signed-off-by: Simon Podlipsky --- .../Storage/RedisClients/Predis.php | 3 ++- tests/Test/Prometheus/Storage/PredisTest.php | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Prometheus/Storage/RedisClients/Predis.php b/src/Prometheus/Storage/RedisClients/Predis.php index 3dfe5a92..0da5fe06 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 82a63f03..99490557 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')); + } }