Skip to content

Commit 0ed04d7

Browse files
committed
Nicolas' Review
1 parent 57f32df commit 0ed04d7

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

src/Symfony/Component/Console/Attribute/Input.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@
88
#[\Attribute(\Attribute::TARGET_PARAMETER)]
99
class Input
1010
{
11-
private \ReflectionClass $class;
1211
/**
1312
* @var array<string, Argument>
1413
*/
15-
private array $arguments = [];
14+
public array $arguments = [];
15+
1616
/**
1717
* @var array<string, Option>
1818
*/
19-
private array $options = [];
19+
public array $options = [];
20+
21+
private \ReflectionClass $class;
2022

2123
public static function tryFrom(\ReflectionParameter $parameter): ?self
2224
{
23-
/** @var self $self */
25+
/** @var static $self */
2426
if (!$self = ($parameter->getAttributes(self::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null)?->newInstance()) {
2527
return null;
2628
}
@@ -38,14 +40,18 @@ public static function tryFrom(\ReflectionParameter $parameter): ?self
3840
$self->class = new \ReflectionClass($class);
3941

4042
foreach ($self->class->getProperties() as $property) {
43+
if (!$property->isPublic() || $property->isStatic()) {
44+
continue;
45+
}
46+
4147
if ($argument = Argument::tryFrom($property)) {
4248
$self->arguments[$property->name] = $argument;
4349
} elseif ($option = Option::tryFrom($property)) {
4450
$self->options[$property->name] = $option;
4551
}
4652
}
4753

48-
if ([] === $self->arguments && [] === $self->options) {
54+
if (!$self->arguments && !$self->options) {
4955
throw new LogicException(\sprintf('The input class "%s" must have at least one argument or option.', $self->class->name));
5056
}
5157

@@ -60,29 +66,13 @@ public function resolveValue(InputInterface $input): mixed
6066
$instance = $this->class->newInstanceWithoutConstructor();
6167

6268
foreach ($this->arguments as $name => $argument) {
63-
$this->class->getProperty($name)->setValue($instance, $argument->resolveValue($input));
69+
$instance->$name = $argument->resolveValue($input);
6470
}
6571

6672
foreach ($this->options as $name => $option) {
67-
$this->class->getProperty($name)->setValue($instance, $option->resolveValue($input));
73+
$instance->$name = $option->resolveValue($input);
6874
}
6975

7076
return $instance;
7177
}
72-
73-
/**
74-
* @return Argument[]
75-
*/
76-
public function getArguments(): array
77-
{
78-
return $this->arguments;
79-
}
80-
81-
/**
82-
* @return Option[]
83-
*/
84-
public function getOptions(): array
85-
{
86-
return $this->options;
87-
}
8878
}

src/Symfony/Component/Console/Command/InvokableCommand.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,16 @@ public function configure(InputDefinition $definition): void
7777
foreach ($this->reflection->getParameters() as $parameter) {
7878
if ($argument = Argument::tryFrom($parameter)) {
7979
$definition->addArgument($argument->toInputArgument());
80-
8180
continue;
8281
}
8382

8483
if ($option = Option::tryFrom($parameter)) {
8584
$definition->addOption($option->toInputOption());
86-
8785
continue;
8886
}
8987

9088
if ($input = Input::tryFrom($parameter)) {
91-
$inputArguments = array_map(fn (Argument $a) => $a->toInputArgument(), $input->getArguments());
89+
$inputArguments = array_map(fn (Argument $a) => $a->toInputArgument(), $input->arguments);
9290

9391
// make sure optional arguments are defined after required ones
9492
usort($inputArguments, fn (InputArgument $a, InputArgument $b) => (int) $b->isRequired() - (int) $a->isRequired());
@@ -97,7 +95,7 @@ public function configure(InputDefinition $definition): void
9795
$definition->addArgument($inputArgument);
9896
}
9997

100-
foreach ($input->getOptions() as $option) {
98+
foreach ($input->options as $option) {
10199
$definition->addOption($option->toInputOption());
102100
}
103101
}

0 commit comments

Comments
 (0)