Skip to content

Commit feba1e4

Browse files
committed
Refactor CacheHandlerInstantiationRule
1 parent 2e1640c commit feba1e4

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

docs/rules.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All rules of this extension is summarised below:
44

55
## Classes
66

7+
### CacheHandlerInstantiationRule
8+
9+
**Class:** `CodeIgniter\PHPStan\Rules\Classes\CacheHandlerInstantiationRule`<br/>
10+
Fixable: No
11+
12+
Simply instantiating cache handlers using `new` is incomplete. The public `initialize()` method
13+
needs to be called on the instance so that the required initializations are done. This is usually
14+
achieved by using `CacheFactory::getHandler()` or using the `cache()` function.
15+
716
### FrameworkExceptionInstantiationRule
817

918
**Class:** `CodeIgniter\PHPStan\Rules\Classes\FrameworkExceptionInstantiationRule`<br/>

src/Rules/Classes/CacheHandlerInstantiationRule.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public function processNode(Node $node, Scope $scope): array
5959

6060
return [
6161
RuleErrorBuilder::message(sprintf(
62-
'Calling new %s() directly is incomplete to get the cache instance.',
63-
$reflection->getNativeReflection()->getShortName(),
62+
'Instantiating "%s" using new is incomplete to get a fully configured cache instance.',
63+
$reflection->getName(),
6464
))
65-
->tip('Use CacheFactory::getHandler() or the cache() function to get the cache instance instead.')
65+
->tip('Use "CacheFactory::getHandler()" or the "cache()" function instead.')
6666
->identifier('codeigniter.cacheHandlerInstance')
6767
->build(),
6868
];

tests/Rules/Classes/CacheHandlerInstantiationRuleTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace CodeIgniter\PHPStan\Tests\Rules\Classes;
1515

1616
use CodeIgniter\PHPStan\Rules\Classes\CacheHandlerInstantiationRule;
17-
use CodeIgniter\PHPStan\Tests\AdditionalConfigFilesTrait;
17+
use CodeIgniter\PHPStan\Tests\AdditionalConfigFilesProvider;
1818
use PHPStan\Rules\Rule;
1919
use PHPStan\Testing\RuleTestCase;
2020
use PHPUnit\Framework\Attributes\Group;
@@ -24,10 +24,10 @@
2424
*
2525
* @extends RuleTestCase<CacheHandlerInstantiationRule>
2626
*/
27-
#[Group('Integration')]
27+
#[Group('static-analysis')]
2828
final class CacheHandlerInstantiationRuleTest extends RuleTestCase
2929
{
30-
use AdditionalConfigFilesTrait;
30+
use AdditionalConfigFilesProvider;
3131

3232
protected function getRule(): Rule
3333
{
@@ -36,16 +36,16 @@ protected function getRule(): Rule
3636

3737
public function testRule(): void
3838
{
39-
$this->analyse([__DIR__ . '/data/cache-handler.php'], [
39+
$this->analyse([__DIR__ . '/../../data/rules/cache-handler.php'], [
4040
[
41-
'Calling new FileHandler() directly is incomplete to get the cache instance.',
41+
'Instantiating "CodeIgniter\Cache\Handlers\FileHandler" using new is incomplete to get a fully configured cache instance.',
4242
19,
43-
'Use CacheFactory::getHandler() or the cache() function to get the cache instance instead.',
43+
'Use "CacheFactory::getHandler()" or the "cache()" function instead.',
4444
],
4545
[
46-
'Calling new RedisHandler() directly is incomplete to get the cache instance.',
46+
'Instantiating "CodeIgniter\Cache\Handlers\RedisHandler" using new is incomplete to get a fully configured cache instance.',
4747
20,
48-
'Use CacheFactory::getHandler() or the cache() function to get the cache instance instead.',
48+
'Use "CacheFactory::getHandler()" or the "cache()" function instead.',
4949
],
5050
]);
5151
}

tests/Rules/Classes/data/cache-handler.php renamed to tests/data/rules/cache-handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
$handler1 = new FileHandler(new Cache());
2020
$handler2 = new RedisHandler(new Cache());
21-
$handler3 = new MockCache();
21+
$handler3 = new MockCache(); // This should not trigger the rule as it's a mock class used in tests.
2222

2323
$cache1 = $handler1->get('foo');
2424
$cache2 = $handler2->get('bar');

0 commit comments

Comments
 (0)