8000 Console application issue when command loader name key is not part of command object · Issue #38015 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Console application issue when command loader name key is not part of command object #38015
Closed
@Xerkus

Description

@Xerkus

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

if ($this->has($name)) {
return $this->get($name);
}

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()

public function has(string $name)
{
$this->init();
return isset($this->commands[$name]) || ($this->commandLoader && $this->commandLoader->has($name) && $this->add($this->commandLoader->get($name)));

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0