rector-symfony icon indicating copy to clipboard operation
rector-symfony copied to clipboard

Symfony Console: Migrate `configure` to `AsCommand`

Open chrisdeeming opened this issue 1 year ago • 4 comments

We have a bunch of Symfony Conole commands, typically configured like this:

	protected function configure()
	{
		$this
			->setName('command:do-thing')
			->setAliases(['command:thing'])
			->setDescription('Does the thing.')
			->addArgument(
				'id',
				InputArgument::OPTIONAL,
				'An ID'
			);
	}

We would like to migrate these as a convention to use the AsCommand attribute, e.g.

#[AsCommand(
	name: 'command:do-thing',
	description: 'Does the thing.',
	aliases: ['command:thing']
)]

I understand CommandPropertyToAttributeRector works to convert properties to attributes (which I didn't realise was a thing to be honest!) but it doesn't touch this approach.

Are there plans to add that? Or is anyone aware of someone else achieving this previously?

Many thanks!

chrisdeeming avatar Apr 08 '24 22:04 chrisdeeming

Hi Chris,

would this be helfpul to you? https://github.com/rectorphp/rector-symfony/blob/main/docs/rector_rules_overview.md#commandpropertytoattributerector

TomasVotruba avatar Apr 09 '24 06:04 TomasVotruba

Thanks @TomasVotruba but no, unfortunately. That ruleset doesn't convert the setName, setDescription and setHidden methods to AsCommand, though I understand it does support setAliases.

I'm using this as a basis to build a ruleset myself. Will keep you updated.

chrisdeeming avatar Apr 09 '24 09:04 chrisdeeming

There is room for improvement, but the description can be updated with CommandDescriptionToPropertyRector The rule @TomasVotruba mentions will also update the description once it is a static property.

Support for setHidden and setName is still missing

stefantalen avatar Apr 09 '24 10:04 stefantalen

Right, I see, that's pretty cool. Good advice guys. I'm part way through writing my own rule to handle specifically all the relevant configure methods to AsCommand. I'll be more than happy to share that code once I'm done but agree perhaps all related rules could benefit from a closer look to ensure they're consistent.

chrisdeeming avatar Apr 09 '24 10:04 chrisdeeming

Looking into this... I think we can create something like AsAttributes set, as Symfony uses these often :)

TomasVotruba avatar Jun 22 '24 13:06 TomasVotruba

The rule is now added https://github.com/rectorphp/rector-symfony/pull/619 :tada:

TomasVotruba avatar Jun 22 '24 14:06 TomasVotruba