Skip to content

Commit 9c7cffe

Browse files
committed
Generic/DuplicateClassName: bug fix - ignore comments in namespace declarations
A comment in a namespace declaration statement would throw the sniff off, leading to false positives and false negatives. Fixed now. Includes unit tests. Additionally, a unit test for classes declared within a scoped global namespace has also been added.
1 parent 4c5c195 commit 9c7cffe

File tree

5 files changed

+43
-13
lines changed

5 files changed

+43
-13
lines changed

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,20 @@ public function process(File $phpcsFile, $stackPtr)
7474
// Ignore namespace keyword used as operator.
7575
&& $tokens[$nextNonEmpty]['code'] !== T_NS_SEPARATOR
7676
) {
77-
$nsEnd = $phpcsFile->findNext(
78-
[
79-
T_NS_SEPARATOR,
80-
T_STRING,
81-
T_WHITESPACE,
82-
],
83-
($stackPtr + 1),
84-
null,
85-
true
86-
);
87-
88-
$namespace = trim($phpcsFile->getTokensAsString(($stackPtr + 1), ($nsEnd - $stackPtr - 1)));
89-
$stackPtr = $nsEnd;
77+
$namespace = '';
78+
for ($i = $nextNonEmpty; $i < $phpcsFile->numTokens; $i++) {
79+
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
80+
continue;
81+
}
82+
83+
if ($tokens[$i]['code'] !== T_STRING && $tokens[$i]['code'] !== T_NS_SEPARATOR) {
84+
break;
85+
}
86+
87+
$namespace .= $tokens[$i]['content'];
88+
}
89+
90+
$stackPtr = $i;
9091
}
9192
} else {
9293
$nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);

src/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.7.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ interface YourInterface {}
77
namespace F;
88
namespace\functionCall();
99
class MyClass {}
10+
11+
namespace /*comment*/ G;
12+
namespace\functionCall();
13+
class MyClass {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace Foo\Bar;
3+
class MyClass {}
4+
interface YourInterface {}
5+
6+
namespace Foo /*comment*/ \ /*comment*/ Bar;
7+
class MyClass {}
8+
interface YourInterface {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace {
3+
class MyClass {}
4+
trait YourTrait {}
5+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ public function getWarningList($testFile='')
7373
case 'DuplicateClassNameUnitTest.6.inc':
7474
return [10 => 1];
7575

76+
case 'DuplicateClassNameUnitTest.8.inc':
77+
return [
78+
7 => 1,
79+
8 => 1,
80+
];
81+
82+
case 'DuplicateClassNameUnitTest.9.inc':
83+
return [
84+
3 => 1,
85+
4 => 1,
86+
];
87+
7688
default:
7789
return [];
7890
}//end switch

0 commit comments

Comments
 (0)