8000 bug #38670 [RateLimiter] Be more type safe when fetching from cache (… · symfony/symfony@7539325 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7539325

Browse files
committed
bug #38670 [RateLimiter] Be more type safe when fetching from cache (Nyholm)
This PR was merged into the 5.x branch. Discussion ---------- [RateLimiter] Be more type safe when fetching from cache | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | This is a super minor thing. A `$cacheItem` can be a hit, but it does not contain a `LimiterStateInterface`. Also, PSR-6 specifies that if the `$cacheItem` is not a hit, it must return null. Commits ------- 4795756 [RateLimiter] Be more type safe when fetching form cache
2 parents 58f4e9d + 4795756 commit 7539325

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/Symfony/Component/RateLimiter/Storage/CacheStorage.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ public function save(LimiterStateInterface $limiterState): void
4242
public function fetch(string $limiterStateId): ?LimiterStateInterface
4343
{
4444
$cacheItem = $this->pool->getItem(sha1($limiterStateId));
45-
if (!$cacheItem->isHit()) {
46-
return null;
45+
$value = $cacheItem->get();
46+
if ($value instanceof LimiterStateInterface) {
47+
return $value;
4748
}
4849

49-
return $cacheItem->get();
50+
return null;
5051
}
5152

5253
public function delete(string $limiterStateId): void

src/Symfony/Component/RateLimiter/Tests/Storage/CacheStorageTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ public function testFetchExistingState()
5555
$this->assertEquals($window, $this->storage->fetch('test'));
5656
}
5757

58+
public function testFetchExistingJunk()
59+
{
60+
$cacheItem = $this->createMock(CacheItemInterface::class);
61+
62+
$cacheItem->expects($this->any())->method('get')->willReturn('junk');
63+
$cacheItem->expects($this->any())->method('isHit')->willReturn(true);
64+
65+
$this->pool->expects($this->any())->method('getItem')->with(sha1('test'))->willReturn($cacheItem);
66+
67+
$this->assertNull($this->storage->fetch('test'));
68+
}
69+
5870
public function testFetchNonExistingState()
5971
{
6072
$cacheItem = $this->createMock(CacheItemInterface::class);

0 commit comments

Comments
 (0)
0