Skip to content

Commit 5c557f6

Browse files
committed
bug #62562 [Console] don't discard existing aliases when constructing Command (henderkes)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [Console] don't discard existing aliases when constructing Command | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | yes | New feature? no | Deprecations? | no | Issues | Fix #62557 | License | MIT <!-- 🛠️ Replace this text with a concise explanation of your change: - What it does and why it's needed - A simple example of how it works (include PHP, YAML, etc.) - If it modifies existing behavior, include a before/after comparison The code in #62557 was broken by 5e7cd96. Here I combine the new aliases found by pipes in the string with existing ones that may have previously been set. --> Contributor guidelines: - [x] Add tests and ensure they pass - [x] Bug fixes must target the **lowest maintained** branch where they apply https://symfony.com/releases#maintained-symfony-branches - [x] New features and deprecations must target the **feature** branch and must add an entry to the changelog file of the patched component: https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - [x] Do not break backward compatibility: https://symfony.com/bc Commits ------- 917d8d0c129 [Console] don't discard existing aliases when constructing Command
2 parents 245d678 + ac543cf commit 5c557f6

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Command/Command.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ public function __construct(?string $name = null, ?callable $code = null)
120120
$name = array_shift($aliases);
121121
}
122122

123+
// we must not overwrite existing aliases, combine new ones with existing ones
124+
$aliases = array_unique([
125+
...$this->aliases,
126+
...$aliases,
127+
]);
128+
123129
$this->setAliases($aliases);
124130
}
125131

Tests/Command/CommandTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,25 @@ public function testGetProcessedHelp()
199199
public function testGetSetAliases()
200200
{
201201
$command = new \TestCommand();
202-
$this->assertEquals(['name'], $command->getAliases(), '->getAliases() returns the aliases');
203202
$ret = $command->setAliases(['name1']);
204203
$this->assertEquals($command, $ret, '->setAliases() implements a fluent interface');
205204
$this->assertEquals(['name1'], $command->getAliases(), '->setAliases() sets the aliases');
206205
}
207206

207+
public function testAliasesSetBeforeParentConstructorArePreserved()
208+
{
209+
$command = new class extends Command {
210+
public function __construct()
211+
{
212+
// set aliases before calling parent constructor
213+
$this->setAliases(['existingalias']);
214+
parent::__construct('foo|newalias');
215+
}
216+
};
217+
218+
$this->assertSame(['existingalias', 'newalias'], $command->getAliases(), 'Aliases set before parent::__construct() must be preserved.');
219+
}
220+
208221
#[TestWith(['name|alias1|alias2', 'name', ['alias1', 'alias2'], false])]
209222
#[TestWith(['|alias1|alias2', 'alias1', ['alias2'], true])]
210223
public function testSetAliasesAndHiddenViaName(string $name, string $expectedName, array $expectedAliases, bool $expectedHidden)

0 commit comments

Comments
 (0)