Skip to content

Commit 664dd13

Browse files
committed
add fixture
1 parent d20ea2f commit 664dd13

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture\DefaultValue;
4+
5+
use Symfony\Component\Console\Attribute\AsCommand;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
#[AsCommand(
13+
name: 'app:hello',
14+
)]
15+
class ImplicitNoValueBoolOption extends Command
16+
{
17+
protected function configure(): void
18+
{
19+
$this->addOption('third', null, InputOption::VALUE_NONE);
20+
}
21+
protected function execute(InputInterface $input, OutputInterface $output): int
22+
{
23+
}
24+
}
25+
26+
?>
27+
-----
28+
<?php
29+
30+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture\DefaultValue;
31+
32+
use Symfony\Component\Console\Attribute\AsCommand;
33+
use Symfony\Component\Console\Command\Command;
34+
use Symfony\Component\Console\Input\InputArgument;
35+
use Symfony\Component\Console\Input\InputInterface;
36+
use Symfony\Component\Console\Input\InputOption;
37+
use Symfony\Component\Console\Output\OutputInterface;
38+
39+
#[AsCommand(
40+
name: 'app:hello',
41+
)]
42+
class ImplicitNoValueBoolOption
43+
{
44+
public function __invoke(
45+
#[\Symfony\Component\Console\Attribute\Option(name: 'third', mode: InputOption::VALUE_NONE)]
46+
bool $third = false
47+
): int
48+
{
49+
}
50+
}
51+
52+
?>

rules/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ public function refactor(Node $node): ?Class_
156156
if ($configureClassMethod instanceof ClassMethod) {
157157
// 3. create arguments and options parameters
158158
$commandArguments = $this->commandArgumentsResolver->resolve($configureClassMethod);
159-
160159
$commandOptions = $this->commandOptionsResolver->resolve($configureClassMethod);
161160

162161
// 4. remove configure() method
@@ -173,17 +172,7 @@ public function refactor(Node $node): ?Class_
173172
// 6. remove parent class
174173
$node->extends = null;
175174

176-
foreach ($executeClassMethod->attrGroups as $attrGroupKey => $attrGroup) {
177-
foreach ($attrGroup->attrs as $attributeKey => $attr) {
178-
if ($this->isName($attr->name, 'Override')) {
179-
unset($attrGroup->attrs[$attributeKey]);
180-
}
181-
}
182-
183-
if ($attrGroup->attrs === []) {
184-
unset($executeClassMethod->attrGroups[$attrGroupKey]);
185-
}
186-
}
175+
$this->removeOverrideAttributeAsDifferentMethod($executeClassMethod);
187176

188177
if ($configureClassMethod instanceof ClassMethod) {
189178
// 7. replace input->getArgument() and input->getOption() calls with direct variable access
@@ -266,4 +255,20 @@ private function isFluentArgumentOptionChain(MethodCall $methodCall): bool
266255
// the left-most var must be $this
267256
return $current instanceof Variable && $this->isName($current, 'this');
268257
}
258+
259+
private function removeOverrideAttributeAsDifferentMethod(ClassMethod $executeClassMethod): void
260+
{
261+
foreach ($executeClassMethod->attrGroups as $attrGroupKey => $attrGroup) {
262+
foreach ($attrGroup->attrs as $attributeKey => $attr) {
263+
if ($this->isName($attr->name, 'Override')) {
264+
unset($attrGroup->attrs[$attributeKey]);
265+
}
266+
}
267+
268+
// is attribute empty? remove whole group
269+
if ($attrGroup->attrs === []) {
270+
unset($executeClassMethod->attrGroups[$attrGroupKey]);
271+
}
272+
}
273+
}
269274
}

0 commit comments

Comments
 (0)