diff --git a/src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php b/src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php index 3596732..db6bdb4 100644 --- a/src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php +++ b/src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php @@ -26,6 +26,14 @@ */ final class NoClassConstFetchOnFactoriesFunctions implements Rule { + /** + * @var array + */ + private static array $namespaceMap = [ + 'config' => 'Config', + 'model' => 'App\\Models', + ]; + public function __construct( private readonly ReflectionProvider $reflectionProvider, private readonly FactoriesReturnTypeHelper $factoriesReturnTypeHelper @@ -92,15 +100,21 @@ public function processNode(Node $node, Scope $scope): array return []; } + $reflection = $reflections[0]; + + if ($reflection->getNativeReflection()->getNamespaceName() === self::$namespaceMap[$function]) { + return []; + } + return [ RuleErrorBuilder::message(sprintf( 'Call to function %s with %s::class is discouraged.', $function, - $reflections[0]->getDisplayName() + $reflection->getDisplayName() ))->tip(sprintf( 'Use %s(\'%s\') instead to allow overriding.', $function, - $reflections[0]->getNativeReflection()->getShortName() + $reflection->getNativeReflection()->getShortName() ))->identifier('codeigniter.factoriesClassConstFetch')->build(), ]; } diff --git a/tests/Fixtures/Type/config.php b/tests/Fixtures/Type/config.php index 9956fed..8193d41 100644 --- a/tests/Fixtures/Type/config.php +++ b/tests/Fixtures/Type/config.php @@ -34,3 +34,5 @@ function bar(string $name): void assertType('null', config($name)); } + +assertType('CodeIgniter\Shield\Config\AuthJWT', config(\CodeIgniter\Shield\Config\AuthJWT::class)); diff --git a/tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php b/tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php index cceec21..6bca8cd 100644 --- a/tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php +++ b/tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php @@ -46,14 +46,10 @@ public function testRule(): void __DIR__ . '/../../Fixtures/Type/model.php', ], [ [ - 'Call to function config with Config\App::class is discouraged.', - 26, - 'Use config(\'App\') instead to allow overriding.', + 'Call to function config with CodeIgniter\Shield\Config\AuthJWT::class is discouraged.', + 38, + 'Use config(\'AuthJWT\') instead to allow overriding.', ], - [ - 'Call to function config with Config\Cache::class is discouraged.', - 19, - 'Use config(\'Cache\') instead to allow overriding.', ], [ 'Call to function model with stdClass::class is discouraged.', 19,