Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "--only" option to process only a single rule #6441

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cweiske
Copy link

@cweiske cweiske commented Nov 15, 2024

The option for the "process" and "list-rules" commands applies the single given rule only, without needing to modify the configuration file.

The option value must be a fully classified class name:

  --only="Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector"

A hint is given when the user forgot to escape the backslashes.


It is impossible to modify the injected "$rectors" after the command line configuration is parsed, so I had to introduce the ConfigurationRuleFilter singleton.

Since both ListRulesCommand and ProcessCommand make use of the ConfigurationRuleFilter - but list-rules does not have a Configuration - I had to make the filterOnlyRule() method public to prevent code duplication.

Resolves rectorphp/rector#8899


Test it:

./bin/rector process --config=e2e/applied-rule-return-array-nodes/rector.php --dry-run --only="Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector"

./bin/rector list-rules --config=e2e/applied-rule-return-array-nodes/rector.php --only="Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector"

@TomasVotruba
Copy link
Member

TomasVotruba commented Nov 15, 2024

There should be kind of e2e tests that passes and that fails if data are incorrect

@cweiske
Copy link
Author

cweiske commented Nov 19, 2024

I've added an end-to-end test that fails when e.g. the ConfigurationRuleFilter simply returns all rectors instead of filtering them.

I had to extend the e2eTestRunner to support additional command line options.

@samsonasik
Copy link
Member

Register new e2e path to

@cweiske
Copy link
Author

cweiske commented Nov 19, 2024

e2e-test is registered.

The option for the "process" and "list-rules" commands applies
the single given rule only, without needing to modify
the configuration file.

The option value must be a fully classified class name:

  --only="Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector"

A hint is given when the user forgot to escape the backslashes.

----

It is impossible to modify the injected "$rectors" after the
command line configuration is parsed, so I had to introduce the
ConfigurationRuleFilter singleton.

Since both ListRulesCommand and ProcessCommand make use of the
ConfigurationRuleFilter - but list-rules does not have a Configuration -
I had to make the filterOnlyRule() method public to prevent
code duplication.

Resolves rectorphp/rector#8899
@cweiske
Copy link
Author

cweiske commented Nov 25, 2024

Code is adjusted.
I also tested it in the windows cmd.exe, and it works:
2024-11-25 rector rule windows

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.

Command line option to execute a single rule only
3 participants