Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d91a0d

Browse files
committed
Deprecating command getDefault* methods
1 parent 8c941de commit 4d91a0d

File tree

6 files changed

+64
-16
lines changed

6 files changed

+64
-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

+31-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
}
@@ -80,8 +90,17 @@ public static function getDefaultDescription(): ?string
8090
public function __construct(?string $name = null)
8191
{
8292
$this->definition = new InputDefinition();
93+
$attribute = ((new \ReflectionClass(static::class))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
94+
95+
if (self::class !== (new \ReflectionMethod($this, 'getDefaultName'))->getDeclaringClass()->getName()) {
96+
trigger_deprecation('symfony/console', '7.3', 'Method "%s()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', static::class);
8397

84-
if (null === $name && null !== $name = static::getDefaultName()) {
98+
$defaultName = static::getDefaultName();
99+
} else {
100+
$defaultName = $attribute?->name;
101+
}
102+
103+
if (null === $name && null !== $name = $defaultName) {
85104
$aliases = explode('|', $name);
86105

87106
if ('' === $name = array_shift($aliases)) {
@@ -96,12 +115,20 @@ public function __construct(?string $name = null)
96115
$this->setName($name);
97116
}
98117

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

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

107134
if (\is_callable($this)) {

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

+7-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,10 @@ 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+
$aliases = str_replace('%', '%%', $tags[0]['command'] ?? $attribute?->name ?? '');
6165
$aliases = explode('|', $aliases);
6266
$commandName = array_shift($aliases);
6367

@@ -111,10 +115,10 @@ public function process(ContainerBuilder $container): void
111115
$definition->addMethodCall('setHelp', [str_replace('%', '%%', $help)]);
112116
}
113117

114-
$description ??= str_replace('%', '%%', $class::getDefaultDescription() ?? '');
118+
$description ??= $attribute?->description ?? '';
115119

116120
if ($description) {
117-
$definition->addMethodCall('setDescription', [$description]);
121+
$definition->addMethodCall('setDescription', [str_replace('%', '%%', $description)]);
118122

119123
$container->register('.'.$id.'.lazy', LazyCommand::class)
120124
->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());
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)