Skip to content

Commit

Permalink
Squiz/ValidClassName: bug fix - improve comment handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Feb 13, 2025
1 parent 72949bb commit d9a3d1c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Standards/Squiz/Sniffs/Classes/ValidClassNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Common;
use PHP_CodeSniffer\Util\Tokens;

class ValidClassNameSniff implements Sniff
{
Expand Down Expand Up @@ -58,8 +59,8 @@ public function process(File $phpcsFile, $stackPtr)
// simply look for the first T_STRING because a class name
// starting with the number will be multiple tokens.
$opener = $tokens[$stackPtr]['scope_opener'];
$nameStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $opener, true);
$nameEnd = $phpcsFile->findNext([T_WHITESPACE, T_COLON], $nameStart, $opener);
$nameStart = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), $opener, true);
$nameEnd = $phpcsFile->findNext((Tokens::$emptyTokens + [T_COLON => T_COLON]), $nameStart, $opener);
if ($nameEnd === false) {
$name = $tokens[$nameStart]['content'];
} else {
Expand All @@ -75,7 +76,7 @@ public function process(File $phpcsFile, $stackPtr)
$type,
$name,
];
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
$phpcsFile->addError($error, $nameStart, 'NotCamelCaps', $data);
$phpcsFile->recordMetric($stackPtr, 'PascalCase class name', 'no');
} else {
$phpcsFile->recordMetric($stackPtr, 'PascalCase class name', 'yes');
Expand Down
10 changes: 10 additions & 0 deletions src/Standards/Squiz/Tests/Classes/ValidClassNameUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,13 @@ $foo = new class(
}
) extends DateTime {
};

class /*comment*/ CommentsShouldBeIgnoredValidName {}
trait //comment
commentsshouldbeignoredInvalidName {}
interface // phpcs:ignore Stnd.Cat.SniffName -- just testing
annotationshouldbeignored_InvalidName {}

class CommentsShouldBeIgnoredValid/*comment*/ {}
interface annotations_should_be_ignored_InvalidName// phpcs:ignore Stnd.Cat.SniffName -- just testing
{}
3 changes: 3 additions & 0 deletions src/Standards/Squiz/Tests/Classes/ValidClassNameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function getErrorList()
150 => 1,
151 => 1,
156 => 1,
195 => 1,
197 => 1,
200 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit d9a3d1c

Please sign in to comment.