Skip to content

Commit 1a199fc

Browse files
authored
[Symfony 7.3] Handle commands without configure method in InvokableCommandInputAttributeRector (#797)
1 parent df74c8a commit 1a199fc

File tree

2 files changed

+66
-19
lines changed

2 files changed

+66
-19
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+
10+
#[AsCommand(name: 'some_name')]
11+
final class WithoutConfigure extends Command
12+
{
13+
protected function execute(InputInterface $input, OutputInterface $output): int
14+
{
15+
// ...
16+
17+
return 1;
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;
26+
27+
use Symfony\Component\Console\Attribute\AsCommand;
28+
use Symfony\Component\Console\Command\Command;
29+
use Symfony\Component\Console\Input\InputInterface;
30+
use Symfony\Component\Console\Output\OutputInterface;
31+
32+
#[AsCommand(name: 'some_name')]
33+
final class WithoutConfigure
34+
{
35+
public function __invoke(OutputInterface $output): int
36+
{
37+
// ...
38+
39+
return 1;
40+
}
41+
}
42+
43+
?>

rules/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,7 @@ public function refactor(Node $node): ?Class_
136136
return null;
137137
}
138138

139-
// 1. fetch configure method to get arguments and options metadata
140-
$configureClassMethod = $node->getMethod(CommandMethodName::CONFIGURE);
141-
if (! $configureClassMethod instanceof ClassMethod) {
142-
return null;
143-
}
144-
145-
// 2. rename execute to __invoke
139+
// 1. rename execute to __invoke
146140
$executeClassMethod = $node->getMethod(CommandMethodName::EXECUTE);
147141
if (! $executeClassMethod instanceof ClassMethod) {
148142
return null;
@@ -151,19 +145,27 @@ public function refactor(Node $node): ?Class_
151145
$executeClassMethod->name = new Identifier('__invoke');
152146
$this->visibilityManipulator->makePublic($executeClassMethod);
153147

154-
// 3. create arguments and options parameters
155-
// @todo
156-
$commandArguments = $this->commandArgumentsAndOptionsResolver->collectCommandArguments(
157-
$configureClassMethod
158-
);
148+
// 2. fetch configure method to get arguments and options metadata
149+
$configureClassMethod = $node->getMethod(CommandMethodName::CONFIGURE);
159150

160-
$commandOptions = $this->commandArgumentsAndOptionsResolver->collectCommandOptions($configureClassMethod);
151+
if ($configureClassMethod instanceof ClassMethod) {
152+
// 3. create arguments and options parameters
153+
// @todo
154+
$commandArguments = $this->commandArgumentsAndOptionsResolver->collectCommandArguments(
155+
$configureClassMethod
156+
);
161157

162-
// 4. remove configure() method
163-
$this->removeConfigureClassMethod($node);
158+
$commandOptions = $this->commandArgumentsAndOptionsResolver->collectCommandOptions($configureClassMethod);
159+
160+
// 4. remove configure() method
161+
$this->removeConfigureClassMethod($node);
162+
163+
// 5. decorate __invoke method with attributes
164+
$invokeParams = $this->commandInvokeParamsFactory->createParams($commandArguments, $commandOptions);
165+
} else {
166+
$invokeParams = [];
167+
}
164168

165-
// 5. decorate __invoke method with attributes
166-
$invokeParams = $this->commandInvokeParamsFactory->createParams($commandArguments, $commandOptions);
167169
$executeClassMethod->params = array_merge($invokeParams, [$executeClassMethod->params[1]]);
168170

169171
// 6. remove parent class
@@ -181,8 +183,10 @@ public function refactor(Node $node): ?Class_
181183
}
182184
}
183185

184-
// 7. replace input->getArgument() and input->getOption() calls with direct variable access
185-
$this->replaceInputArgumentOptionFetchWithVariables($executeClassMethod);
186+
if ($configureClassMethod instanceof ClassMethod) {
187+
// 7. replace input->getArgument() and input->getOption() calls with direct variable access
188+
$this->replaceInputArgumentOptionFetchWithVariables($executeClassMethod);
189+
}
186190

187191
return $node;
188192
}

0 commit comments

Comments
 (0)