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

Code before:
            return $attribute[0]->newInstance()->name;
        }

        return null;
    }

    /**
     * @deprecated since Symfony 7.3, use the #[AsCommand] attribute instead
     */
    public static function getDefaultDescription(): ?string
    {
        trigger_deprecation('symfony/console', '7.3', 'Method "%s()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', __METHOD__);

        if ($attribute = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class)) {
            return $attribute[0]->newInstance()->description;
        }

        return null;
    }

    /**
     * @param string|null $name The name of the command; passing null means it must be set in configure()
     *
     * @throws LogicException When the command name is empty
     */
    public function __construct(?string $name = null, ?callable $code = null)
    {
        $this->definition = new InputDefinition();

        if ($name === null && $code !== null) {
            if (!\is_object($code) || $code instanceof \Closure) {
                throw new InvalidArgumentException(\sprintf('The command must be an instance of "%s" or an invokable object.', Command::class));
            }

            /** @var AsCommand $attribute */
            $attribute = ((new \ReflectionObject($code))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance()
                ?? throw new LogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class));

            $this->setName($attribute->name)
                ->setDescription($attribute->description ?? '')
                ->setHelp($attribute->help ?? '')
                ->setCode(new InvokableCommand($this, $code));

            foreach ($attribute->usages as $usage) {
                $this->addUsage($usage);
            }

            return;
        }

        $attribute = ((new \ReflectionClass(static::class))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();

Review comment (suggestion):
As a new feature, we should call `$this->setCode($code)` line 162 if set.

Code after:
            return $attribute[0]->newInstance()->name;
        }

        return null;
    }

    /**
     * @deprecated since Symfony 7.3, use the #[AsCommand] attribute instead
     */
    public static function getDefaultDescription(): ?string
    {
        trigger_deprecation('symfony/console', '7.3', 'Method "%s()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', __METHOD__);

        if ($attribute = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class)) {
            return $attribute[0]->newInstance()->description;
        }

        return null;
    }

    /**
     * @param string|null $name The name of the command; passing null means it must be set in configure()
     *
     * @throws LogicException When the command name is empty
     */
    public function __construct(?string $name = null, ?callable $code = null)
    {
        $this->definition = new InputDefinition();

        if ($code !== null) {
            if (!\is_object($code) || $code instanceof \Closure) {
                throw new InvalidArgumentException(\sprintf('The command must be an instance of "%s" or an invokable object.', Command::class));
            }

            /** @var AsCommand $attribute */
            $attribute = ((new \ReflectionObject($code))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance()
                ?? throw new LogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class));

            $this->setName($name ?? $attribute->name)
                ->setDescription($attribute->description ?? '')
                ->setHelp($attribute->help ?? '')
                ->setCode($code);

            foreach ($attribute->usages as $usage) {
                $this->addUsage($usage);
            }

            return;
        }

        $attribute = ((new \ReflectionClass(static::class))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();