diff --git a/rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/with_named_arg_mode_different_position.php.inc b/rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/with_named_arg_mode_different_position.php.inc new file mode 100644 index 000000000..9b61f6156 --- /dev/null +++ b/rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/with_named_arg_mode_different_position.php.inc @@ -0,0 +1,60 @@ +addOption( + name: 'startDate', + mode: InputOption::VALUE_REQUIRED, + description: 'non empty description needed to reproduce the bug', + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + return Command::SUCCESS; + } +} + +?> +----- + \ No newline at end of file diff --git a/rules/Symfony73/NodeAnalyzer/CommandOptionsResolver.php b/rules/Symfony73/NodeAnalyzer/CommandOptionsResolver.php index 8219670ab..7e7bf456f 100644 --- a/rules/Symfony73/NodeAnalyzer/CommandOptionsResolver.php +++ b/rules/Symfony73/NodeAnalyzer/CommandOptionsResolver.php @@ -6,6 +6,7 @@ use PhpParser\Node\Arg; use PhpParser\Node\Expr; +use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Type\Type; use Rector\NodeTypeResolver\NodeTypeResolver; @@ -32,47 +33,43 @@ public function resolve(ClassMethod $configureClassMethod): array $commandOptions = []; foreach ($addOptionMethodCalls as $addOptionMethodCall) { - $addOptionArgs = $addOptionMethodCall->getArgs(); + $nameArg = $addOptionMethodCall->getArg('name', 0); + if (! $nameArg instanceof Arg) { + continue; + } - $optionName = $this->valueResolver->getValue($addOptionArgs[0]->value); - - $isImplicitBoolean = $this->isImplicitBoolean($addOptionArgs); + $optionName = $this->valueResolver->getValue($nameArg->value); + $isImplicitBoolean = $this->isImplicitBoolean($addOptionMethodCall); $commandOptions[] = new CommandOption( $optionName, - $addOptionArgs[0]->value, - $addOptionArgs[1]->value ?? null, - $addOptionArgs[2]->value ?? null, - $addOptionArgs[3]->value ?? null, - $addOptionArgs[4]->value ?? null, - $this->isArrayMode($addOptionArgs), + $nameArg->value, + $addOptionMethodCall->getArg('shortcut', 1)?->value, + $addOptionMethodCall->getArg('mode', 2)?->value, + $addOptionMethodCall->getArg('description', 3)?->value, + $addOptionMethodCall->getArg('default', 4)?->value, + $this->isArrayMode($addOptionMethodCall), $isImplicitBoolean, - $this->resolveDefaultType($addOptionArgs) + $this->resolveDefaultType($addOptionMethodCall) ); } return $commandOptions; } - /** - * @param Arg[] $args - */ - private function resolveDefaultType(array $args): ?Type + private function resolveDefaultType(MethodCall $methodCall): ?Type { - $defaultArg = $args[4] ?? null; - if (! $defaultArg instanceof Arg) { + $defaultExpr = $methodCall->getArg('default', 4)?->value; + if (! $defaultExpr instanceof Expr) { return null; } - return $this->nodeTypeResolver->getType($defaultArg->value); + return $this->nodeTypeResolver->getType($defaultExpr); } - /** - * @param Arg[] $args - */ - private function isArrayMode(array $args): bool + private function isArrayMode(MethodCall $methodCall): bool { - $modeExpr = $args[2]->value ?? null; + $modeExpr = $methodCall->getArg('mode', 2)?->value; if (! $modeExpr instanceof Expr) { return false; } @@ -82,12 +79,9 @@ private function isArrayMode(array $args): bool return (bool) ($modeValue & 8); } - /** - * @param Arg[] $args - */ - private function isImplicitBoolean(array $args): bool + private function isImplicitBoolean(MethodCall $methodCall): bool { - $modeExpr = $args[2]->value ?? null; + $modeExpr = $methodCall->getArg('mode', 2)?->value; if (! $modeExpr instanceof Expr) { return false; }