Skip to content

Commit

Permalink
Fix: add support for the Name attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
radmen committed Nov 23, 2024
1 parent 7622735 commit dfdfeab
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Attributes/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Attribute;

#[Attribute(Attribute::TARGET_PARAMETER)]
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
final class Argument
{
public function __construct()
Expand Down
6 changes: 3 additions & 3 deletions src/Attributes/Description.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use Attribute;

#[Attribute(Attribute::TARGET_PARAMETER)]
final class Description
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
final readonly class Description
{
public function __construct(
public readonly string $description,
public string $description,
) {
//
}
Expand Down
6 changes: 3 additions & 3 deletions src/Attributes/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use Attribute;

#[Attribute(Attribute::TARGET_PARAMETER)]
final class Name
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
final readonly class Name
{
public function __construct(public readonly string $name)
public function __construct(public string $name)
{
//
}
Expand Down
2 changes: 1 addition & 1 deletion src/Attributes/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Attribute;

#[Attribute(Attribute::TARGET_PARAMETER)]
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
final class Option
{
public function __construct()
Expand Down
6 changes: 3 additions & 3 deletions src/Attributes/Shortcut.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use Attribute;

#[Attribute(Attribute::TARGET_PARAMETER)]
final class Shortcut
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
final readonly class Shortcut
{
public function __construct(public readonly string $shortcut)
public function __construct(public string $shortcut)
{
//
}
Expand Down
12 changes: 9 additions & 3 deletions src/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Baethon\Symfony\Console\Input;

use Baethon\Symfony\Console\Input\Attributes\Argument;
use Baethon\Symfony\Console\Input\Attributes\Name;
use Baethon\Symfony\Console\Input\Attributes\Option;
use ReflectionClass;
use ReflectionProperty;
Expand Down Expand Up @@ -43,12 +44,17 @@ function (ReflectionProperty $item) {
private function getPropertyData(ReflectionProperty $item, InputInterface $input): array
{
$option = $item->getAttributes(Option::class)[0] ?? null;
$name = $item->getName();
$propertyName = $item->getName();
$name = $item->getAttributes(Name::class)[0] ?? null;

$name = $name
? $name->newInstance()->name
: $propertyName;

if ($option) {
return [$name, $input->getOption($name)];
return [$propertyName, $input->getOption($name)];
}

return [$name, $input->getArgument($name)];
return [$propertyName, $input->getArgument($name)];
}
}
24 changes: 24 additions & 0 deletions tests/Unit/ProxyFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Baethon\Symfony\Console\Input\Attributes\Argument;
use Baethon\Symfony\Console\Input\Attributes\Name;
use Baethon\Symfony\Console\Input\ProxyFactory;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -22,3 +24,25 @@
expect($ghost->age)->toEqual(25);
expect($ghost->name)->toEqual('Jon');
});

it('supports name attribute', function () {
$factory = new ProxyFactory;
$input = new ArrayInput([
'foo' => 'Jon',
], new InputDefinition([
new InputArgument('foo', mode: InputArgument::REQUIRED),
]));

$dto = new class
{
public function __construct(
#[Argument]
#[Name('foo')]
public ?string $name = null
) {}
};

$ghost = $factory->create($dto::class, $input);

expect($ghost->name)->toEqual('Jon');
});

0 comments on commit dfdfeab

Please sign in to comment.