Skip to content

Commit 47ff4cc

Browse files
committed
Refactor
1 parent 5cd829f commit 47ff4cc

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,35 @@ public static function getDefaultDescription(): ?string
8383
}
8484

8585
/**
86-
* @param string|null $name The name of the command; passing null means it must be set in configure()
86+
* @param string|callable|null $name The name of the command or an invokable object that uses AsCommand attribute; passing null means it must be set in configure()
8787
*
8888
* @throws LogicException When the command name is empty
8989
*/
90-
public function __construct(?string $name = null)
90+
public function __construct(null|callable|string $name = null)
9191
{
9292
$this->definition = new InputDefinition();
9393

94+
if (is_callable($name)) {
95+
if (!\is_object($name) || $name instanceof \Closure) {
96+
throw new InvalidArgumentException(\sprintf('The command must be an instance of "%s" or an invokable object.', Command::class));
97+
}
98+
99+
/** @var AsCommand $attribute */
100+
$attribute = ((new \ReflectionObject($name))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance()
101+
?? throw new LogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class));
102+
103+
$this->setName($attribute->name)
104+
->setDescription($attribute->description ?? '')
105+
->setHelp($attribute->help ?? '')
106+
->setCode($name);
107+
108+
foreach ($attribute->usages as $usage) {
109+
$this->addUsage($usage);
110+
}
111+
112+
return;
113+
}
114+
94115
$attribute = ((new \ReflectionClass(static::class))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
95116

96117
if (null === $name) {

src/Symfony/Component/Console/Tester/CommandTester.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,7 @@ class CommandTester
3232
public function __construct(
3333
callable|Command $command,
3434
) {
35-
if (!$command instanceof Command) {
36-
if (!\is_object($command) || $command instanceof \Closure) {
37-
throw new InvalidArgumentException(\sprintf('The command must be an instance of "%s" or an invokable object.', Command::class));
38-
}
39-
40-
/** @var AsCommand $attribute */
41-
$attribute = ((new \ReflectionObject($command))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance()
42-
?? throw new LogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class));
43-
44-
$command = (new Command($attribute->name))
45-
->setDescription($attribute->description ?? '')
46-
->setHelp($attribute->help ?? '')
47-
->setCode($command);
48-
49-
foreach ($attribute->usages as $usage) {
50-
$command->addUsage($usage);
51-
}
52-
}
53-
54-
$this->command = $command;
35+
$this->command = is_callable($command) ? new Command($command) : $command;
5536
}
5637

5738
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy