Symfony Console: Migrate `configure` to `AsCommand`
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!
Hi Chris,
would this be helfpul to you? https://github.com/rectorphp/rector-symfony/blob/main/docs/rector_rules_overview.md#commandpropertytoattributerector
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.
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
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.
Looking into this... I think we can create something like AsAttributes set, as Symfony uses these often :)
The rule is now added https://github.com/rectorphp/rector-symfony/pull/619 :tada: