Skip to content

Can't add commands that use AsCommand #62473

@afilina

Description

@afilina

Symfony version(s) affected

7.2.9

Description

Running a command using the standalone Console component throws an error:

PHP Fatal error: Uncaught TypeError: Symfony\Component\Console\Application::add(): Argument #1 ($command) must be of type Symfony\Component\Console\Command\Command, App\MyCommand given, called in /var/www/html/bin/console.php on line 12 and defined in /var/www/html/vendor/symfony/console/Application.php:53

add() expects a Command, but the example doesn't extend anything, since it uses the AsCommand attribute instead.

How to reproduce

  1. Install dependencies:
composer require symfony/console
  1. Create a basic command, per the documentation:
#[AsCommand(name: 'app:my-command')]
final readonly class MyCommand
{
    public function __invoke(): int
    {
        return Command::SUCCESS;
    }
}
  1. Create the console app, per the documentation:
#!/usr/bin/env php
<?php

require __DIR__.'/../vendor/autoload.php';

use App\MyCommand;
use Symfony\Component\Console\Application;

$application = new Application();

$application->add(new MyCommand());

$application->run();
  1. Run:
bin/application.php app:my-command

Possible Solution

No response

Additional Context

No response

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