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

Skip to content

Commit 99ba1a4

Browse files
committed
Deprecating command getDefault* methods
1 parent 8c941de commit 99ba1a4

File tree

6 files changed

+90
-16
lines changed

6 files changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2404,7 +2404,7 @@ 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+
$application->add(new LazyCommand($command->getName(), [], '', false, fn () => $command, true));
24082408

24092409
return $application;
24102410
}

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

+29-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Console\Tests\Command;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Console\Application;
1617
use Symfony\Component\Console\Attribute\AsCommand;
1718
use Symfony\Component\Console\Command\Command;
@@ -28,6 +29,8 @@
2829

2930
class CommandTest extends TestCase
3031
{
32+
use ExpectDeprecationTrait;
33+
3134
protected static string $fixturesPath;
3235

3336
public static function setUpBeforeClass(): void
@@ -427,9 +430,6 @@ public function testSetCodeWithStaticAnonymousFunction()
427430

428431
public function testCommandAttribute()
429432
{
430-
$this->assertSame('|foo|f', Php8Command::getDefaultName());
431-
$this->assertSame('desc', Php8Command::getDefaultDescription());
432-
433433
$command = new Php8Command();
434434

435435
$this->assertSame('foo', $command->getName());
@@ -439,26 +439,47 @@ public function testCommandAttribute()
439439
$this->assertSame(['f'], $command->getAliases());
440440
}
441441

442-
public function testAttributeOverridesProperty()
442+
/**
443+
* @group legacy
444+
*/
445+
public function testCommandAttributeWithDeprecatedMethods()
443446
{
444-
$this->assertSame('my:command', MyAnnotatedCommand::getDefaultName());
445-
$this->assertSame('This is a command I wrote all by myself', MyAnnotatedCommand::getDefaultDescription());
447+
$this->expectDeprecation('Since symfony/console 7.3: Method "Symfony\Component\Console\Command\Command::getDefaultName()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.');
448+
$this->expectDeprecation('Since symfony/console 7.3: Method "Symfony\Component\Console\Command\Command::getDefaultDescription()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.');
446449

450+
$this->assertSame('|foo|f', Php8Command::getDefaultName());
451+
$this->assertSame('desc', Php8Command::getDefaultDescription());
452+
}
453+
454+
public function testAttributeOverridesProperty()
455+
{
447456
$command = new MyAnnotatedCommand();
448457

449458
$this->assertSame('my:command', $command->getName());
450459
$this->assertSame('This is a command I wrote all by myself', $command->getDescription());
451460
}
452461

462+
/**
463+
* @group legacy
464+
*/
465+
public function testAttributeOverridesPropertyWithDeprecatedMethods()
466+
{
467+
$this->expectDeprecation('Since symfony/console 7.3: Method "Symfony\Component\Console\Command\Command::getDefaultName()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.');
468+
$this->expectDeprecation('Since symfony/console 7.3: Method "Symfony\Component\Console\Command\Command::getDefaultDescription()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.');
469+
470+
$this->assertSame('my:command', MyAnnotatedCommand::getDefaultName());
471+
$this->assertSame('This is a command I wrote all by myself', MyAnnotatedCommand::getDefaultDescription());
472+
}
473+
453474
public function testDefaultCommand()
454475
{
455476
$apl = new Application();
456-
$apl->setDefaultCommand(Php8Command::getDefaultName());
477+
$apl->setDefaultCommand('foo');
457478
$property = new \ReflectionProperty($apl, 'defaultCommand');
458479

459480
$this->assertEquals('foo', $property->getValue($apl));
460481

461-
$apl->setDefaultCommand(Php8Command2::getDefaultName());
482+
$apl->setDefaultCommand('foo2');
462483
$property = new \ReflectionProperty($apl, 'defaultCommand');
463484

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

0 commit comments

Comments
 (0)