diff --git a/src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php b/src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php index fc3ec0473b..a5e53ac1ba 100644 --- a/src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php +++ b/src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php @@ -11,6 +11,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Util\Tokens; class ClassFileNameSniff implements Sniff { @@ -23,12 +24,10 @@ class ClassFileNameSniff implements Sniff */ public function register() { - return [ - T_CLASS, - T_INTERFACE, - T_TRAIT, - T_ENUM, - ]; + $targets = Tokens::$ooScopeTokens; + unset($targets[T_ANON_CLASS]); + + return $targets; }//end register() @@ -44,22 +43,27 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - $fullPath = basename($phpcsFile->getFilename()); - $fileName = substr($fullPath, 0, strrpos($fullPath, '.')); - if ($fileName === '') { - // No filename probably means STDIN, so we can't do this check. - return; + $fullPath = $phpcsFile->getFilename(); + if ($fullPath === 'STDIN') { + return $phpcsFile->numTokens; } - $tokens = $phpcsFile->getTokens(); - $decName = $phpcsFile->findNext(T_STRING, $stackPtr); + $fileName = basename($fullPath); + $fileNoExt = substr($fileName, 0, strrpos($fileName, '.')); + $extension = substr($fileName, (strrpos($fileName, '.') + 1)); + + $tokens = $phpcsFile->getTokens(); + $ooName = $phpcsFile->getDeclarationName($stackPtr); + if ($ooName === null) { + // Probably parse error/live coding. + return; + } - if ($tokens[$decName]['content'] !== $fileName) { - $error = '%s name doesn\'t match filename; expected "%s %s"'; + if ($ooName !== $fileNoExt) { + $error = 'Filename doesn\'t match %s name; expected file name "%s"'; $data = [ - ucfirst($tokens[$stackPtr]['content']), $tokens[$stackPtr]['content'], - $fileName, + $ooName.'.'.$extension, ]; $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data); } diff --git a/src/Standards/Squiz/Tests/Classes/ClassFileName Spaces In FilenameUnitTest.inc b/src/Standards/Squiz/Tests/Classes/ClassFileName Spaces In FilenameUnitTest.inc new file mode 100644 index 0000000000..a89e3d03c3 --- /dev/null +++ b/src/Standards/Squiz/Tests/Classes/ClassFileName Spaces In FilenameUnitTest.inc @@ -0,0 +1,7 @@ + +enum + // Comment + ExtraClassFileNameUnitTest {} diff --git a/src/Standards/Squiz/Tests/Classes/ClassFileNameUnitTest.php b/src/Standards/Squiz/Tests/Classes/ClassFileNameUnitTest.php index 7a01e9c09f..2678a05267 100644 --- a/src/Standards/Squiz/Tests/Classes/ClassFileNameUnitTest.php +++ b/src/Standards/Squiz/Tests/Classes/ClassFileNameUnitTest.php @@ -9,6 +9,7 @@ namespace PHP_CodeSniffer\Standards\Squiz\Tests\Classes; +use DirectoryIterator; use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; /** @@ -20,46 +21,109 @@ final class ClassFileNameUnitTest extends AbstractSniffUnitTest { + /** + * Get a list of all test files to check. + * + * These will have the same base as the sniff name but different extensions. + * We ignore the .php file as it is the class. + * + * @param string $testFileBase The base path that the unit tests files will have. + * + * @return string[] + */ + protected function getTestFiles($testFileBase) + { + $testFiles = []; + + $dir = substr($testFileBase, 0, strrpos($testFileBase, DIRECTORY_SEPARATOR)); + $di = new DirectoryIterator($dir); + + // Strip off the path and the "UnitTest." suffix from the $testFileBase to allow + // for some less conventionally named test case files. + $fileBase = str_replace($dir, '', $testFileBase); + $fileBase = substr($fileBase, 1, -9); + + foreach ($di as $file) { + $fileName = $file->getBasename('UnitTest.inc'); + $extension = $file->getExtension(); + if (substr($fileName, 0, strlen($fileBase)) === $fileBase + && $extension === 'inc' + ) { + $testFiles[] = $file->getPathname(); + } + } + + // Put them in order. + sort($testFiles, SORT_NATURAL); + + return $testFiles; + + }//end getTestFiles() + + /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * + * @param string $testFile The name of the file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 12 => 1, - 13 => 1, - 14 => 1, - 15 => 1, - 16 => 1, - 17 => 1, - 18 => 1, - 19 => 1, - 20 => 1, - 21 => 1, - 22 => 1, - 23 => 1, - 27 => 1, - 28 => 1, - 29 => 1, - 30 => 1, - 31 => 1, - 32 => 1, - 33 => 1, - 34 => 1, - 35 => 1, - 36 => 1, - 37 => 1, - 38 => 1, - 39 => 1, - 40 => 1, - 41 => 1, - 42 => 1, - ]; + switch ($testFile) { + case 'ClassFileNameUnitTest.inc': + return [ + 12 => 1, + 13 => 1, + 14 => 1, + 15 => 1, + 16 => 1, + 17 => 1, + 18 => 1, + 19 => 1, + 20 => 1, + 21 => 1, + 22 => 1, + 23 => 1, + 27 => 1, + 28 => 1, + 29 => 1, + 30 => 1, + 31 => 1, + 32 => 1, + 33 => 1, + 34 => 1, + 35 => 1, + 36 => 1, + 37 => 1, + 38 => 1, + 39 => 1, + 40 => 1, + 41 => 1, + 42 => 1, + ]; + + case 'ClassFileNameLiveCodingFailUnitTest.inc': + return [ + 6 => 1, + ]; + + case 'ClassFileName Spaces In FilenameUnitTest.inc': + return [ + 7 => 1, + ]; + + case 'ClassFileName-Dashes-In-FilenameUnitTest.inc': + return [ + 7 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList()