Skip to content

[4.x] Read the #[AsCommand] attribute in AbstractCommand - #38

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-as-command-attribute
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-as-command-attribute

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary of Changes

AbstractCommand does not extend Symfony's Command, so Symfony's #[AsCommand] attribute was ignored: a command written the way the Symfony 7 documentation describes (the static $defaultName was removed from Symfony 7) had no name, and Application::addCommand() rejected it with "The command class … does not have a name". The docs called this out as a trap.

AbstractCommand now reads the attribute:

  • getDefaultName() returns $defaultName when the class declares it (unchanged), otherwise the name from the class' #[AsCommand], without aliases or the hidden marker, so callers that build command maps from getDefaultName() still get a plain name;
  • the constructor applies the attribute's aliases and hidden flag when the name came from the attribute, and its description and help when they are set (help exists on the attribute since Symfony 7.4 and is read only when present) — before configure(), so values set there still win; a name passed to the constructor takes precedence as before;
  • as with $defaultName, the attribute is not inherited by subclasses;
  • the reflection result (whether the class declares $defaultName, and its attribute) is cached per class instead of creating a ReflectionProperty on every getDefaultName() call.

$defaultName keeps working unchanged. Deprecating it in favour of the attribute is a separate follow-up.

Testing Instructions

New tests in tests/Command/AbstractCommandTest.php cover name, aliases, description and help from the attribute, a hidden command, precedence of $defaultName, configure() and an explicit name, and non-inheritance (with the new tests/Fixtures/Command/AttributeCommand.php); ApplicationTest::testAddCommandNamedByAttribute registers an attribute-only command. On 4.x-dev the attribute tests fail (getDefaultName() returns null). The help test is skipped with Symfony < 7.4 (CI's lowest-dependencies job installs 7.0).

Documentation Changes Required

  • docs/overview.md: the "Use $defaultName, not #[AsCommand]" warning is replaced by a description of both ways to name a command, with an example and the precedence rules.
  • docs/v3-to-v4-update.md: the note that the attribute is not read now says this applies to 4.0 and that 4.1 reads it.

🤖 Generated with Claude Code

@Hackwar
Hackwar force-pushed the 4.x-as-command-attribute branch from 5a47e9d to ea0b105 Compare November 13, 2026 00:08
AbstractCommand does not extend Symfony's Command, so a command written
the way the Symfony 7 documentation describes, with #[AsCommand] instead
of the static $defaultName removed from Symfony, had no name and could
not be registered.

getDefaultName() now falls back to the attribute when the class does
not declare $defaultName, and the constructor applies the attribute's
aliases, hidden flag, description and help. $defaultName keeps
precedence. The reflection result is cached per class instead of being
rebuilt on every getDefaultName() call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Hackwar

Hackwar commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Approved

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant