Skip to content

Commit 1ee835c

Browse files
committed
Fixed bug #19270 : DuplicateClassName does not handle namespaces correctly
1 parent d28b6d3 commit 1ee835c

File tree

6 files changed

+71
-21
lines changed

6 files changed

+71
-21
lines changed

CodeSniffer/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,49 @@ public function process(array $files)
4242
foreach ($files as $phpcsFile) {
4343
$tokens = $phpcsFile->getTokens();
4444

45-
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), 0);
45+
$namespace = '';
46+
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_NAMESPACE), 0);
4647
while ($stackPtr !== false) {
47-
$nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
48-
$name = $tokens[$nameToken]['content'];
49-
$compareName = strtolower($name);
50-
if (isset($foundClasses[$compareName]) === true) {
51-
$type = strtolower($tokens[$stackPtr]['content']);
52-
$file = $foundClasses[$compareName]['file'];
53-
$line = $foundClasses[$compareName]['line'];
54-
$error = 'Duplicate %s name "%s" found; first defined in %s on line %s';
55-
$data = array(
56-
$type,
57-
$name,
58-
$file,
59-
$line,
60-
);
61-
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
48+
// Keep track of what namespace we are in.
49+
if ($tokens[$stackPtr]['code'] === T_NAMESPACE) {
50+
$nsEnd = $phpcsFile->findNext(
51+
array(T_NS_SEPARATOR, T_STRING, T_WHITESPACE),
52+
($stackPtr + 1),
53+
null,
54+
true
55+
);
56+
57+
$namespace = trim($phpcsFile->getTokensAsString(($stackPtr + 1), ($nsEnd - $stackPtr - 1)));
58+
$stackPtr = $nsEnd;
6259
} else {
63-
$foundClasses[$compareName] = array(
64-
'file' => $phpcsFile->getFilename(),
65-
'line' => $tokens[$stackPtr]['line'],
66-
);
60+
$nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
61+
$name = $tokens[$nameToken]['content'];
62+
if ($namespace !== '') {
63+
$name = $namespace.'\\'.$name;
64+
}
65+
66+
$compareName = strtolower($name);
67+
if (isset($foundClasses[$compareName]) === true) {
68+
$type = strtolower($tokens[$stackPtr]['content']);
69+
$file = $foundClasses[$compareName]['file'];
70+
$line = $foundClasses[$compareName]['line'];
71+
$error = 'Duplicate %s name "%s" found; first defined in %s on line %s';
72+
$data = array(
73+
$type,
74+
$name,
75+
$file,
76+
$line,
77+
);
78+
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
79+
} else {
80+
$foundClasses[$compareName] = array(
81+
'file' => $phpcsFile->getFilename(),
82+
'line' => $tokens[$stackPtr]['line'],
83+
);
84+
}
6785
}
6886

69-
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), ($stackPtr + 1));
87+
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_NAMESPACE), ($stackPtr + 1));
7088
}//end while
7189

7290
}//end foreach
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace A\B {
3+
class MyClass {}
4+
interface MyInterface {}
5+
}
6+
7+
namespace D {
8+
class MyClass {}
9+
}
10+
?>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace C;
3+
class MyClass {}
4+
interface MyInterface {}
5+
?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace C;
3+
class MyClass {}
4+
interface YourInterface {}
5+
6+
namespace D;
7+
class MyClass {}
8+
?>

CodeSniffer/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public function getErrorList()
5151
* The key of the array should represent the line number and the value
5252
* should represent the number of warnings that should occur on that line.
5353
*
54+
* @param string $testFile The name of the file being tested.
55+
*
5456
* @return array(int => int)
5557
*/
5658
public function getWarningList($testFile='')
@@ -68,6 +70,12 @@ public function getWarningList($testFile='')
6870
3 => 1,
6971
);
7072
break;
73+
case 'DuplicateClassNameUnitTest.5.inc':
74+
return array(
75+
3 => 1,
76+
7 => 1,
77+
);
78+
break;
7179
default:
7280
return array();
7381
break;

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
4646
- Fixed bug #19256 : T_DOC_COMMENT in CSS files breaks ClassDefinitionNameSpacingSniff
4747
-- Thanks to Klaus Purer for the patch
4848
- Fixed bug #19264 : Squiz.PHP.NonExecutableCode does not handle RETURN in CASE without BREAK
49+
- Fixed bug #19270 : DuplicateClassName does not handle namespaces correctly
4950
</notes>
5051
<contents>
5152
<dir name="/">

0 commit comments

Comments
 (0)