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
< 8000 div id="repo-content-pjax-container" class="repository-content " >

Console application issue when command loader name key is not part of command object #38015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Xerkus opened this issue Aug 31, 2020 · 0 comments

Comments

@Xerkus
Copy link
Xerkus commented Aug 31, 2020

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

@Xerkus Xerkus added the Bug label Aug 31, 2020
@symfony symfony locked and limited conversation to collaborators Sep 1, 2020
@symfony symfony unlocked this conversation Sep 1, 2020
@fabpot fabpot closed this as completed Sep 2, 2020
fabpot added a commit that referenced this issue Sep 2, 2020
…e definition (chalasr)

This PR was merged into the 3.4 branch.

Discussion
----------

[Console] Fix undefined index for inconsistent command name definition

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fixes #38015
| License       | MIT
| Doc PR        | -

The issue happens when the command name is set via construct/setName() and is routed via a command loader under a different name, which causes `Application::get(): Command` to return null (return type violation) with a notice. This makes it throws a proper CommandNotFoundException as expected.

Commits
-------

d59140e Fix undefined index for inconsistent command name definition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0