|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Symplify\PHPStanRules\Rules\Symfony; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\Closure; |
| 9 | +use PhpParser\Node\Expr\ConstFetch; |
| 10 | +use PhpParser\Node\Expr\MethodCall; |
| 11 | +use PhpParser\NodeFinder; |
| 12 | +use PHPStan\Analyser\Scope; |
| 13 | +use PHPStan\Rules\Rule; |
| 14 | +use PHPStan\Rules\RuleErrorBuilder; |
| 15 | +use Symplify\PHPStanRules\Enum\RuleIdentifier\SymfonyRuleIdentifier; |
| 16 | +use Symplify\PHPStanRules\Helper\NamingHelper; |
| 17 | +use Symplify\PHPStanRules\Symfony\NodeAnalyzer\SymfonyClosureDetector; |
| 18 | + |
| 19 | +/** |
| 20 | + * @implements Rule<Closure> |
| 21 | + */ |
| 22 | +final class NoServiceAutowireDuplicateRule implements Rule |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + public const ERROR_MESSAGE = 'Service autowire() is called as duplicate of $services->defaults()->autowire(). Remove it on the service'; |
| 28 | + |
| 29 | + public function getNodeType(): string |
| 30 | + { |
| 31 | + return Closure::class; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @param Closure $node |
| 36 | + */ |
| 37 | + public function processNode(Node $node, Scope $scope): array |
| 38 | + { |
| 39 | + if (! SymfonyClosureDetector::detect($node)) { |
| 40 | + return []; |
| 41 | + } |
| 42 | + |
| 43 | + $ruleErrors = []; |
| 44 | + |
| 45 | + $hasDefaultsAutowire = false; |
| 46 | + |
| 47 | + foreach ($node->stmts as $stmt) { |
| 48 | + if ($this->hasAutowireDefaultsMethodCall($stmt)) { |
| 49 | + $hasDefaultsAutowire = true; |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + if (! $hasDefaultsAutowire) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + $serviceAutowireMethodCall = $this->matchServiceAutowireMethodCall($stmt); |
| 58 | + if (! $serviceAutowireMethodCall instanceof MethodCall) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + $ruleErrors[] = RuleErrorBuilder::message(self::ERROR_MESSAGE) |
| 63 | + ->line($serviceAutowireMethodCall->getLine()) |
| 64 | + ->identifier(SymfonyRuleIdentifier::RULE_IDENTIFIER) |
| 65 | + ->build(); |
| 66 | + } |
| 67 | + |
| 68 | + return $ruleErrors; |
| 69 | + } |
| 70 | + |
| 71 | + private function hasAutowireDefaultsMethodCall(Node $someNode): bool |
| 72 | + { |
| 73 | + $nodeFinder = new NodeFinder(); |
| 74 | + |
| 75 | + $autowireDefaultsMethodCall = $nodeFinder->findFirst($someNode, function (Node $node): bool { |
| 76 | + if (! $node instanceof MethodCall) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + if (! NamingHelper::isName($node->name, 'autowire')) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + if (! $node->var instanceof MethodCall) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + // dummy way to detect, @todo improve with possible type check |
| 89 | + return NamingHelper::isName($node->var->name, 'defaults'); |
| 90 | + }); |
| 91 | + |
| 92 | + return $autowireDefaultsMethodCall instanceof MethodCall; |
| 93 | + } |
| 94 | + |
| 95 | + private function matchServiceAutowireMethodCall(Node $someNode): ?MethodCall |
| 96 | + { |
| 97 | + $nodeFinder = new NodeFinder(); |
| 98 | + |
| 99 | + $foundNode = $nodeFinder->findFirst($someNode, function (Node $node): bool { |
| 100 | + if (! $node instanceof MethodCall) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + if (! NamingHelper::isName($node->name, 'autowire')) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + if ($node->getArgs() === []) { |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + $firstArg = $node->getArgs()[0]; |
| 113 | + if (! $firstArg->value instanceof ConstFetch) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + return $firstArg->value->name->toLowerString() === 'true'; |
| 118 | + }); |
| 119 | + |
| 120 | + /** @var MethodCall|null $foundNode */ |
| 121 | + return $foundNode; |
| 122 | + } |
| 123 | +} |
0 commit comments