Repository navigation
[Symfony61] Do not add #[AsCommand] without a name in CommandConfigureToAttributeRector - #1102
Conversation
| if (! $node instanceof MethodCall || ! $this->isName($node->name, $methodName)) { | ||
| return null; | ||
| } | ||
|
|
There was a problem hiding this comment.
early check :
if ($node->isFirstClassCallable()) {
return null;
}here ...
|
We hit the same bug running the composer-based Symfony set (
The other 11 commands in core set their name with Commit with the change: https://git.drupalcode.org/issue/drupal-3638148/-/commit/0283aebd5ce13193397e2c300eb07786caf91187 I ported the check from this PR onto the rule bundled in Rector 2.8.4 and re-ran it on core: |
) It moves setDescription()/setAliases()/setHidden() into #[AsCommand(...)] without making sure the attribute gets a name. Commands that name themselves through the constructor, like core's ServerCommand, end up with #[AsCommand(description: '...')] and the console fatals because AsCommand's $name is required. Remove once rectorphp/rector-symfony#1102 is released. https://www.drupal.org/i/3638148
|
I'll look into it... |
…eToAttributeRector #[AsCommand] requires `name`. When configure() only sets a description, aliases or hidden, and the name is not on the attribute nor set by a constant setName() (it is passed to the parent constructor, for example), the rule created #[AsCommand(description: '...')], which throws ArgumentCountError when the container or the console boots. Leave such classes untouched. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
7adfaaf to
e87a48d
Compare
|
Thank you 👍 |
|
I'll make new release today 🤞 |
CommandConfigureToAttributeRectormovessetName(),setDescription(),setAliases()andsetHidden()out ofconfigure()into#[AsCommand(...)]. It never checks that anameends up in the attribute.AsCommand::__construct()takes a requiredstring $nameas its first argument. When a command gets its name from somewhere else, for example the constructor:the rule currently produces
#[AsCommand(description: 'Some description')]. Compiling the container (or booting the console) then fails with:We hit this running the Symfony 6.0-7.2 sets on a Symfony 7.4 app: two commands got a nameless attribute and every
bin/consolecall crashed.Change
Before extracting anything, require that a name will be there: either already on an existing
#[AsCommand](name:) or set by a constantsetName()inconfigure(). If there is something to move into the attribute but no name, the class is left untouched (same approach as the existing skip for non-constant values).configure()bodies with nothing to move, likeempty_configure_body, still behave as before.Tests
New fixtures:
skip_name_passed_to_parent_constructor: the case above, left unchanged. Fails without the change.skip_non_constant_name:setName($this->commandName)next to a constant description, left unchanged. Fails without the change.description_with_name_on_existing_attribute:#[AsCommand(name: ...)]plus a description inconfigure()still converts (regression guard).The rule's tests pass (17), plus ECS and PHPStan.
🤖 Generated with Claude Code