Skip to content

Commit 7d7625e

Browse files
committed
misc
1 parent eaa09ba commit 7d7625e

3 files changed

Lines changed: 68 additions & 22 deletions

File tree

rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/argument_with_default_value.php.inc renamed to rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/DefaultValue/argument_with_default_value.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;
3+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture\DefaultValue;
44

55
use Symfony\Component\Console\Attribute\AsCommand;
66
use Symfony\Component\Console\Command\Command;
@@ -28,7 +28,7 @@ class ArgumentWithDefaultValue extends Command
2828
-----
2929
<?php
3030

31-
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;
31+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture\DefaultValue;
3232

3333
use Symfony\Component\Console\Attribute\AsCommand;
3434
use Symfony\Component\Console\Command\Command;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Output\OutputInterface;
10+
11+
#[AsCommand(
12+
name: 'app:hello',
13+
)]
14+
class IterableTypeWithDefaultValue extends Command
15+
{
16+
protected function configure(): void
17+
{
18+
$this->addArgument('iterable', InputArgument::REQUIRED | InputArgument::IS_ARRAY, null, ['many values']);
19+
}
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\Output\OutputInterface;
37+
38+
#[AsCommand(
39+
name: 'app:hello',
40+
)]
41+
class IterableTypeWithDefaultValue
42+
{
43+
public function __invoke(
44+
#[\Symfony\Component\Console\Attribute\Argument(name: 'iterable')]
45+
array $iterable = ['many values']
46+
): int
47+
{
48+
}
49+
}
50+
51+
?>

rules/Symfony73/NodeFactory/CommandInvokeParamsFactory.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private function createArgumentParams(array $commandArguments): array
6363

6464
$argumentArgs = [new Arg(value: $commandArgument->getName(), name: new Identifier('name'))];
6565

66-
if ($this->hasUsefulDescription($commandArgument)) {
66+
if ($this->isNonEmptyExpr($commandArgument->getDescription())) {
6767
$argumentArgs[] = new Arg(value: $commandArgument->getDescription(), name: new Identifier(
6868
'description'
6969
));
@@ -97,19 +97,15 @@ private function createOptionParams(array $commandOptions): array
9797

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

100-
if ($commandOption->getShortcut() instanceof Expr && ! $this->valueResolver->isNull(
101-
$commandOption->getShortcut()
102-
)) {
100+
if ($this->isNonEmptyExpr($commandOption->getShortcut())) {
103101
$optionArgs[] = new Arg(value: $commandOption->getShortcut(), name: new Identifier('shortcut'));
104102
}
105103

106-
if ($commandOption->getMode() instanceof Expr && ! $this->valueResolver->isNull(
107-
$commandOption->getMode()
108-
)) {
104+
if ($this->isNonEmptyExpr($commandOption->getMode())) {
109105
$optionArgs[] = new Arg(value: $commandOption->getMode(), name: new Identifier('mode'));
110106
}
111107

112-
if ($this->hasUsefulDescription($commandOption)) {
108+
if ($this->isNonEmptyExpr($commandOption->getDescription())) {
113109
$optionArgs[] = new Arg(value: $commandOption->getDescription(), name: new Identifier('description'));
114110
}
115111

@@ -135,26 +131,25 @@ private function createCamelCase(string $value): string
135131
return lcfirst($value);
136132
}
137133

138-
private function hasUsefulDescription(CommandArgument|CommandOption $commandArgumentOrOption): bool
134+
private function isOptionalArgument(CommandArgument $commandArgument): bool
135+
{
136+
if (! $commandArgument->getMode() instanceof Expr) {
137+
return true;
138+
}
139+
140+
return $this->valueResolver->isValue($commandArgument->getMode(), 2);
141+
}
142+
143+
private function isNonEmptyExpr(?Expr $expr): bool
139144
{
140-
if (! $commandArgumentOrOption->getDescription() instanceof Expr) {
145+
if (! $expr instanceof Expr) {
141146
return false;
142147
}
143148

144-
$expr = $commandArgumentOrOption->getDescription();
145149
if ($this->valueResolver->isNull($expr)) {
146150
return false;
147151
}
148152

149153
return ! $this->valueResolver->isValue($expr, '');
150154
}
151-
152-
private function isOptionalArgument(CommandArgument $commandArgument): bool
153-
{
154-
if (! $commandArgument->getMode() instanceof Expr) {
155-
return true;
156-
}
157-
158-
return $this->valueResolver->isValue($commandArgument->getMode(), 2);
159-
}
160155
}

0 commit comments

Comments
 (0)