From 03d612a5332b97a2511f2de52050755e173814e3 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sat, 7 Jun 2025 17:59:31 +0200 Subject: [PATCH] [Console] Fix setting aliases & hidden via name --- src/Symfony/Component/Console/CHANGELOG.md | 5 +++++ src/Symfony/Component/Console/Command/Command.php | 6 +++--- .../Component/Console/Tests/Command/CommandTest.php | 13 +++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index 9f3ae3d7d2326..509a9d03c5707 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.4 +--- + + * Allow setting aliases and the hidden flag via the command name passed to the constructor + 7.3 --- diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php index f6cd8499791f1..23e3b662138c8 100644 --- a/src/Symfony/Component/Console/Command/Command.php +++ b/src/Symfony/Component/Console/Command/Command.php @@ -97,13 +97,13 @@ public function __construct(?string $name = null) 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(); + $name = static::getDefaultName(); } else { - $defaultName = $attribute?->name; + $name = $attribute?->name; } } - if (null === $name && null !== $name = $defaultName) { + if (null !== $name) { $aliases = explode('|', $name); if ('' === $name = array_shift($aliases)) { diff --git a/src/Symfony/Component/Console/Tests/Command/CommandTest.php b/src/Symfony/Component/Console/Tests/Command/CommandTest.php index 0db3572fc3476..85442c7b9243e 100644 --- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php @@ -205,6 +205,19 @@ public function testGetSetAliases() $this->assertEquals(['name1'], $command->getAliases(), '->setAliases() sets the aliases'); } + /** + * @testWith ["name|alias1|alias2", "name", ["alias1", "alias2"], false] + * ["|alias1|alias2", "alias1", ["alias2"], true] + */ + public function testSetAliasesAndHiddenViaName(string $name, string $expectedName, array $expectedAliases, bool $expectedHidden) + { + $command = new Command($name); + + self::assertSame($expectedName, $command->getName()); + self::assertSame($expectedHidden, $command->isHidden()); + self::assertSame($expectedAliases, $command->getAliases()); + } + public function testGetSynopsis() { $command = new \TestCommand();