Skip to content

feat: console application - add completion support#8887

Merged
keradus merged 13 commits intoPHP-CS-Fixer:masterfrom
gharlan:console-completion
Jul 29, 2025
Merged

feat: console application - add completion support#8887
keradus merged 13 commits intoPHP-CS-Fixer:masterfrom
gharlan:console-completion

Conversation

@gharlan
Copy link
Copy Markdown
Member

@gharlan gharlan commented Jul 24, 2025

@coveralls
Copy link
Copy Markdown

coveralls commented Jul 24, 2025

Coverage Status

coverage: 94.753% (-0.002%) from 94.755%
when pulling 5a7f8d5 on gharlan:console-completion
into 5bd26f0 on PHP-CS-Fixer:master.

$this->setDefinition(
[
new InputArgument('name', InputArgument::REQUIRED, 'Name of rule / set.'),
new InputArgument('name', InputArgument::REQUIRED, 'Name of rule / set.', null, fn () => array_merge($this->getSetNames(), array_keys($this->getFixers()))),
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using symfony/console < 6.1 you will only get completion for command names and option names, not for the argument/option values, because the $suggestedValues param for InputArgument/InputOption didn't exist before.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So especially when using php-cs-fixer via phar file, because the phar file uses symfony/console 5.4.

Also note that 5.4 supports bash only (fish came with 6.1, and zsh with 6.2).

Comment thread src/Console/Command/FixCommand.php Outdated
Comment thread src/Console/Command/ListSetsCommand.php Outdated
Copy link
Copy Markdown
Member

@keradus keradus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so far good, one question will be placed

protected function getDefaultCommands(): array
{
return [new HelpCommand(), new ListCommand()];
return [new HelpCommand(), new ListCommand(), new CompleteCommand(), new DumpCompletionCommand()];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does one need to run php-cs-fixer completion -h manually?
if list of commands/params/values change, do they need to re-run it after Fixer upgrade?

if so, we should document to end-user to do it

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does one need to run php-cs-fixer completion -h manually?

Everyone who wants to use the completion has to follow the instructions in php-cs-fixer completion -h once.
The paths in the instructions differ depending on your shell (bash, zsh, fish).

if list of commands/params/values change, do they need to re-run it after Fixer upgrade?

No, the installed completion script is dynamic. It uses the CompleteCommand (hidden php-cs-fixer _complete command). So the completions are always "live".

The installed completion script works for all php-cs-fixer executables in all projects (or all php-cs-fixer.phar executables, when installed via php-cs-fixer.phar completion).

The completion scripts are the same for all symfony/console applications: https://github.com/symfony/console/tree/7.3/Resources

if so, we should document to end-user to do it

I'v added a one-liner to the docs now, similar to the one from the composer docs: https://getcomposer.org/doc/03-cli.md#command-line-completion

Comment thread box.json.dist
"*.php",
"*.php8"
"*.php8",
"completion.*"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are those files?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vendor/symfony/console/Resources/: https://github.com/symfony/console/tree/7.3/Resources

Comment thread doc/usage.rst

.. code-block:: console

php php-cs-fixer.phar completion --help
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to detect it's not installed and suggest to install it ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if/how it is possible

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not really possible to detect

@keradus
Copy link
Copy Markdown
Member

keradus commented Jul 29, 2025

nice one, thanks

@keradus keradus enabled auto-merge (squash) July 29, 2025 22:26
@keradus keradus merged commit 47953b2 into PHP-CS-Fixer:master Jul 29, 2025
32 checks passed
@gharlan gharlan deleted the console-completion branch July 29, 2025 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants