Repository: symfony/symfony
File: src/Symfony/Component/Console/Application.php (PHP)

Code before:
        foreach ($commands as $command) {
            $this->addCommand($command);
        }
    }

    /**
     * @deprecated since Symfony 7.4, use Application::addCommand() instead
     */
    public function add(Command $command): ?Command
    {
        trigger_deprecation('symfony/console', '7.4', 'The "%s()" method is deprecated and will be removed in Symfony 8.0, use "%s::addCommand()" instead.', __METHOD__, self::class);

        return $this->addCommand($command);
    }

    /**
     * Adds a command object.
     *
     * If a command with the same name already exists, it will be overridden.
     * If the command is not enabled it will not be added.
     */
    public function addCommand(callable|Command $command): ?Command
    {
        $this->init();

        if (is_callable($command)) {
            $command = new Command($command);
        }

        $command->setApplication($this);

        if (!$command->isEnabled()) {
            $command->setApplication(null);

            return null;
        }

        if (!$command instanceof LazyCommand) {
            // Will throw if the command is not correctly initialized.
            $command->getDefinition();
        }

        if (!$command->getName()) {
            throw new LogicException(\sprintf('The command defined in "%s" cannot have an empty name.', get_debug_type($command)));
        }

        $this->commands[$command->getName()] = $command;

        foreach ($command->getAliases() as $alias) {
            $this->commands[$alias] = $command;
        }

Review comment (bug):
The command can both extend the `Command` class and implement the `__invoke` method. You need to keep the previous condition in order to fix the broken test.

```suggestion
        if (!$command instanceof Command) {
```

Code after:
        foreach ($commands as $command) {
            $this->addCommand($command);
        }
    }

    /**
     * @deprecated since Symfony 7.4, use Application::addCommand() instead
     */
    public function add(Command $command): ?Command
    {
        trigger_deprecation('symfony/console', '7.4', 'The "%s()" method is deprecated and will be removed in Symfony 8.0, use "%s::addCommand()" instead.', __METHOD__, self::class);

        return $this->addCommand($command);
    }

    /**
     * Adds a command object.
     *
     * If a command with the same name already exists, it will be overridden.
     * If the command is not enabled it will not be added.
     */
    public function addCommand(callable|Command $command): ?Command
    {
        $this->init();

        if (!$command instanceof Command) {
            $command = new Command(null, $command);
        }

        $command->setApplication($this);

        if (!$command->isEnabled()) {
            $command->setApplication(null);

            return null;
        }

        if (!$command instanceof LazyCommand) {
            // Will throw if the command is not correctly initialized.
            $command->getDefinition();
        }

        if (!$command->getName()) {
            throw new LogicException(\sprintf('The command defined in "%s" cannot have an empty name.', get_debug_type($command)));
        }

        $this->commands[$command->getName()] = $command;

        foreach ($command->getAliases() as $alias) {
            $this->commands[$alias] = $command;
        }