Skip to content

Don't stop scan on invalid inline property annotation #99

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

Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Nov 11, 2023

Description

Recreation of upstream PR squizlabs/PHP_CodeSniffer#3916:

Follow up on squizlabs/PHP_CodeSniffer#3629, which was merged for PHPCS 3.8.0.

PR squizlabs/PHP_CodeSniffer#3629 added logic to throw a "Ruleset invalid. Property "$propertyName" does not exist on sniff ..." error.

This error is intended for the command-line when reading the phpcs.xml.dist ruleset file.

However, this error could also be encountered if an inline // phpcs:set ... annotation would try to set a non-existent property.

While the use of // phpcs:set is typically reserved for sniff test case files, there is nothing stopping end-users from using the annotation.

The net-effect would be:

  • The Ruleset::setSniffProperty() throws a RuntimeException.
  • This exception is then passed to File::addMessage() where it is not thrown as the line on which the error is being thrown is an annotation line.
  • The scan of the file stops dead in its tracks as a RuntimeException was encountered.
  • The end-user doesn't know the file does not finish scanning as no Internal error is shown for the file.

To me, this is counter-intuitive and counter-productive as it may give people a false sense of security (CI is green, while in reality files are not being scanned).

To fix this, I propose the following:

  • Collect all // phpcs:set related inline annotations encountered while scanning.
  • Do not stop the file scan for these errors.
  • Add a warning with information about the incorrect annotations on line 1 once the file has finished scanning.

Includes a test via the Generic.PHP.BacktickOperator sniff.

Suggested changelog entry

I'd suggest updating the existing changelog entry for the change from PR squizlabs/PHP_CodeSniffer#3629 and adding:

    -- Invalid sniff properties set for sniffs via inline annotations will result in an informative `Internal.PropertyDoesNotExist` errror on line 1, but will not halt the execution of PHPCS

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Follow up on 3629, which was merged for PHPCS 3.8.0.

PR 3629 added logic to throw a "Ruleset invalid. Property \"$propertyName\" does not exist on sniff ..." error.

This error is intended for the command-line when reading the `phpcs.xml.dist` ruleset file.

However, this error could _also_ be encountered if an inline `// phpcs:set ...` annotation would try to set a non-existent property.

While the use of `// phpcs:set` is typically reserved for sniff test case files, there is nothing stopping end-users from using the annotation.

The net-effect would be:
* The `Ruleset::setSniffProperty()` throws a `RuntimeException`.
* This exception is then passed to `File::addMessage()` where it is **not** thrown as the line on which the error is being thrown is an annotation line.
* The scan of the file stops dead in its tracks as a `RuntimeException` was encountered.
* The end-user doesn't know the file does not finish scanning as no `Internal` error is shown for the file.

To me, this is counter-intuitive and counter-productive as it may give people a false sense of security (CI is green, while in reality files are not being scanned).

To fix this, I propose the following:
* Collect all `// phpcs:set` related inline annotations encountered while scanning.
* Do **not** stop the file scan for these errors.
* Add a warning with information about the incorrect annotations on line 1 once the file has finished scanning.

Includes a test via the `Generic.PHP.BacktickOperator` sniff.
@jrfnl jrfnl force-pushed the feature/3629-dont-stop-scan-on-invalid-property-set-annotation branch from 92938bd to 540c3bc Compare December 4, 2023 13:03
@jrfnl jrfnl merged commit c1cf656 into master Dec 4, 2023
@jrfnl jrfnl deleted the feature/3629-dont-stop-scan-on-invalid-property-set-annotation branch December 4, 2023 13:07
@jrfnl jrfnl mentioned this pull request Mar 17, 2025
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.

1 participant