diff --git a/src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php b/src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php index 19ff1edf64..ca1ed09991 100644 --- a/src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php +++ b/src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php @@ -11,6 +11,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Util\Tokens; class DuplicateClassNameSniff implements Sniff { @@ -55,63 +56,70 @@ public function process(File $phpcsFile, $stackPtr) T_TRAIT, T_ENUM, T_NAMESPACE, - T_CLOSE_TAG, ]; $stackPtr = $phpcsFile->findNext($findTokens, ($stackPtr + 1)); while ($stackPtr !== false) { - if ($tokens[$stackPtr]['code'] === T_CLOSE_TAG) { - // We can stop here. The sniff will continue from the next open - // tag when PHPCS reaches that token, if there is one. - return; - } - // Keep track of what namespace we are in. if ($tokens[$stackPtr]['code'] === T_NAMESPACE) { - $nsEnd = $phpcsFile->findNext( - [ - T_NS_SEPARATOR, - T_STRING, - T_WHITESPACE, - ], - ($stackPtr + 1), - null, - true - ); - - $namespace = trim($phpcsFile->getTokensAsString(($stackPtr + 1), ($nsEnd - $stackPtr - 1))); - $stackPtr = $nsEnd; - } else { - $nameToken = $phpcsFile->findNext(T_STRING, $stackPtr); - $name = $tokens[$nameToken]['content']; - if ($namespace !== '') { - $name = $namespace.'\\'.$name; + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); + if ($nextNonEmpty !== false + // Ignore namespace keyword used as operator. + && $tokens[$nextNonEmpty]['code'] !== T_NS_SEPARATOR + ) { + $namespace = ''; + for ($i = $nextNonEmpty; $i < $phpcsFile->numTokens; $i++) { + if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) { + continue; + } + + if ($tokens[$i]['code'] !== T_STRING && $tokens[$i]['code'] !== T_NS_SEPARATOR) { + break; + } + + $namespace .= $tokens[$i]['content']; + } + + $stackPtr = $i; } - - $compareName = strtolower($name); - if (isset($this->foundClasses[$compareName]) === true) { - $type = strtolower($tokens[$stackPtr]['content']); - $file = $this->foundClasses[$compareName]['file']; - $line = $this->foundClasses[$compareName]['line']; - $error = 'Duplicate %s name "%s" found; first defined in %s on line %s'; - $data = [ - $type, - $name, - $file, - $line, - ]; - $phpcsFile->addWarning($error, $stackPtr, 'Found', $data); - } else { - $this->foundClasses[$compareName] = [ - 'file' => $phpcsFile->getFilename(), - 'line' => $tokens[$stackPtr]['line'], - ]; + } else { + $name = $phpcsFile->getDeclarationName($stackPtr); + if (empty($name) === false) { + if ($namespace !== '') { + $name = $namespace.'\\'.$name; + } + + $compareName = strtolower($name); + if (isset($this->foundClasses[$compareName]) === true) { + $type = strtolower($tokens[$stackPtr]['content']); + $file = $this->foundClasses[$compareName]['file']; + $line = $this->foundClasses[$compareName]['line']; + $error = 'Duplicate %s name "%s" found; first defined in %s on line %s'; + $data = [ + $type, + $name, + $file, + $line, + ]; + $phpcsFile->addWarning($error, $stackPtr, 'Found', $data); + } else { + $this->foundClasses[$compareName] = [ + 'file' => $phpcsFile->getFilename(), + 'line' => $tokens[$stackPtr]['line'], + ]; + } + }//end if + + if (isset($tokens[$stackPtr]['scope_closer']) === true) { + $stackPtr = $tokens[$stackPtr]['scope_closer']; } }//end if $stackPtr = $phpcsFile->findNext($findTokens, ($stackPtr + 1)); }//end while + return $phpcsFile->numTokens; + }//end process() diff --git a/src/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.10.inc b/src/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.10.inc new file mode 100644 index 0000000000..d5ab20e3ed --- /dev/null +++ b/src/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.10.inc @@ -0,0 +1,6 @@ + + + + + + 1]; + case 'DuplicateClassNameUnitTest.8.inc': + return [ + 7 => 1, + 8 => 1, + ]; + + case 'DuplicateClassNameUnitTest.9.inc': + return [ + 3 => 1, + 4 => 1, + ]; + + case 'DuplicateClassNameUnitTest.11.inc': + return [13 => 1]; + default: return []; }//end switch