From 9864d764222b022a4fa3111b01aff531ab00fd47 Mon Sep 17 00:00:00 2001 From: Qiangjun Date: Sat, 11 Apr 2020 12:38:35 +0800 Subject: [PATCH] Issue #3126781: Add a sniff to fix class name or interface name not identical with the filename --- ...rfaceNameNotIdenticalWithFilenameSniff.php | 77 +++++++++++++++++++ ...otIdenticalWithFilenameInterface.php.fixed | 5 ++ ...rInterfaceNameNotIdenticalWithFilename.inc | 8 ++ ...faceNameNotIdenticalWithFilename.inc.fixed | 8 ++ ...eNameNotIdenticalWithFilenameInterface.inc | 8 ++ ...otIdenticalWithFilenameInterface.inc.fixed | 8 ++ ...ceNameNotIdenticalWithFilenameUnitTest.php | 70 +++++++++++++++++ 7 files changed, 184 insertions(+) create mode 100644 coder_sniffer/Drupal/Sniffs/Classes/ClassOrInterfaceNameNotIdenticalWithFilenameSniff.php create mode 100644 coder_sniffer/Drupal/Test/ClassOrInterfaceNameNotIdenticalWithFilenameInterface.php.fixed create mode 100644 coder_sniffer/Drupal/Test/Classes/ClassOrInterfaceNameNotIdenticalWithFilename.inc create mode 100644 coder_sniffer/Drupal/Test/Classes/ClassOrInterfaceNameNotIdenticalWithFilename.inc.fixed create mode 100644 coder_sniffer/Drupal/Test/Classes/ClassOrInterfaceNameNotIdenticalWithFilenameInterface.inc create mode 100644 coder_sniffer/Drupal/Test/Classes/ClassOrInterfaceNameNotIdenticalWithFilenameInterface.inc.fixed create mode 100644 coder_sniffer/Drupal/Test/Classes/ClassOrInterfaceNameNotIdenticalWithFilenameUnitTest.php diff --git a/coder_sniffer/Drupal/Sniffs/Classes/ClassOrInterfaceNameNotIdenticalWithFilenameSniff.php b/coder_sniffer/Drupal/Sniffs/Classes/ClassOrInterfaceNameNotIdenticalWithFilenameSniff.php new file mode 100644 index 00000000..f6ed6380 --- /dev/null +++ b/coder_sniffer/Drupal/Sniffs/Classes/ClassOrInterfaceNameNotIdenticalWithFilenameSniff.php @@ -0,0 +1,77 @@ + + */ + public function register() + { + return [ + T_CLASS, + T_INTERFACE, + ]; + + }//end register() + + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in + * the stack passed in $tokens. + * + * @return void + */ + public function process(File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + $pathInfo = pathinfo($phpcsFile->getFilename()); + $filename = $pathInfo['filename']; + $nameIndex = $phpcsFile->findNext(T_STRING, $stackPtr); + if ($nameIndex === false) { + return; + } + + $classOrInterfaceName = $tokens[$nameIndex]['content']; + if ($filename !== $classOrInterfaceName && strtolower($filename) === strtolower($classOrInterfaceName)) { + $type = $tokens[$stackPtr]['type']; + if ($type === 'T_CLASS') { + $phpcsFile->addFixableWarning("Class name '$classOrInterfaceName' is not identical with its filename '$filename' ", $nameIndex, 'ClassOrInterfaceNameNotIdenticalWithFilename'); + } else { + $phpcsFile->addFixableWarning("Interface name '$classOrInterfaceName' is not identical with its filename '$filename' ", $nameIndex, 'ClassOrInterfaceNameNotIdenticalWithFilename'); + } + + if ($phpcsFile->fixer !== null) { + $phpcsFile->fixer->replaceToken($nameIndex, $filename); + } + } + + }//end process() + + +}//end class diff --git a/coder_sniffer/Drupal/Test/ClassOrInterfaceNameNotIdenticalWithFilenameInterface.php.fixed b/coder_sniffer/Drupal/Test/ClassOrInterfaceNameNotIdenticalWithFilenameInterface.php.fixed new file mode 100644 index 00000000..2dd1fb8d --- /dev/null +++ b/coder_sniffer/Drupal/Test/ClassOrInterfaceNameNotIdenticalWithFilenameInterface.php.fixed @@ -0,0 +1,5 @@ + + */ + protected function getErrorList(string $testFile): array + { + return []; + + }//end getErrorList() + + + /** + * Returns the lines where warnings should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of warnings that should occur on that line. + * + * @param string $testFile The name of the file being tested. + * + * @return array + */ + protected function getWarningList(string $testFile): array + { + if ($testFile === 'ClassOrInterfaceNameNotIdenticalWithFilename.inc') { + return [6 => 1]; + } + + if ($testFile === 'ClassOrInterfaceNameNotIdenticalWithFilenameInterface.inc') { + return [6 => 1]; + } + + return []; + + }//end getWarningList() + + + /** + * Returns a list of test files that should be checked. + * + * @param string $testFileBase The base path that the unit tests files will have. + * + * @return array + */ + protected function getTestFiles($testFileBase): array + { + return [ + __DIR__.'/ClassOrInterfaceNameNotIdenticalWithFilename.inc', + __DIR__.'/ClassOrInterfaceNameNotIdenticalWithFilenameInterface.inc', + ]; + + }//end getTestFiles() + + +}//end class