Skip to content

[Symfony61] Skip CommandConfigureToAttributeRector on non-constant configure() values - #1057

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-command-configure-non-constant
Aug 25, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
fix-command-configure-non-constant

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes the regression reported in #1054.

CommandConfigureToAttributeRector inlined the argument of setName()/setDescription()/setAliases()/setHidden() straight into #[AsCommand], without checking it is a compile-time constant. A runtime value produced invalid PHP:

// before
class ExampleCommand extends Command
{
    public function __construct(private readonly string $description) {}

    public function configure(): void
    {
        $this->setDescription($this->description);
    }
}

// after — invalid, attribute args must be constant
#[AsCommand(description: $this->description)]
class ExampleCommand extends Command
{
    public function __construct(private readonly string $description) {}
}

Now the rule only extracts scalar / const-fetch / class-const / constant-array values and leaves any non-constant setX() call untouched in configure():

 class ExampleCommand extends Command
 {
     public function __construct(private readonly string $description) {}

     public function configure(): void
     {
         $this->setDescription($this->description);
     }
 }

Also returns null when nothing could be extracted, so the rule no longer reports a no-op change.

Credit to @Jean85 for the report and reproduction fixture in #1054.

…nfigure() values

Attribute arguments must be constant expressions, so a runtime value like
$this->setDescription($this->description) cannot be inlined into #[AsCommand].
Leave such setX() calls in configure() instead of producing invalid code.

Also return null when nothing could be extracted, so the rule no longer
reports a no-op change.

Reported in #1054.

Co-authored-by: Alessandro Lai <alessandro.lai85@gmail.com>
@TomasVotruba
TomasVotruba force-pushed the fix-command-configure-non-constant branch from 894f90a to ac8f297 Compare August 25, 2026 15:45
@TomasVotruba
TomasVotruba merged commit dc8927a into main Aug 25, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the fix-command-configure-non-constant branch August 25, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant