Skip to content

Commit b73e4ad

Browse files
committed
fix moving option metadata
1 parent d037307 commit b73e4ad

File tree

12 files changed

+50
-14
lines changed

12 files changed

+50
-14
lines changed

rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/name_with_hyphen.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use Symfony\Component\Console\Output\OutputInterface;
4444
class NameWithHyphen
4545
{
4646
public function __invoke(#[\Symfony\Component\Console\Attribute\Argument(name: 'argument-with-hyphen', description: 'Argument description')]
47-
?string $argument_with_hyphen, #[\Symfony\Component\Console\Attribute\Option]
47+
?string $argument_with_hyphen, #[\Symfony\Component\Console\Attribute\Option(name: 'option-with-hyphen')]
4848
$option_with_hyphen): int
4949
{
5050
$argument = $argument_with_hyphen;

rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/some_command.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use Symfony\Component\Console\Input\InputOption;
4646
final class SomeCommand
4747
{
4848
public function __invoke(#[\Symfony\Component\Console\Attribute\Argument(name: 'argument', description: 'Argument description')]
49-
string $argument, #[\Symfony\Component\Console\Attribute\Option]
49+
string $argument, #[\Symfony\Component\Console\Attribute\Option(name: 'option', shortcut: 'o', mode: InputOption::VALUE_NONE, description: 'Option description')]
5050
$option): int
5151
{
5252
$someArgument = $argument;

rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/some_command_with_method_chaining.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use Symfony\Component\Console\Input\InputOption;
4747
final class SomeCommandWithMethodChaining
4848
{
4949
public function __invoke(#[\Symfony\Component\Console\Attribute\Argument(name: 'argument', description: 'Argument description')]
50-
string $argument, #[\Symfony\Component\Console\Attribute\Option]
50+
string $argument, #[\Symfony\Component\Console\Attribute\Option(name: 'option', shortcut: 'o', mode: InputOption::VALUE_NONE, description: 'Option description')]
5151
$option): int
5252
{
5353
$someArgument = $argument;

rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/some_command_with_set_help.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ final class SomeCommandWithSetHelp
5252
}
5353

5454
public function __invoke(#[\Symfony\Component\Console\Attribute\Argument(name: 'argument', description: 'Argument description')]
55-
string $argument, #[\Symfony\Component\Console\Attribute\Option]
55+
string $argument, #[\Symfony\Component\Console\Attribute\Option(name: 'option', shortcut: 'o', mode: InputOption::VALUE_NONE, description: 'Option description')]
5656
$option): int
5757
{
5858
$someArgument = $argument;

rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/with_multiple_arguments_options_fluent.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ final class WithMultipleArgumentsOptionsFluent
5656
string $argument1,
5757
#[\Symfony\Component\Console\Attribute\Argument(name: 'argument2', description: 'Argument2 description')]
5858
string $argument2,
59-
#[\Symfony\Component\Console\Attribute\Option]
59+
#[\Symfony\Component\Console\Attribute\Option(name: 'option1', shortcut: 'o', mode: InputOption::VALUE_NONE, description: 'Option1 description')]
6060
$option1,
61-
#[\Symfony\Component\Console\Attribute\Option]
61+
#[\Symfony\Component\Console\Attribute\Option(name: 'option2', shortcut: 'p', mode: InputOption::VALUE_NONE, description: 'Option2 description')]
6262
$option2
6363
): int
6464
{

rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/with_multiple_arguments_options_no_fluent.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ final class WithMultipleArgumentsOptionsNotFluent
6161
string $argument1,
6262
#[\Symfony\Component\Console\Attribute\Argument(name: 'argument2', description: 'Argument2 description')]
6363
string $argument2,
64-
#[\Symfony\Component\Console\Attribute\Option]
64+
#[\Symfony\Component\Console\Attribute\Option(name: 'option1', shortcut: 'o', mode: InputOption::VALUE_NONE, description: 'Option1 description')]
6565
$option1,
66-
#[\Symfony\Component\Console\Attribute\Option]
66+
#[\Symfony\Component\Console\Attribute\Option(name: 'option2', shortcut: 'p', mode: InputOption::VALUE_NONE, description: 'Option2 description')]
6767
$option2
6868
): int
6969
{

rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/with_optional_argument.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use Symfony\Component\Console\Input\InputOption;
4646
final class WithOptionalArgument
4747
{
4848
public function __invoke(#[\Symfony\Component\Console\Attribute\Argument(name: 'argument', description: 'Argument description')]
49-
?string $argument, #[\Symfony\Component\Console\Attribute\Option]
49+
?string $argument, #[\Symfony\Component\Console\Attribute\Option(name: 'option', shortcut: 'o', mode: InputOption::VALUE_NONE, description: 'Option description')]
5050
$option): int
5151
{
5252
$someArgument = $argument;

rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/with_override.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ final class WithOverride
5050
public function __invoke(
5151
#[\Symfony\Component\Console\Attribute\Argument(name: 'argument', description: 'Argument description')]
5252
string $argument,
53-
#[\Symfony\Component\Console\Attribute\Option]
53+
#[\Symfony\Component\Console\Attribute\Option(name: 'option', shortcut: 'o', mode: InputOption::VALUE_NONE, description: 'Option description')]
5454
$option
5555
): int
5656
{

rules/Symfony73/NodeAnalyzer/CommandOptionsResolver.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public function resolve(ClassMethod $configureClassMethod): array
2424
{
2525
$addOptionMethodCalls = $this->methodCallFinder->find($configureClassMethod, 'addOption');
2626

27-
$commandOptionMetadatas = [];
27+
$commandOptions = [];
28+
2829
foreach ($addOptionMethodCalls as $addOptionMethodCall) {
2930
$addOptionArgs = $addOptionMethodCall->getArgs();
3031

@@ -34,9 +35,13 @@ public function resolve(ClassMethod $configureClassMethod): array
3435
throw new ShouldNotHappenException('Option name is required');
3536
}
3637

37-
$commandOptionMetadatas[] = new CommandOption($nameArgValue);
38+
$shortcutExpr = $addOptionArgs[1]?->value ?? null;
39+
$modeExpr = $addOptionArgs[2]?->value ?? null;
40+
$descriptionExpr = $addOptionArgs[3]?->value ?? null;
41+
42+
$commandOptions[] = new CommandOption($nameArgValue, $shortcutExpr, $modeExpr, $descriptionExpr);
3843
}
3944

40-
return $commandOptionMetadatas;
45+
return $commandOptions;
4146
}
4247
}

rules/Symfony73/NodeFactory/CommandInvokeParamsFactory.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node\Arg;
88
use PhpParser\Node\Attribute;
99
use PhpParser\Node\AttributeGroup;
10+
use PhpParser\Node\Expr;
1011
use PhpParser\Node\Expr\Variable;
1112
use PhpParser\Node\Identifier;
1213
use PhpParser\Node\Name\FullyQualified;
@@ -93,6 +94,18 @@ private function createOptionParams(array $commandOptions): array
9394

9495
$optionArgs = [new Arg(value: $commandOption->getName(), name: new Identifier('name'))];
9596

97+
if ($commandOption->getShortcut() instanceof Expr) {
98+
$optionArgs[] = new Arg(value: $commandOption->getShortcut(), name: new Identifier('shortcut'));
99+
}
100+
101+
if ($commandOption->getMode() instanceof Expr) {
102+
$optionArgs[] = new Arg(value: $commandOption->getMode(), name: new Identifier('mode'));
103+
}
104+
105+
if ($commandOption->getDescription() instanceof Expr) {
106+
$optionArgs[] = new Arg(value: $commandOption->getDescription(), name: new Identifier('description'));
107+
}
108+
96109
$optionParam->attrGroups[] = new AttributeGroup([
97110
new Attribute(new FullyQualified(SymfonyAttribute::COMMAND_OPTION), $optionArgs),
98111
]);

0 commit comments

Comments
 (0)