Skip to content

Squiz/ValidClassName: bug fix - improve comment handling #817

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
merged 1 commit into from
Feb 13, 2025

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Feb 13, 2025

Description

Noticed while working on something else.

If there would be a comment between the OO keyword and the declared name, the sniff could throw false positives with unhelpful error messages, like:

 193 | ERROR | Class name "/*comment*/" is not in PascalCase format (Squiz.Classes.ValidClassName.NotCamelCaps)
 194 | ERROR | Trait name "//comment" is not in PascalCase format (Squiz.Classes.ValidClassName.NotCamelCaps)
 196 | ERROR | Interface name "// phpcs:ignore Stnd.Cat.SniffName -- just testing" is not in PascalCase format
     |       | (Squiz.Classes.ValidClassName.NotCamelCaps)
 199 | ERROR | Class name "CommentsShouldBeIgnoredValid/*comment*/" is not in PascalCase format (Squiz.Classes.ValidClassName.NotCamelCaps)
 200 | ERROR | Interface name "annotations_should_be_ignored_InvalidName" is not in PascalCase format (Squiz.Classes.ValidClassName.NotCamelCaps)

Fixed now by:

  1. Ignoring any comments between the OO keyword and the name.
  2. Not including comments directly following a name in the name to be evaluated.

Includes tests.

Includes minor error message precision fix - the error will now be thrown on the name which is being flagged as invalid, not on the OO keyword.

Suggested changelog entry

Squiz.Classes.ValidClassName will now ignore comments when determining the name to be validated.

Types of changes

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

Sorry, something went wrong.

Verified

This commit was signed with the committer’s verified signature.
ctavan Christoph Tavan
Noticed while working on something else.

If there would be a comment between the OO keyword and the declared name, the sniff could throw false positives with unhelpful error messages, like:
```
193 | ERROR | Class name "/*comment*/" is not in PascalCase format (Squiz.Classes.ValidClassName.NotCamelCaps)
 194 | ERROR | Trait name "//comment" is not in PascalCase format (Squiz.Classes.ValidClassName.NotCamelCaps)
 196 | ERROR | Interface name "// phpcs:ignore Stnd.Cat.SniffName -- just testing" is not in PascalCase format
     |       | (Squiz.Classes.ValidClassName.NotCamelCaps)
 199 | ERROR | Class name "CommentsShouldBeIgnoredValid/*comment*/" is not in PascalCase format (Squiz.Classes.ValidClassName.NotCamelCaps)
 200 | ERROR | Interface name "annotations_should_be_ignored_InvalidName" is not in PascalCase format (Squiz.Classes.ValidClassName.NotCamelCaps)
```

Fixed now by:
1. Ignoring any comments between the OO keyword and the name.
2. Not including comments directly following a name in the name to be evaluated.

Includes tests.

Includes minor error message precision fix - the error will now be thrown on the name which is being flagged as invalid, not on the OO keyword.
@jrfnl jrfnl added this to the 3.12.0 milestone Feb 13, 2025
@jrfnl jrfnl requested a review from rodrigoprimo February 13, 2025 08:21
@jrfnl jrfnl merged commit 7acc4aa into master Feb 13, 2025
59 checks passed
@jrfnl jrfnl deleted the feature/squiz-validclassname-bug-fix-comment-handling branch February 13, 2025 22:08
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.

None yet

2 participants