Skip to content

Commit 96d29e0

Browse files
committed
Limit rule to package configs and models only
1 parent 542275a commit 96d29e0

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
*/
2727
final class NoClassConstFetchOnFactoriesFunctions implements Rule
2828
{
29+
/**
30+
* @var array<string, string>
31+
*/
32+
private static array $namespaceMap = [
33+
'config' => 'Config',
34+
'model' => 'App\\Models',
35+
];
36+
2937
public function __construct(
3038
private readonly ReflectionProvider $reflectionProvider,
3139
private readonly FactoriesReturnTypeHelper $factoriesReturnTypeHelper
@@ -92,15 +100,21 @@ public function processNode(Node $node, Scope $scope): array
92100
return [];
93101
}
94102

103+
$reflection = $reflections[0];
104+
105+
if ($reflection->getNativeReflection()->getNamespaceName() === self::$namespaceMap[$function]) {
106+
return [];
107+
}
108+
95109
return [
96110
RuleErrorBuilder::message(sprintf(
97111
'Call to function %s with %s::class is discouraged.',
98112
$function,
99-
$reflections[0]->getDisplayName()
113+
$reflection->getDisplayName()
100114
))->tip(sprintf(
101115
'Use %s(\'%s\') instead to allow overriding.',
102116
$function,
103-
$reflections[0]->getNativeReflection()->getShortName()
117+
$reflection->getNativeReflection()->getShortName()
104118
))->identifier('codeigniter.factoriesClassConstFetch')->build(),
105119
];
106120
}

tests/Fixtures/Type/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ function bar(string $name): void
3434

3535
assertType('null', config($name));
3636
}
37+
38+
assertType('CodeIgniter\Shield\Config\AuthJWT', config(\CodeIgniter\Shield\Config\AuthJWT::class));

tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,10 @@ public function testRule(): void
4646
__DIR__ . '/../../Fixtures/Type/model.php',
4747
], [
4848
[
49-
'Call to function config with Config\App::class is discouraged.',
50-
26,
51-
'Use config(\'App\') instead to allow overriding.',
49+
'Call to function config with CodeIgniter\Shield\Config\AuthJWT::class is discouraged.',
50+
38,
51+
'Use config(\'AuthJWT\') instead to allow overriding.',
5252
],
53-
[
54-
'Call to function config with Config\Cache::class is discouraged.',
55-
19,
56-
'Use config(\'Cache\') instead to allow overriding.', ],
5753
[
5854
'Call to function model with stdClass::class is discouraged.',
5955
19,

0 commit comments

Comments
 (0)