diff --git a/src/PHPStan/Laravel/DisallowAppHelperUsageRule.php b/src/PHPStan/Laravel/DisallowAppHelperUsageRule.php index 083c38b..0773b49 100644 --- a/src/PHPStan/Laravel/DisallowAppHelperUsageRule.php +++ b/src/PHPStan/Laravel/DisallowAppHelperUsageRule.php @@ -13,7 +13,10 @@ */ class DisallowAppHelperUsageRule implements Rule { - const FUNCTION_NAME = 'app'; + private array $forbiddenFunctions = [ + 'app', + 'resolve', + ]; public function getNodeType(): string { @@ -33,13 +36,13 @@ public function processNode(Node $node, Scope $scope): array $functionName = $nodeName->toString(); - if ($functionName !== self::FUNCTION_NAME) { + if (! in_array($functionName, $this->forbiddenFunctions)) { return []; } return [ RuleErrorBuilder::message( - "Usage of app helper is disallowed. Use dependency injection instead." + "Usage of {$functionName} helper is disallowed. Use dependency injection instead." )->build(), ]; } diff --git a/tests/PHPStan/Laravel/DisallowAppUsageRule/DisallowAppUsageRuleTest.php b/tests/PHPStan/Laravel/DisallowAppUsageRule/DisallowAppUsageRuleTest.php index d802b8c..66d251f 100644 --- a/tests/PHPStan/Laravel/DisallowAppUsageRule/DisallowAppUsageRuleTest.php +++ b/tests/PHPStan/Laravel/DisallowAppUsageRule/DisallowAppUsageRuleTest.php @@ -16,6 +16,13 @@ 7, ], ], + 'calls resolve helper with parameters' => [ + __DIR__ . '/Fixture/resolve_helper_with_parameters.php.inc', + [ + 'Usage of resolve helper is disallowed. Use dependency injection instead.', + 7, + ], + ], 'calls app helper without args' => [ __DIR__ . '/Fixture/app_helper_without_arg.php.inc', [ @@ -26,6 +33,9 @@ 'skips app function from other namespace' => [ __DIR__ . '/Fixture/skip_app_function_in_namespace.php.inc', ], + 'skips resolve function from other namespace' => [ + __DIR__ . '/Fixture/skip_resolve_function_in_namespace.php.inc', + ], 'skips function call on variable' => [ __DIR__ . '/Fixture/skip_variable_call.php.inc', ], @@ -36,6 +46,13 @@ 9, ], ], + 'calls resolve helper when inside a namespace' => [ + __DIR__ . '/Fixture/resolve_helper_in_namespace.php.inc', + [ + 'Usage of resolve helper is disallowed. Use dependency injection instead.', + 9, + ], + ], 'calls app helper with chain calls after' => [ __DIR__ . '/Fixture/app_helper_with_chain_calls.php.inc', [ @@ -43,4 +60,11 @@ 9, ], ], + 'calls resolve helper with chain calls after' => [ + __DIR__ . '/Fixture/resolve_helper_with_chain_calls.php.inc', + [ + 'Usage of resolve helper is disallowed. Use dependency injection instead.', + 9, + ], + ], ]); diff --git a/tests/PHPStan/Laravel/DisallowAppUsageRule/Fixture/resolve_helper_in_namespace.php.inc b/tests/PHPStan/Laravel/DisallowAppUsageRule/Fixture/resolve_helper_in_namespace.php.inc new file mode 100644 index 0000000..a7688ac --- /dev/null +++ b/tests/PHPStan/Laravel/DisallowAppUsageRule/Fixture/resolve_helper_in_namespace.php.inc @@ -0,0 +1,13 @@ + diff --git a/tests/PHPStan/Laravel/DisallowAppUsageRule/Fixture/resolve_helper_with_chain_calls.php.inc b/tests/PHPStan/Laravel/DisallowAppUsageRule/Fixture/resolve_helper_with_chain_calls.php.inc new file mode 100644 index 0000000..751b647 --- /dev/null +++ b/tests/PHPStan/Laravel/DisallowAppUsageRule/Fixture/resolve_helper_with_chain_calls.php.inc @@ -0,0 +1,13 @@ +execute(); + } +} + +?> diff --git a/tests/PHPStan/Laravel/DisallowAppUsageRule/Fixture/resolve_helper_with_parameters.php.inc b/tests/PHPStan/Laravel/DisallowAppUsageRule/Fixture/resolve_helper_with_parameters.php.inc new file mode 100644 index 0000000..e1fbf73 --- /dev/null +++ b/tests/PHPStan/Laravel/DisallowAppUsageRule/Fixture/resolve_helper_with_parameters.php.inc @@ -0,0 +1,11 @@ + 'bar']); + } +} + +?> diff --git a/tests/PHPStan/Laravel/DisallowAppUsageRule/Fixture/skip_resolve_function_in_namespace.php.inc b/tests/PHPStan/Laravel/DisallowAppUsageRule/Fixture/skip_resolve_function_in_namespace.php.inc new file mode 100644 index 0000000..8c4dffa --- /dev/null +++ b/tests/PHPStan/Laravel/DisallowAppUsageRule/Fixture/skip_resolve_function_in_namespace.php.inc @@ -0,0 +1,11 @@ +