Skip to content

Commit

Permalink
Merge pull request #7 from worksome/disallow_resolve_function
Browse files Browse the repository at this point in the history
Adds `resolve` to forbidden function all along with `app`
  • Loading branch information
lukeraymonddowning authored Jan 12, 2022
2 parents ed82c57 + 72d1543 commit 4faedf7
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/PHPStan/Laravel/DisallowAppHelperUsageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
*/
class DisallowAppHelperUsageRule implements Rule
{
const FUNCTION_NAME = 'app';
private array $forbiddenFunctions = [
'app',
'resolve',
];

public function getNodeType(): string
{
Expand All @@ -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(),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
[
Expand All @@ -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',
],
Expand All @@ -36,11 +46,25 @@
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',
[
'Usage of app helper is disallowed. Use dependency injection instead.',
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,
],
],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Resolve\Services;

class CallsResolveHelper
{
public function method()
{
return resolve('myService');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Resolve\Services;

class CallsResolveHelper
{
public function method()
{
return \resolve('myService')->execute();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class CallsResolveHelper
{
public function method()
{
return \resolve('myService', ['foo' => 'bar']);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class CallsOtherResolveHelper
{
public function container()
{
return \MyCustom\resolve();
}
}

?>

0 comments on commit 4faedf7

Please sign in to comment.