|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Symfony\Symfony73\NodeAnalyzer; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\MethodCall; |
| 9 | +use PhpParser\Node\Identifier; |
| 10 | +use PhpParser\Node\Scalar\String_; |
| 11 | +use PhpParser\Node\Stmt\ClassMethod; |
| 12 | +use PhpParser\NodeFinder; |
| 13 | +use Rector\Exception\ShouldNotHappenException; |
| 14 | +use Rector\Symfony\Symfony73\ValueObject\CommandArgument; |
| 15 | +use Rector\Symfony\Symfony73\ValueObject\CommandOption; |
| 16 | + |
| 17 | +final class CommandArgumentsAndOptionsResolver |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @return CommandArgument[] |
| 21 | + */ |
| 22 | + public function collectCommandArguments(ClassMethod $configureClassMethod): array |
| 23 | + { |
| 24 | + $addArgumentMethodCalls = $this->findMethodCallsByName($configureClassMethod, 'addArgument'); |
| 25 | + |
| 26 | + $commandArguments = []; |
| 27 | + foreach ($addArgumentMethodCalls as $addArgumentMethodCall) { |
| 28 | + // @todo extract name, type and requirements |
| 29 | + $addArgumentArgs = $addArgumentMethodCall->getArgs(); |
| 30 | + |
| 31 | + $nameArgValue = $addArgumentArgs[0]->value; |
| 32 | + if (! $nameArgValue instanceof String_) { |
| 33 | + // we need string value, otherwise param will not have a name |
| 34 | + throw new ShouldNotHappenException('Argument name is required'); |
| 35 | + } |
| 36 | + |
| 37 | + $optionName = $nameArgValue->value; |
| 38 | + |
| 39 | + $commandArguments[] = new CommandArgument($optionName); |
| 40 | + } |
| 41 | + |
| 42 | + return $commandArguments; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return CommandOption[] |
| 47 | + */ |
| 48 | + public function collectCommandOptions(ClassMethod $configureClassMethod): array |
| 49 | + { |
| 50 | + $addOptionMethodCalls = $this->findMethodCallsByName($configureClassMethod, 'addOption'); |
| 51 | + |
| 52 | + $commandOptionMetadatas = []; |
| 53 | + foreach ($addOptionMethodCalls as $addOptionMethodCall) { |
| 54 | + // @todo extract name, type and requirements |
| 55 | + $addOptionArgs = $addOptionMethodCall->getArgs(); |
| 56 | + |
| 57 | + $nameArgValue = $addOptionArgs[0]->value; |
| 58 | + if (! $nameArgValue instanceof String_) { |
| 59 | + // we need string value, otherwise param will not have a name |
| 60 | + throw new ShouldNotHappenException('Option name is required'); |
| 61 | + } |
| 62 | + |
| 63 | + $optionName = $nameArgValue->value; |
| 64 | + |
| 65 | + $commandOptionMetadatas[] = new CommandOption($optionName); |
| 66 | + } |
| 67 | + |
| 68 | + return $commandOptionMetadatas; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return MethodCall[] |
| 73 | + */ |
| 74 | + private function findMethodCallsByName(ClassMethod $classMethod, string $desiredMethodName): array |
| 75 | + { |
| 76 | + $nodeFinder = new NodeFinder(); |
| 77 | + |
| 78 | + return $nodeFinder->find($classMethod, function (Node $node) use ($desiredMethodName): bool { |
| 79 | + if (! $node instanceof MethodCall) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + if (! $node->name instanceof Identifier) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + return $node->name->toString() === $desiredMethodName; |
| 88 | + }); |
| 89 | + } |
| 90 | +} |
0 commit comments