Skip to content

[symfony 7.3] InvokableCommandInputAttributeRector Option name with hyphen #9205

@antoniovj1

Description

@antoniovj1

Bug Report

Subject Details
Rector version 2.0.17

When applying InvokableCommandInputAttributeRector, using an option name that includes a hyphen results in the generation of an invalid variable name.

Minimal PHP Code Causing Issue

Original code:

<?php

declare(strict_types=1);

namespace App\Entrypoint\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'sync:test_rector', description: 'Hello PhpStorm')]
class TestRectorCommand extends Command
{
    protected function configure(): void
    {
        $this->addOption('option-with-hyphen');
    }
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        return Command::SUCCESS;
    }
}

After applying the rector rule, the generated code incorrectly includes a variable name with hyphens, which is not valid in PHP:

<?php

declare(strict_types=1);

namespace App\Entrypoint\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'sync:test_rector', description: 'Hello PhpStorm')]
class TestRectorCommand
{
    public function __invoke(#[\Symfony\Component\Console\Attribute\Command\Option]
    $option-with-hyphen, OutputInterface $output): int
    {
        return Command::SUCCESS;
    }
}

Expected Behaviour

<?php

declare(strict_types=1);

namespace App\Entrypoint\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'sync:test_rector', description: 'Hello PhpStorm')]
class TestRectorCommand
{
    public function __invoke(#[\Symfony\Component\Console\Attribute\Command\Option]
    $option_with_hyphen, OutputInterface $output): int
    {
        return Command::SUCCESS;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions