Skip to content

[Console] Deprecating Command getDefaultName and getDefaultDescription methods #59565

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

Merged
merged 1 commit into from
Jan 25, 2025

Conversation

yceruto
Copy link
Member

@yceruto yceruto commented Jan 20, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? no
Deprecations? yes
Issues -
License MIT

related discussion #59473 (comment)

@yceruto yceruto force-pushed the command_static_methods_depr branch from a951fd4 to 4d91a0d Compare January 21, 2025 14:25
@yceruto yceruto force-pushed the command_static_methods_depr branch 2 times, most recently from 0aae29c to 25d44d4 Compare January 21, 2025 20:56
@yceruto yceruto force-pushed the command_static_methods_depr branch from 25d44d4 to 99ba1a4 Compare January 22, 2025 13:24
@yceruto yceruto force-pushed the command_static_methods_depr branch from 99ba1a4 to 7e3d761 Compare January 22, 2025 15:26
@yceruto yceruto force-pushed the command_static_methods_depr branch from 7e3d761 to b529726 Compare January 23, 2025 02:13
@fabpot
Copy link
Member

fabpot commented Jan 25, 2025

Thank you @yceruto.

@fabpot fabpot merged commit b8f1db9 into symfony:7.3 Jan 25, 2025
5 of 11 checks passed
@yceruto yceruto deleted the command_static_methods_depr branch January 25, 2025 21:46
@fabpot fabpot mentioned this pull request May 2, 2025
nicolas-grekas added a commit that referenced this pull request Jun 6, 2025
This PR was merged into the 8.0 branch.

Discussion
----------

[Console] Remove deprecated code

| Q             | A
| ------------- | ---
| Branch?       | 8.0
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Issues        | -
| License       | MIT

Refs: #60076, #59565, #59474

Commits
-------

a13c74c Remove deprecated methods and legacy code
@ToshY
Copy link
Contributor

ToshY commented Aug 11, 2025

@yceruto @nicolas-grekas

I've encountered this deprecation warning today, but the message does clarify on how to resolve it.

What would be alternative to using Command::getDefaultName() is in the following use case?

    public function execute(InputInterface $input, OutputInterface $output): int
    {
        $commandName = MyCommand::getDefaultName(); // 'hello:world' as defined in the AsCommand attribute

        $commandInput = new ArrayInput([]);
        $command = $this->getApplication()->find($commandName);
        $code = $command->run($commandInput, $output);

        if ($code !== Command::SUCCESS) {
            return Command::FAILURE;
        }

        return Command::SUCCESS;
    }

Do I need to start extacting the command name myself starting from 8.0?

@chalasr
Copy link
Member

chalasr commented Aug 11, 2025

@ToshY Indeed, that use case wasn't considered when deprecating these methods. I'd suggest hardcoding command names in such tests -- just like route paths for controllers in functional tests -- and check for all occurrences in case of renaming.

If you really want to get it programmatically from the command class name, you can either get it from the AsCommand attribute via Reflection or put the name in a constant referenced in both attribute construction and tests i.e. #[AsCommand(name: SomeCommand::NAME)]

@ToshY
Copy link
Contributor

ToshY commented Aug 11, 2025

@ToshY Indeed, that use case wasn't considered when deprecating these methods. I'd suggest hardcoding command names in such tests -- just like route paths for controllers in functional tests -- and check for all occurrences in case of renaming.

FYI, the use case I sketched is not for a "test" (even though it could be). It is a use case on "how to call other commands". I've been using Command::getDefaultName() because I found using the hardcoded name when calling in several other commands a bit tedious, especially when someone decides to change the name and forgets to change it on other commands where it has been used/called.

If you really want to get it programmatically from the command class name, you can either get it from the AsCommand attribute via Reflection or put the name in a constant referenced in both attribute construction and tests i.e. #[AsCommand(name: SomeCommand::NAME)]

Yes, I understand I would need to do either the reflection bit myself or indeed could use a constant for it. I just don't understand why it got deprecated, as #59473 (comment) only talks about deprecating name/description for consistency reasons.

@derrabus
Copy link
Member

It got deprecated because we don't need it anymore. I understand that the method was convenient for you, but it was never intended to be used that way.

@ToshY
Copy link
Contributor

ToshY commented Aug 11, 2025

It got deprecated because we don't need it anymore. I understand that the method was convenient for you, but it was never intended to be used that way.

Ah okay, I didn't know that. Thanks both for the clarification 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants