Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed bug #19270 : DuplicateClassName does not handle namespaces corr…

…ectly
  • Loading branch information...
commit 1ee835c9b36623d4937506965469a4c60d2c4c50 1 parent d28b6d3
@gsherwood gsherwood authored
View
60 CodeSniffer/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php
@@ -42,31 +42,49 @@ public function process(array $files)
foreach ($files as $phpcsFile) {
$tokens = $phpcsFile->getTokens();
- $stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), 0);
+ $namespace = '';
+ $stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_NAMESPACE), 0);
while ($stackPtr !== false) {
- $nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
- $name = $tokens[$nameToken]['content'];
- $compareName = strtolower($name);
- if (isset($foundClasses[$compareName]) === true) {
- $type = strtolower($tokens[$stackPtr]['content']);
- $file = $foundClasses[$compareName]['file'];
- $line = $foundClasses[$compareName]['line'];
- $error = 'Duplicate %s name "%s" found; first defined in %s on line %s';
- $data = array(
- $type,
- $name,
- $file,
- $line,
- );
- $phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
+ // Keep track of what namespace we are in.
+ if ($tokens[$stackPtr]['code'] === T_NAMESPACE) {
+ $nsEnd = $phpcsFile->findNext(
+ array(T_NS_SEPARATOR, T_STRING, T_WHITESPACE),
+ ($stackPtr + 1),
+ null,
+ true
+ );
+
+ $namespace = trim($phpcsFile->getTokensAsString(($stackPtr + 1), ($nsEnd - $stackPtr - 1)));
+ $stackPtr = $nsEnd;
} else {
- $foundClasses[$compareName] = array(
- 'file' => $phpcsFile->getFilename(),
- 'line' => $tokens[$stackPtr]['line'],
- );
+ $nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
+ $name = $tokens[$nameToken]['content'];
+ if ($namespace !== '') {
+ $name = $namespace.'\\'.$name;
+ }
+
+ $compareName = strtolower($name);
+ if (isset($foundClasses[$compareName]) === true) {
+ $type = strtolower($tokens[$stackPtr]['content']);
+ $file = $foundClasses[$compareName]['file'];
+ $line = $foundClasses[$compareName]['line'];
+ $error = 'Duplicate %s name "%s" found; first defined in %s on line %s';
+ $data = array(
+ $type,
+ $name,
+ $file,
+ $line,
+ );
+ $phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
+ } else {
+ $foundClasses[$compareName] = array(
+ 'file' => $phpcsFile->getFilename(),
+ 'line' => $tokens[$stackPtr]['line'],
+ );
+ }
}
- $stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), ($stackPtr + 1));
+ $stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_NAMESPACE), ($stackPtr + 1));
}//end while
}//end foreach
View
10 CodeSniffer/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.3.inc
@@ -0,0 +1,10 @@
+<?php
+namespace A\B {
+ class MyClass {}
+ interface MyInterface {}
+}
+
+namespace D {
+ class MyClass {}
+}
+?>
View
5 CodeSniffer/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.4.inc
@@ -0,0 +1,5 @@
+<?php
+namespace C;
+class MyClass {}
+interface MyInterface {}
+?>
View
8 CodeSniffer/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.5.inc
@@ -0,0 +1,8 @@
+<?php
+namespace C;
+class MyClass {}
+interface YourInterface {}
+
+namespace D;
+class MyClass {}
+?>
View
8 CodeSniffer/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.php
@@ -51,6 +51,8 @@ public function getErrorList()
* 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(int => int)
*/
public function getWarningList($testFile='')
@@ -68,6 +70,12 @@ public function getWarningList($testFile='')
3 => 1,
);
break;
+ case 'DuplicateClassNameUnitTest.5.inc':
+ return array(
+ 3 => 1,
+ 7 => 1,
+ );
+ break;
default:
return array();
break;
View
1  package.xml
@@ -46,6 +46,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #19256 : T_DOC_COMMENT in CSS files breaks ClassDefinitionNameSpacingSniff
-- Thanks to Klaus Purer for the patch
- Fixed bug #19264 : Squiz.PHP.NonExecutableCode does not handle RETURN in CASE without BREAK
+ - Fixed bug #19270 : DuplicateClassName does not handle namespaces correctly
</notes>
<contents>
<dir name="/">

5 comments on commit 1ee835c

@sebastianbergmann

Any idea when PHP_CodeSniffer 1.3.3 will be released? Thanks!

@gsherwood
Owner

This fix was the last one I was aiming to get into 1.3.3, but I am still waiting on confirmation that another is also working for someone. If I get that confirmation, I'll release. So hopefully within a week.

@guilhermeblanco

@sebastianbergmann @gsherwood I'm going to re-run CodeSniffer here and give the feedback as soon as possible. =)

@guilhermeblanco

@gsherwood @squizlabs Verified and working correctly.

Already closed bug #18717: https://pear.php.net/bugs/bug.php?id=18717

Thanks a lot! =)

@gsherwood
Owner

Thanks a lot for getting back to me.

Please sign in to comment.
Something went wrong with that request. Please try again.