|
10 | 10 | use PHPStan\Rules\RuleErrorBuilder;
|
11 | 11 | use Sfp\ResourceOperations\ResourceOperations;
|
12 | 12 |
|
| 13 | +use function in_array; |
| 14 | +use function sprintf; |
| 15 | +use function strtolower; |
| 16 | + |
13 | 17 | /**
|
14 | 18 | * @implements Rule<Node\Expr\MethodCall>
|
15 | 19 | */
|
16 | 20 | final class ResourceOperationMethodCallRule implements Rule
|
17 | 21 | {
|
18 |
| - public function getNodeType(): string |
19 |
| - { |
20 |
| - return Node\Expr\MethodCall::class; |
21 |
| - } |
22 |
| - |
23 |
| - public function processNode(Node $node, Scope $scope): array |
24 |
| - { |
25 |
| - if (! $node->name instanceof Node\Identifier) { |
26 |
| - // @codeCoverageIgnoreStart |
27 |
| - return []; // @codeCoverageIgnoreEnd |
28 |
| - } |
29 |
| - |
30 |
| - if ($scope->getFunctionName() !== '__construct') { |
31 |
| - return []; |
32 |
| - } |
33 |
| - |
34 |
| - $calledOnType = $scope->getType($node->var); |
35 |
| - |
36 |
| - $methodNames = []; |
37 |
| - foreach ($calledOnType->getObjectClassNames() as $objectClassName) { |
38 |
| - $methodNames [] = $objectClassName . '::' .strtolower($node->name->name); |
39 |
| - } |
40 |
| - |
41 |
| - $errors = []; |
42 |
| - foreach ($methodNames as $methodName) { |
43 |
| - if (in_array($methodName, ResourceOperations::getMethods(), true)) { |
44 |
| - $errors[] = RuleErrorBuilder::message( |
45 |
| - sprintf("Don't resource operation inside constructor. Method %s() is called.", $methodName) |
46 |
| - )->identifier('sfp-dont-operation.resourceOperationMethodCall')->build(); |
47 |
| - } |
48 |
| - } |
49 |
| - |
50 |
| - return $errors; |
51 |
| - } |
| 22 | + public function getNodeType(): string |
| 23 | + { |
| 24 | + return Node\Expr\MethodCall::class; |
| 25 | + } |
| 26 | + |
| 27 | + public function processNode(Node $node, Scope $scope): array |
| 28 | + { |
| 29 | + if (! $node->name instanceof Node\Identifier) { |
| 30 | + // @codeCoverageIgnoreStart |
| 31 | + return []; // @codeCoverageIgnoreEnd |
| 32 | + } |
| 33 | + |
| 34 | + if ($scope->getFunctionName() !== '__construct') { |
| 35 | + return []; |
| 36 | + } |
| 37 | + |
| 38 | + $calledOnType = $scope->getType($node->var); |
| 39 | + |
| 40 | + $methodNames = []; |
| 41 | + foreach ($calledOnType->getObjectClassNames() as $objectClassName) { |
| 42 | + $methodNames [] = $objectClassName . '::' . strtolower($node->name->name); |
| 43 | + } |
| 44 | + |
| 45 | + $errors = []; |
| 46 | + foreach ($methodNames as $methodName) { |
| 47 | + if (in_array($methodName, ResourceOperations::getMethods(), true)) { |
| 48 | + $errors[] = RuleErrorBuilder::message( |
| 49 | + sprintf("Don't resource operation inside constructor. Method %s() is called.", $methodName) |
| 50 | + )->identifier('sfp-dont-operation.resourceOperationMethodCall')->build(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return $errors; |
| 55 | + } |
52 | 56 | }
|
0 commit comments