From c234b9dd9dee04868f41927cbaa2706547a6ee91 Mon Sep 17 00:00:00 2001 From: Yoann Renard Date: Fri, 4 Jun 2021 20:31:07 +0200 Subject: [PATCH 1/2] Handle 'hidden' param from AsCommand attribute --- src/Symfony/Component/Console/Attribute/AsCommand.php | 2 +- src/Symfony/Component/Console/Command/Command.php | 7 ++++++- .../Component/Console/Tests/Command/CommandTest.php | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Attribute/AsCommand.php b/src/Symfony/Component/Console/Attribute/AsCommand.php index b337f548f4663..70215c8a65709 100644 --- a/src/Symfony/Component/Console/Attribute/AsCommand.php +++ b/src/Symfony/Component/Console/Attribute/AsCommand.php @@ -21,7 +21,7 @@ public function __construct( public string $name, public ?string $description = null, array $aliases = [], - bool $hidden = false, + public bool $hidden = false, ) { if (!$hidden && !$aliases) { return; diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php index e35ae51ebfa28..ec06c6e720c6b 100644 --- a/src/Symfony/Component/Console/Command/Command.php +++ b/src/Symfony/Component/Console/Command/Command.php @@ -513,7 +513,12 @@ public function setHidden(bool $hidden /*= true*/) */ public function isHidden() { - return $this->hidden; + $isHidden = false; + if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($this))->getAttributes(AsCommand::class)) { + $isHidden = $attribute[0]->newInstance()->hidden; + } + + return $this->hidden || $isHidden; } /** diff --git a/src/Symfony/Component/Console/Tests/Command/CommandTest.php b/src/Symfony/Component/Console/Tests/Command/CommandTest.php index 68c0366210374..0af9aa1d6a3e0 100644 --- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php @@ -414,6 +414,8 @@ public function testCommandAttribute() { $this->assertSame('|foo|f', Php8Command::getDefaultName()); $this->assertSame('desc', Php8Command::getDefaultDescription()); + + $this->assertSame(true, (new Php8Command())->isHidden()); } } From d310b7907f113c81232ab0d2a5c9a77c803271a0 Mon Sep 17 00:00:00 2001 From: Yoann Renard Date: Fri, 4 Jun 2021 21:06:38 +0200 Subject: [PATCH 2/2] fixup! Handle 'hidden' param from AsCommand attribute --- src/Symfony/Component/Console/Tests/Command/CommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Tests/Command/CommandTest.php b/src/Symfony/Component/Console/Tests/Command/CommandTest.php index 0af9aa1d6a3e0..10d29314711e9 100644 --- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php @@ -415,7 +415,7 @@ public function testCommandAttribute() $this->assertSame('|foo|f', Php8Command::getDefaultName()); $this->assertSame('desc', Php8Command::getDefaultDescription()); - $this->assertSame(true, (new Php8Command())->isHidden()); + $this->assertTrue((new Php8Command())->isHidden()); } }