Skip to content

Commit 8f8e0c3

Browse files
committed
Skip Mockcache on disallowed direct cache call
1 parent f4c4ec5 commit 8f8e0c3

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

src/Rules/Classes/CacheHandlerInstantiationRule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\Cache\CacheFactory;
1717
use CodeIgniter\Cache\CacheInterface;
18+
use CodeIgniter\Test\Mock\MockCache;
1819
use PhpParser\Node;
1920
use PHPStan\Analyser\Scope;
2021
use PHPStan\Rules\Rule;
@@ -51,6 +52,10 @@ public function processNode(Node $node, Scope $scope): array
5152
return [];
5253
}
5354

55+
if ($reflection->getName() === MockCache::class) {
56+
return [];
57+
}
58+
5459
if ($scope->isInClass() && $scope->getClassReflection()->getName() === CacheFactory::class) {
5560
return [];
5661
}

tests/Fixtures/Rules/Classes/cache-handler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313

1414
use CodeIgniter\Cache\Handlers\FileHandler;
1515
use CodeIgniter\Cache\Handlers\RedisHandler;
16+
use CodeIgniter\Test\Mock\MockCache;
1617
use Config\Cache;
1718

1819
$handler1 = new FileHandler(new Cache());
1920
$handler2 = new RedisHandler(new Cache());
21+
$handler3 = new MockCache();
2022

2123
$cache1 = $handler1->get('foo');
2224
$cache2 = $handler2->get('bar');
25+
$cache3 = $handler3->get('baz');

tests/Rules/Classes/CacheHandlerInstantiationRuleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public function testRule(): void
3939
$this->analyse([__DIR__ . '/../../Fixtures/Rules/Classes/cache-handler.php'], [
4040
[
4141
'Calling new FileHandler() directly is incomplete to get the cache instance.',
42-
18,
42+
19,
4343
'Use CacheFactory::getHandler() or the cache() function to get the cache instance instead.',
4444
],
4545
[
4646
'Calling new RedisHandler() directly is incomplete to get the cache instance.',
47-
19,
47+
20,
4848
'Use CacheFactory::getHandler() or the cache() function to get the cache instance instead.',
4949
],
5050
]);

0 commit comments

Comments
 (0)