8000 Deprecating command getDefault* methods · symfony/symfony@0aae29c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0aae29c

Browse files
committed
Deprecating command getDefault* methods
1 parent 8c941de commit 0aae29c

File tree

6 files changed

+83
-16
lines changed

6 files changed

+83
-16
lines changed

UPGRADE-7.3.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Console
3030
});
3131
```
3232

33+
* Deprecate methods `Command::getDefaultName()` and `Command::getDefaultDescription()` in favor of the `#[AsCommand]` attribute
34+
3335
FrameworkBundle
3436
---------------
3537

src/Symfony/Component/Console/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Add support for invokable commands and add `#[Argument]` and `#[Option]` attributes to define input arguments and options
88
* Deprecate not declaring the parameter type in callable commands defined through `setCode` method
99
* Add support for help definition via `AsCommand` attribute
10+
* Deprecate methods `Command::getDefaultName()` and `Command::getDefaultDescription()` in favor of the `#[AsCommand]` attribute
1011

1112
7.2
1213
---

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

+34-4
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,27 @@ class Command
5454
private array $usages = [];
5555
private ?HelperSet $helperSet = null;
5656

57+
/**
58+
* @deprecated since Symfony 7.3, use the #[AsCommand] attribute instead
59+
*/
5760
public static function getDefaultName(): ?string
5861
{
62+
trigger_deprecation('symfony/console', '7.3', 'Method "%s()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', __METHOD__);
63+
5964
if ($attribute = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class)) {
6065
return $attribute[0]->newInstance()->name;
6166
}
6267

6368
return null;
6469
}
6570

