Skip to content

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)

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.

2 participants