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

Skip to content

[Console] Deprecating Command getDefaultName and getDefaultDescription methods #59565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UPGRADE-7.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Console
});
```

* Deprecate methods `Command::getDefaultName()` and `Command::getDefaultDescription()` in favor of the `#[AsCommand]` attribute

FrameworkBundle
---------------

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add support for invokable commands and add `#[Argument]` and `#[Option]` attributes to define input arguments and options
* Deprecate not declaring the parameter type in callable commands defined through `setCode` method
* Add support for help definition via `AsCommand` attribute
* Deprecate methods `Command::getDefaultName()` and `Command::getDefaultDescription()` in favor of the `#[AsCommand]` attribute

7.2
---
Expand Down
38 changes: 34 additions & 4 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,27 @@ class Command
private array $usages = [];
private ?HelperSet $helperSet = null;

/**
* @deprecated since Symfony 7.3, use the #[AsCommand] attribute instead
*/
public static function getDefaultName(): ?string
{
trigger_deprecation('symfony/console', '7.3', 'Method "%s()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', __METHOD__);

if ($attribute = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class)) {
return $attribute[0]->newInstance()->name;
}

return null;
}

/**
* @deprecated since Symfony 7.3, use the #[AsCommand] attribute instead
*/
public static function getDefaultDescription(): ?string
{
trigger_deprecation('symfony/console', '7.3', 'Method "%s()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', __METHOD__);

if ($attribute = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class)) {
return $attribute[0]->newInstance()->description;
}
Expand All @@ -81,7 +91,19 @@ public function __construct(?string $name = null)
{
$this->definition = new InputDefinition();

if (null === $name && null !== $name = static::getDefaultName()) {
$attribute = ((new \ReflectionClass(static::class))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();

if (null === $name) {
if (self::class !== (new \ReflectionMethod($this, 'getDefaultName'))->class) {
trigger_deprecation('symfony/console', '7.3', 'Overriding "Command::getDefaultName()" in "%s" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', static::class);

$defaultName = static::getDefaultName();
} else {
$defaultName = $attribute?->name;
}
}

if (null === $name && null !== $name = $defaultName) {
$aliases = explode('|', $name);

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

if ('' === $this->description) {
$this->setDescription(static::getDefaultDescription() ?? '');
if (self::class !== (new \ReflectionMethod($this, 'getDefaultDescription'))->class) {
trigger_deprecation('symfony/console', '7.3', 'Overriding "Command::getDefaultDescription()" in "%s" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', static::class);

$defaultDescription = static::getDefaultDescription();
} else {
$defaultDescription = $attribute?->description;
}

$this->setDescription($defaultDescription ?? '');
}

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

if (\is_callable($this)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Console\DependencyInjection;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LazyCommand;
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
Expand Down Expand Up @@ -57,7 +58,18 @@ public function process(ContainerBuilder $container): void
$invokableRef = null;
}

$aliases = $tags[0]['command'] ?? str_replace('%', '%%', $class::getDefaultName() ?? '');
/** @var AsCommand|null $attribute */
$attribute = ($r->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();

if (Command::class !== (new \ReflectionMethod($class, 'getDefaultName'))->class) {
trigger_deprecation('symfony/console', '7.3', 'Overriding "Command::getDefaultName()" in "%s" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', $class);

$defaultName = $class::getDefaultName();
} else {
$defaultName = $attribute?->name;
}

$aliases = str_replace('%', '%%', $tags[0]['command'] ?? $defaultName ?? '');
$aliases = explode('|', $aliases);
$commandName = array_shift($aliases);

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

$description ??= str_replace('%', '%%', $class::getDefaultDescription() ?? '');
if (!$description) {
if (Command::class !== (new \ReflectionMethod($class, 'getDefaultDescription'))->class) {
trigger_deprecation('symfony/console', '7.3', 'Overriding "Command::getDefaultDescription()" in "%s" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', $class);

$description = $class::getDefaultDescription();
} else {
$description = $attribute?->description;
}
}

if ($description) {
$definition->addMethodCall('setDescription', [$description]);
$definition->addMethodCall('setDescription', [str_replace('%', '%%', $description)]);

$container->register('.'.$id.'.lazy', LazyCommand::class)
->setArguments([$commandName, $aliases, $description, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2404,7 +2404,7 @@ private function createSignalableApplication(Command $command, ?EventDispatcherI
if ($dispatcher) {
$application->setDispatcher($dispatcher);
}
$application->add(new LazyCommand($command::getDefaultName(), [], '', false, fn () => $command, true));
$application->add(new LazyCommand($command->getName(), [], '', false, fn () => $command, true));

return $application;
}
Expand Down
61 changes: 53 additions & 8 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -28,6 +29,8 @@

class CommandTest extends TestCase
{
use ExpectDeprecationTrait;

protected static string $fixturesPath;

public static function setUpBeforeClass(): void
Expand Down Expand Up @@ -427,9 +430,6 @@ public function testSetCodeWithStaticAnonymousFunction()

public function testCommandAttribute()
{
$this->assertSame('|foo|f', Php8Command::getDefaultName());
$this->assertSame('desc', Php8Command::getDefaultDescription());

$command = new Php8Command();

$this->assertSame('foo', $command->getName());
Expand All @@ -439,30 +439,62 @@ public function testCommandAttribute()
$this->assertSame(['f'], $command->getAliases());
}

public function testAttributeOverridesProperty()
/**
* @group legacy
*/
public function testCommandAttributeWithDeprecatedMethods()
{
$this->assertSame('my:command', MyAnnotatedCommand::getDefaultName());
$this->assertSame('This is a command I wrote all by myself', MyAnnotatedCommand::getDefaultDescription());
$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.');
$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.');

$this->assertSame('|foo|f', Php8Command::getDefaultName());
$this->assertSame('desc', Php8Command::getDefaultDescription());
}

public function testAttributeOverridesProperty()
{
$command = new MyAnnotatedCommand();

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

/**
* @group legacy
*/
public function testAttributeOverridesPropertyWithDeprecatedMethods()
{
$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.');
$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.');

$this->assertSame('my:command', MyAnnotatedCommand::getDefaultName());
$this->assertSame('This is a command I wrote all by myself', MyAnnotatedCommand::getDefaultDescription());
}

public function testDefaultCommand()
{
$apl = new Application();
$apl->setDefaultCommand(Php8Command::getDefaultName());
$apl->setDefaultCommand('foo');
$property = new \ReflectionProperty($apl, 'defaultCommand');

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

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

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

/**
* @group legacy
*/
public function testDeprecatedMethods()
{
$this->expectDeprecation('Since symfony/console 7.3: Overriding "Command::getDefaultName()" in "Symfony\Component\Console\Tests\Command\FooCommand" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.');
$this->expectDeprecation('Since symfony/console 7.3: Overriding "Command::getDefaultDescription()" in "Symfony\Component\Console\Tests\Command\FooCommand" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.');

new FooCommand();
}
}

// In order to get an unbound closure, we should create it outside a class
Expand Down Expand Up @@ -491,3 +523,16 @@ class MyAnnotatedCommand extends Command

protected static $defaultDescription = 'This description should be ignored.';
}

class FooCommand extends Command
{
public static function getDefaultName(): ?string
{
return 'foo';
}

public static function getDefaultDescription(): ?string
{
return 'foo description';
}
}
Loading