Skip to content

Commit cbcb88f

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 281519a commit cbcb88f

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,24 @@ public function process(File $phpcsFile, $stackPtr)
7171
if ($tokens[$stackPtr]['code'] === T_NAMESPACE) {
7272
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
7373
if ($nextNonEmpty !== false) {
74-
$find = Tokens::$emptyTokens;
75-
$find[] = T_STRING;
76-
$find[] = T_NAME_QUALIFIED;
77-
78-
$nsEnd = $phpcsFile->findNext(
79-
$find,
80-
($stackPtr + 1),
81-
null,
82-
true
83-
);
84-
85-
$namespace = trim($phpcsFile->getTokensAsString(($stackPtr + 1), ($nsEnd - $stackPtr - 1)));
86-
$stackPtr = $nsEnd;
74+
$namespace = '';
75+
for ($i = $nextNonEmpty; $i < $phpcsFile->numTokens; $i++) {
76+
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
77+
continue;
78+
}
79+
80+
if ($tokens[$i]['code'] !== T_NAME_QUALIFIED
81+
// Allow for pre-PHP 8.0 style declarations containing comments and whitespace.
82+
&& $tokens[$i]['code'] !== T_STRING
83+
&& $tokens[$i]['code'] !== T_NS_SEPARATOR
84+
) {
85+
break;
86+
}
87+
88+
$namespace .= $tokens[$i]['content'];
89+
}
90+
91+
$stackPtr = $i;
8792
}
8893
} else {
8994
$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)