Skip to content

Commit b00a2c7

Browse files
[console] [7.3] Replace input/output with symfony style if used in InvokableCommandInputAttributeRector (#838)
* Create command_with_io.php.inc * dx * extract ConsoleOptionAndArgumentMethodCallVariableReplacer * pass symfony style via execute() if created inside * remove input/output if not used --------- Co-authored-by: darthf1 <darthf1@users.noreply.github.com>
1 parent 398a272 commit b00a2c7

16 files changed

+308
-73
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;
4+
5+
use Symfony\Component\Console\Attribute\AsCommand;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
use Symfony\Component\Console\Style\SymfonyStyle;
10+
11+
#[AsCommand('app:my-command')]
12+
final class MyCommand extends Command
13+
{
14+
protected function execute(InputInterface $input, OutputInterface $output): int
15+
{
16+
$io = new SymfonyStyle($input, $output);
17+
$io->info('Great success!');
18+
19+
return 1;
20+
}
21+
}
22+
?>
23+
-----
24+
<?php
25+
26+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;
27+
28+
use Symfony\Component\Console\Attribute\AsCommand;
29+
use Symfony\Component\Console\Command\Command;
30+
use Symfony\Component\Console\Input\InputInterface;
31+
use Symfony\Component\Console\Output\OutputInterface;
32+
use Symfony\Component\Console\Style\SymfonyStyle;
33+
34+
#[AsCommand('app:my-command')]
35+
final class MyCommand
36+
{
37+
public function __invoke(\Symfony\Component\Console\Style\SymfonyStyle $io): int
38+
{
39+
$io->info('Great success!');
40+
return 1;
41+
}
42+
}
43+
?>

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
@@ -45,7 +45,7 @@ class NameWithHyphen
4545
{
4646
public function __invoke(#[\Symfony\Component\Console\Attribute\Argument(name: 'argument-with-hyphen', description: 'Argument description')]
4747
?string $argument_with_hyphen, #[\Symfony\Component\Console\Attribute\Option]
48-
$option_with_hyphen, OutputInterface $output): int
48+
$option_with_hyphen): int
4949
{
5050
$argument = $argument_with_hyphen;
5151
$option = $option_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
@@ -47,7 +47,7 @@ final class SomeCommand
4747
{
4848
public function __invoke(#[\Symfony\Component\Console\Attribute\Argument(name: 'argument', description: 'Argument description')]
4949
string $argument, #[\Symfony\Component\Console\Attribute\Option]
50-
$option, OutputInterface $output): int
50+
$option): int
5151
{
5252
$someArgument = $argument;
5353
$someOption = $option;

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
@@ -48,7 +48,7 @@ final class SomeCommandWithMethodChaining
4848
{
4949
public function __invoke(#[\Symfony\Component\Console\Attribute\Argument(name: 'argument', description: 'Argument description')]
5050
string $argument, #[\Symfony\Component\Console\Attribute\Option]
51-
$option, OutputInterface $output): int
51+
$option): int
5252
{
5353
$someArgument = $argument;
5454
$someOption = $option;

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
@@ -53,7 +53,7 @@ final class SomeCommandWithSetHelp
5353

5454
public function __invoke(#[\Symfony\Component\Console\Attribute\Argument(name: 'argument', description: 'Argument description')]
5555
string $argument, #[\Symfony\Component\Console\Attribute\Option]
56-
$option, OutputInterface $output): int
56+
$option): int
5757
{
5858
$someArgument = $argument;
5959
$someOption = $option;

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,22 @@ use Symfony\Component\Console\Input\InputOption;
5151
#[AsCommand(name: 'some_name')]
5252
final class WithMultipleArgumentsOptionsFluent
5353
{
54-
public function __invoke(#[\Symfony\Component\Console\Attribute\Argument(name: 'argument1', description: 'Argument1 description')]
55-
string $argument1, #[\Symfony\Component\Console\Attribute\Argument(name: 'argument2', description: 'Argument2 description')]
56-
string $argument2, #[\Symfony\Component\Console\Attribute\Option]
57-
$option1, #[\Symfony\Component\Console\Attribute\Option]
58-
$option2, OutputInterface $output): int
54+
public function __invoke(
55+
#[\Symfony\Component\Console\Attribute\Argument(name: 'argument1', description: 'Argument1 description')]
56+
string $argument1,
57+
#[\Symfony\Component\Console\Attribute\Argument(name: 'argument2', description: 'Argument2 description')]
58+
string $argument2,
59+
#[\Symfony\Component\Console\Attribute\Option]
60+
$option1,
61+
#[\Symfony\Component\Console\Attribute\Option]
62+
$option2
63+
): int
5964
{
6065
$arg1 = $argument1;
6166
$arg2 = $argument2;
6267
$opt1 = $option1;
6368
$opt2 = $option2;
64-
6569
// ...
66-
6770
return 1;
6871
}
6972
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,22 @@ use Symfony\Component\Console\Input\InputOption;
5656
#[AsCommand(name: 'some_name')]
5757
final class WithMultipleArgumentsOptionsNotFluent
5858
{
59-
public function __invoke(#[\Symfony\Component\Console\Attribute\Argument(name: 'argument1', description: 'Argument1 description')]
60-
string $argument1, #[\Symfony\Component\Console\Attribute\Argument(name: 'argument2', description: 'Argument2 description')]
61-
string $argument2, #[\Symfony\Component\Console\Attribute\Option]
62-
$option1, #[\Symfony\Component\Console\Attribute\Option]
63-
$option2, OutputInterface $output): int
59+
public function __invoke(
60+
#[\Symfony\Component\Console\Attribute\Argument(name: 'argument1', description: 'Argument1 description')]
61+
string $argument1,
62+
#[\Symfony\Component\Console\Attribute\Argument(name: 'argument2', description: 'Argument2 description')]
63+
string $argument2,
64+
#[\Symfony\Component\Console\Attribute\Option]
65+
$option1,
66+
#[\Symfony\Component\Console\Attribute\Option]
67+
$option2
68+
): int
6469
{
6570
$arg1 = $argument1;
6671
$arg2 = $argument2;
6772
$opt1 = $option1;
6873
$opt2 = $option2;
69-
7074
// ...
71-
7275
return 1;
7376
}
7477
}

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
@@ -47,7 +47,7 @@ final class WithOptionalArgument
4747
{
4848
public function __invoke(#[\Symfony\Component\Console\Attribute\Argument(name: 'argument', description: 'Argument description')]
4949
?string $argument, #[\Symfony\Component\Console\Attribute\Option]
50-
$option, OutputInterface $output): int
50+
$option): int
5151
{
5252
$someArgument = $argument;
5353
$someOption = $option;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ final class WithOverride
5151
#[\Symfony\Component\Console\Attribute\Argument(name: 'argument', description: 'Argument description')]
5252
string $argument,
5353
#[\Symfony\Component\Console\Attribute\Option]
54-
$option,
55-
OutputInterface $output
54+
$option
5655
): int
5756
{
5857
$someArgument = $argument;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ use Symfony\Component\Console\Output\OutputInterface;
3232
#[AsCommand(name: 'some_name')]
3333
final class WithoutConfigure
3434
{
35-
public function __invoke(OutputInterface $output): int
35+
public function __invoke(): int
3636
{
3737
// ...
38-
3938
return 1;
4039
}
4140
}

0 commit comments

Comments
 (0)