Skip to content

Commit

Permalink
Limit rule to package configs and models only
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Oct 21, 2023
1 parent 542275a commit 96d29e0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
18 changes: 16 additions & 2 deletions src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
*/
final class NoClassConstFetchOnFactoriesFunctions implements Rule
{
/**
* @var array<string, string>
*/
private static array $namespaceMap = [
'config' => 'Config',
'model' => 'App\\Models',
];

public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly FactoriesReturnTypeHelper $factoriesReturnTypeHelper
Expand Down Expand Up @@ -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(),
];
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/Type/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ function bar(string $name): void

assertType('null', config($name));
}

assertType('CodeIgniter\Shield\Config\AuthJWT', config(\CodeIgniter\Shield\Config\AuthJWT::class));
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 96d29e0

Please sign in to comment.