71+
/**
72+
* @deprecated since Symfony 7.3, use the #[AsCommand] attribute instead
73+
*/
6674
public static function getDefaultDescription(): ?string
6775
{
76+
trigger_deprecation('symfony/console', '7.3', 'Method "%s()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', __METHOD__);
77+
6878
if ($attribute = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class)) {
6979
return $attribute[0]->newInstance()->description;
7080
}
@@ -81,7 +91,19 @@ public function __construct(?string $name = null)
8191
{
8292
$this->definition = new InputDefinition();
8393

84-
if (null === $name && null !== $name = static::getDefaultName()) {
94+
$attribute = ((new \ReflectionClass(static::class))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
95+
96+
if (null === $name) {
97+
if (self::class !== (new \ReflectionMethod($this, 'getDefaultName'))->getDeclaringClass()->getName()) {
98+
trigger_deprecation('symfony/console', '7.3', 'Method "%s::getDefaultName()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', static::class);
99+
100+
$defaultName = static::getDefaultName();
101+
} else {
102+
$defaultName = $attribute?->name;
103+
}
104+
}
105+
106+
if (null === $name && null !== $name = $defaultName) {
85107
$aliases = explode('|', $name);
86108

87109
if ('' === $name = array_shift($aliases)) {
@@ -97,11 +119,19 @@ public function __construct(?string $name = null)
97119
}
98120

99121
if ('' === $this->description) {
100-
$this->setDescription(static::getDefaultDescription() ?? '');
122+
if (self::class !== (new \ReflectionMethod($this, 'getDefaultDescription'))->getDeclaringClass()->getName()) {
123+
trigger_deprecation('symfony/console', '7.3', 'Method "%s::getDefaultDescription()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', static::class);
124+
125+
$defaultDescription = static::getDefaultDescription();
126+
} else {
127+
$defaultDescription = $attribute?->description;
128+
}
129+
130+
$this->setDescription($defaultDescription ?? '');
101131
}
102132

103-
if ('' === $this->help && $attributes = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class)) {
104-
$this->setHelp($attributes[0]->newInstance()->help ?? '');
133+
if ('' === $this->help) {
134+
$this->setHelp($attribute?->help ?? '');
105135
}
106136

107137
if (\is_callable($this)) {

src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php

+23-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console\DependencyInjection;
1313

14+
use Symfony\Component\Console\Attribute\AsCommand;
1415
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Command\LazyCommand;
1617
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
@@ -57,7 +58,18 @@ public function process(ContainerBuilder $container): void
5758
$invokableRef = null;
5859
}
5960

60-
$aliases = $tags[0]['command'] ?? str_replace('%', '%%', $class::getDefaultName() ?? '');
61+
/** @var AsCommand|null $attribute */
62+
$attribute = ($r->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
63+
64+
if (Command::class !== (new \ReflectionMethod($class, 'getDefaultName'))->getDeclaringClass()->getName()) {
65+
trigger_deprecation('symfony/console', '7.3', 'Method "%s::getDefaultName()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', $class);
66+
67+
$defaultName = $class::getDefaultName();
68+
} else {
69+
$defaultName = $attribute?->name;
70+
}
71+
72+
$aliases = str_replace('%', '%%', $tags[0]['command'] ?? $defaultName ?? '');
6173
$aliases = explode('|', $aliases);
6274
$commandName = array_shift($aliases);
6375

@@ -111,10 +123,18 @@ public function process(ContainerBuilder $container): void
111123
$definition->addMethodCall('setHelp', [str_replace('%', '%%', $help)]);
112124
}
113125

114-
$description ??= str_replace('%', '%%', $class::getDefaultDescription() ?? '');
126+
if (!$description) {
127+
if (Command::class !== (new \ReflectionMethod($class, 'getDefaultDescription'))->getDeclaringClass()->getName()) {
128+
trigger_deprecation('symfony/console', '7.3', 'Method "%s::getDefaultDescription()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', $class);
129+
130+
$description = $class::getDefaultDescription();
131+
} else {
132+
$description = $attribute?->description;
133+
}
134+
}
115135

116136
if ($description) {
117-
$definition->addMethodCall('setDescription', [$description]);
137+
$definition->addMethodCall('setDescription', [str_replace('%', '%%', $description)]);
118138

119139
$container->register('.'.$id.'.lazy', LazyCommand::class)
120140
->setArguments([$commandName, $aliases, $description, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]);

src/Symfony/Component/Console/Tests/ApplicationTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2404,7 +2404,9 @@ private function createSignalableApplication(Command $command, ?EventDispatcherI
24042404
if ($dispatcher) {
24052405
$application->setDispatcher($dispatcher);
24062406
}
2407-
$application->add(new LazyCommand($command::getDefaultName(), [], '', false, fn () => $command, true));
2407+
/** @var AsCommand $attribute */
2408+
$attribute = (new \ReflectionClass($command))->getAttributes(AsCommand::class)[0]->newInstance();
2409+
$application->add(new LazyCommand($attribute->name, [], '', false, fn () => $command, true));
24082410

24092411
return $application;
24102412
}

src/Symfony/Component/Console/Tests/Command/CommandTest.php

+20-8
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,6 @@ public function testSetCodeWithStaticAnonymousFunction()
427427

428428
public function testCommandAttribute()
429429
{
430-
$this->assertSame('|foo|f', Php8Command::getDefaultName());
431-
$this->assertSame('desc', Php8Command::getDefaultDescription());
432-
433430
$command = new Php8Command();
434431

435432
$this->assertSame('foo', $command->getName());
@@ -439,26 +436,41 @@ public function testCommandAttribute()
439436
$this->assertSame(['f'], $command->getAliases());
440437
}
441438

442-
public function testAttributeOverridesProperty()
439+
/**
440+
* @group legacy
441+
*/
442+
public function testCommandAttributeWithDeprecatedMethods()
443443
{
444-
$this->assertSame('my:command', MyAnnotatedCommand::getDefaultName());
445-
$this->assertSame('This is a command I wrote all by myself', MyAnnotatedCommand::getDefaultDescription());
444+
$this->assertSame('|foo|f', Php8Command::getDefaultName());
445+
$this->assertSame('desc', Php8Command::getDefaultDescription());
446+
}
446447

448+
public function testAttributeOverridesProperty()
449+
{
447450
$command = new MyAnnotatedCommand();
448451

449452
$this->assertSame('my:command', $command->getName());
450453
$this->assertSame('This is a command I wrote all by myself', $command->getDescription() E5FC );
451454
}
452455

456+
/**
457+
* @group legacy
458+
*/
459+
public function testAttributeOverridesPropertyWithDeprecatedMethods()
460+
{
461+
$this->assertSame('my:command', MyAnnotatedCommand::getDefaultName());
462+
$this->assertSame('This is a command I wrote all by myself', MyAnnotatedCommand::getDefaultDescription());
463+
}
464+
453465
public function testDefaultCommand()
454466
{
455467
$apl = new Application();
456-
$apl->setDefaultCommand(Php8Command::getDefaultName());
468+
$apl->setDefaultCommand('foo');
457469
$property = new \ReflectionProperty($apl, 'defaultCommand');
458470

459471
$this->assertEquals('foo', $property->getValue($apl));
460472

461-
$apl->setDefaultCommand(Php8Command2::getDefaultName());
473+
$apl->setDefaultCommand('foo2');
462474
$property = new \ReflectionProperty($apl, 'defaultCommand');
463475

464476
$this->assertEquals('foo2', $property->getValue($apl));

0 commit comments

Comments
 (0)
0