Skip to content

Commit abf9433

Browse files
committed
Deprecating Command static methods
1 parent 8c941de commit abf9433

File tree

6 files changed

+65
-16
lines changed

6 files changed

+65
-16
lines changed

UPGRADE-7.3.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Console
3030
});
3131
```
3232

33+
* Static methods `Command::getDefaultName()` and `Command::getDefaultDescription()` are deprecated.
34+
Extract the command name and description through class reflection instead
35+
3336
FrameworkBundle
3437
---------------
3538

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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 static methods `Command::getDefaultName()` and `Command::getDefaultDescription()`
1011

1112
7.2
1213
---

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

Lines changed: 31 additions & 4 deletions
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
59+
*/
5760
public static function getDefaultName(): ?string
5861
{
62+
trigger_deprecation('symfony/console', '7.3', 'The static method "%s()" is deprecated and will be removed in Symfony 8.0, extract the command name from the "%s" attribute instead.', __METHOD__, AsCommand::class);
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
73+
*/
6674
public static function getDefaultDescription(): ?string
6775
{
76+
trigger_deprecation('symfony/console', '7.3', 'The static method "%s()" is deprecated and will be removed in Symfony 8.0, extract the command description from the "%s" attribute instead.', __METHOD__, AsCommand::class);
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 ((new \ReflectionMethod($this, 'getDefaultName'))->getDeclaringClass()->getName() !== self::class) {
96+
trigger_deprecation('symfony/console', '7.3', 'The static method "%s::getDefaultName()" is deprecated, define the command name in the "%s" attribute instead.', static::class, AsCommand::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 ((new \ReflectionMethod($this, 'getDefaultDescription'))->getDeclaringClass()->getName() !== self::class) {
119+
trigger_deprecation('symfony/console', '7.3', 'The static method "%s::getDefaultDescription()" is deprecated, define the command description in the "%s" attribute instead.', static::class, AsCommand::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

Lines changed: 7 additions & 3 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 20 additions & 8 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy