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

Config: display user-friendly message when invalid generator is passed #771

Merged

Conversation

rodrigoprimo
Copy link
Contributor

Description

This PR improves how Config::processLongArgument() handles the --generator parameter to display a user-friendly message if an invalid generator name is passed. Before, an invalid generator name caused a fatal error.

I have checked, and it is not possible to add custom generators using third-party code (at least not without adding files to the PHPCS directory). This is because the Runner class only loads generators found in the src/Generator/ directory:

$class = 'PHP_CodeSniffer\Generators\\'.$this->config->generator;
$generator = new $class($ruleset);

So, it should be fine to limit the list of valid generators added in this PR to only the three generators provided by PHPCS.

Suggested changelog entry

Config: display a user-friendly message if an invalid generator name is passed in the command line

Related issues/external references

Fixes #709

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.

Sorry, something went wrong.

@rodrigoprimo
Copy link
Contributor Author

@jrfnl, while working on this PR, I noticed that, besides what I reported in #709, it is possible to trigger a different PHP fatal error if Generator is passed as the name of the generator:

$ phpcs test.php --generator=Generator --standard=Generic
PHP Fatal error:  Uncaught Error: Cannot instantiate abstract class PHP_CodeSniffer\Generators\Generator in src/Runner.php:98
Stack trace:
#0 bin/phpcs(14): PHP_CodeSniffer\Runner->runPHPCS()
#1 {main}
  thrown in src/Runner.php on line 98

This happens because PHPCS tries to instantiate anything that it finds in the src/Generator directory:

$class = 'PHP_CodeSniffer\Generators\\'.$this->config->generator;
$generator = new $class($ruleset);

The change suggested in this PR prevents this fatal error from happening, at least when using PHPCS in the command line. But I'm mentioning it anyway in case we want to do something else about it.

@rodrigoprimo
Copy link
Contributor Author

Looking into the unrelated test that failed in the CI build.

@jrfnl
Copy link
Member

jrfnl commented Dec 13, 2024

Looking into the unrelated test that failed in the CI build.

Eh.. the test failure is not unrelated...

@rodrigoprimo
Copy link
Contributor Author

@jrfnl, I dropped the commit that changed testDeprecatedSniffsListDoesNotShowNeedsCsMode() and added a new commit to ensure that the code handles the --generator argument in a case-insensitive way. This PR is ready for another review.

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

@rodrigoprimo Thanks for updating this. I have left some comments inline, mostly nitpicks/dotting of the i's and crossing the t's.

Other than that, this is looking good to go into the 3.12.0 release.

@jrfnl jrfnl added this to the 3.12.0 milestone Dec 19, 2024
@rodrigoprimo rodrigoprimo force-pushed the generator-param-error-handling branch from a6ddc1e to 541e2ef Compare December 20, 2024 00:11
@rodrigoprimo
Copy link
Contributor Author

Thanks for your additional comments, @jrfnl. I pushed a new commit addressing all of them. Please take another look when you get a chance.

rodrigoprimo and others added 4 commits February 15, 2025 23:49

Verified

This commit was signed with the committer’s verified signature.
jrfnl Juliette
This commit improves how `Config::processLongArgument()` handles the
`--generator` parameter. Now it will show a user-friendly message if an
invalid generator name is passed. Before, an invalid generator name
caused a fatal error.

Verified

This commit was signed with the committer’s verified signature.
jrfnl Juliette

Verified

This commit was signed with the committer’s verified signature.
jrfnl Juliette
Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com>

Verified

This commit was signed with the committer’s verified signature.
jrfnl Juliette
to a class constant in the future
Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

@rodrigoprimo Thank you for making those updates and for working on this. All looks good to me!

I'm going to rebase the PR before merging it as the last push feel in the time slot when Coveralls were having problems, so code coverage is not recorded (and I'd like to double-check it's okay).
I'll merge once the build has passed again.

@jrfnl jrfnl force-pushed the generator-param-error-handling branch from 502270e to b958235 Compare February 15, 2025 23:07
@jrfnl jrfnl merged commit e53a925 into PHPCSStandards:master Feb 15, 2025
58 checks passed
@jrfnl jrfnl deleted the generator-param-error-handling branch February 15, 2025 23:45
jrfnl pushed a commit that referenced this pull request Feb 15, 2025

Verified

This commit was signed with the committer’s verified signature.
jrfnl Juliette
#771)

This commit improves how `Config::processLongArgument()` handles the
`--generator` parameter. Now it will show a user-friendly message if an
invalid generator name is passed. Before, an invalid generator name
caused a fatal error.

The commit also ensures that the value for the `generator` CLI parameter is handled case-insensitively.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PHP fatal error when user passes an invalid generator
2 participants