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

Command line option to execute a single rule only #8899

Open
cweiske opened this issue Nov 15, 2024 · 1 comment · May be fixed by rectorphp/rector-src#6441
Open

Command line option to execute a single rule only #8899

cweiske opened this issue Nov 15, 2024 · 1 comment · May be fixed by rectorphp/rector-src#6441
Labels

Comments

@cweiske
Copy link

cweiske commented Nov 15, 2024

Feature Request

It should be possible to provide a command line option that causes rector to apply a single rule only.
Configuration file adjustments should not be necessary.

This is a feature needed for #8898 (automatically splitting up rector fixes into different commits).

History

rector once had such a parameter:

Problems

#7366 (comment):

Hi, we actually had this feature few years ago. It was breaking very often as the rules discovery by short class name requires full autoload of all classes. Also you have to make sure there are no 2 short classes.
Due to that design, it caused more confusion than value, so we removed it.

#3342 (comment):

Since the summer switch from YAML to PHP configs, the set optoins was also redisigned to be used purely in PHP. The set names from CLI were mismatched to their PHP names. The --set option CLI has been removed as well, so the config would be the single place to use

#3342 (comment)

the --only="Namespace\Class" must be quoted right - slashes? single? double? + OS differences

Maybe we can extend the api to include this backslash, slash, single, double... and reduce complexity.

#4699 (comment)

After this year's switch from YAML to PHP configs, and dropping --set CLI option to make use of the PHP configs, this feature is contradictory towards PHP autocomplete filosophy

Naming

--only (--rule was renamed to --only in #2655).

Accepted values

The option should accept a single value only: A fully qualified class name (not a short/relative one, see #7366).

./vendor/bin/rector --rule="Rector\Php73\Rector\BooleanOr\IsCountableRector"

Things to consider:

  • Backslashes must be escaped differently in different shells and operating systems. Either document that or warn the user if the option value has no backslash in it.
  • Leading backslash required or not? I'd say not. Rules always must be fully namespaced; a missing leading backslash shall get added automatically.

Affected commands

  • process
  • list-rules

❗ After reading the comments in the previous issues and patches, I have no idea what requirements regarding naming, functionality and stability you have for this feature.

@TomasVotruba
Copy link
Member

Thanks for great summary. I think now I'm open to accept this feature, under 2 requirements:

  • CLI options is tested in both Linux and Windows; the problem with class name escaping was big pain to drop this feature
  • there is a feedback about such a incorrectly passed escaped class, so user knows how to fix the class name

If the feature is user-friendly, let's go 👍

cweiske added a commit to mogic-le/rector-src that referenced this issue 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
cweiske added a commit to mogic-le/rector-src that referenced this issue 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
cweiske added a commit to mogic-le/rector-src that referenced this issue Nov 19, 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
cweiske added a commit to mogic-le/rector-src that referenced this issue Nov 19, 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
cweiske added a commit to mogic-le/rector-src that referenced this issue Nov 25, 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
cweiske added a commit to mogic-le/rector-src that referenced this issue Nov 25, 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
cweiske added a commit to mogic-le/rector-src that referenced this issue Nov 25, 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
cweiske added a commit to mogic-le/rector-src that referenced this issue Nov 26, 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
cweiske added a commit to mogic-le/rector-src that referenced this issue Nov 28, 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants