Description
Symfony version(s) affected: 5.1.4
Description
Console Application::get()
method could return null with undefined index notice when command loader is used but command name key does not match name or aliases from command object.
Application::find()
is affected too because of
symfony/src/Symfony/Component/Console/Application.php
Lines 645 to 647 in d6468a9
How to reproduce
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
$app = new Application();
$loader = new FactoryCommandLoader([
'test' => static fn() => new Command('test-command'),
]);
$app->setCommandLoader($loader);
var_export($app->has('test'));
var_export($app->get('test'));
Possible Solution
After command is obtained from command loader in Application::has()
, assert that command is actually present in $this->commands
.
I believe proper approach would be to raise an error when this occurs.
Ideally, application would transparently decorate command loader and decorator will then handle assertions that command object has matching name or alias:
CommandLoaderDecorator implements CommandLoaderInterface
{
...
public function get(string $name) : Command
{
$command = $this->commandLoader->get($name);
self::assertNamePresent($command, $name);
return $command;
}
}
Additional context
Command is loaded in has()
symfony/src/Symfony/Component/Console/Application.php
Lines 552 to 556 in d6468a9