Skip to content

Commit 8f928f7

Browse files
committed
Squiz/ClassFileName: don't error when there is no class name
As things were, the sniff would try to find the next `T_STRING`, but didn't take live coding into account, which means it would end up comparing `$tokens[false]['content']` to the filename, which would end up comparing `<?php` to the file name. This changes the sniff to use the `File::getDeclarationName()` method instead and verifies we have a usable name to compare against before throwing the error. Includes test.
1 parent ce14c66 commit 8f928f7

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ public function process(File $phpcsFile, $stackPtr)
5151
$fullPath = basename($filename);
5252
$fileName = substr($fullPath, 0, strrpos($fullPath, '.'));
5353

54-
$tokens = $phpcsFile->getTokens();
55-
$decName = $phpcsFile->findNext(T_STRING, $stackPtr);
54+
$tokens = $phpcsFile->getTokens();
55+
$ooName = $phpcsFile->getDeclarationName($stackPtr);
56+
if ($ooName === null) {
57+
// Probably parse error/live coding.
58+
return;
59+
}
5660

57-
if ($tokens[$decName]['content'] !== $fileName) {
61+
if ($ooName !== $fileName) {
5862
$error = '%s name doesn\'t match filename; expected "%s %s"';
5963
$data = [
6064
ucfirst($tokens[$stackPtr]['content']),
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Intentional parse error. This should be the only test in this file.
4+
// Class missing class name should be ignored.
5+
6+
class

0 commit comments

Comments
 (0)