Skip to content

Commit 8e2b570

Browse files
committed
Fixed bug #2095 : PSR2.Namespaces.NamespaceDeclaration does not handle namespaces defined over multiple lines
1 parent 92822a0 commit 8e2b570

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
7676
- Fixed bug #2065 : Generic.ControlStructures.InlineControlStructure fixing fails when inline control structure contains closure
7777
- Fixed bug #2072 : Squiz.Arrays.ArrayDeclaration throws NoComma error when array value is a shorthand IF statement
7878
- Fixed bug #2082 : File with "defined() or define()" syntax triggers PSR1.Files.SideEffects.FoundWithSymbols
79+
- Fixed bug #2095 : PSR2.Namespaces.NamespaceDeclaration does not handle namespaces defined over multiple lines
7980
</notes>
8081
<contents>
8182
<dir name="/">

src/Standards/PSR2/Sniffs/Namespaces/NamespaceDeclarationSniff.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ public function process(File $phpcsFile, $stackPtr)
4141
{
4242
$tokens = $phpcsFile->getTokens();
4343

44-
for ($i = ($stackPtr + 1); $i < ($phpcsFile->numTokens - 1); $i++) {
45-
if ($tokens[$i]['line'] === $tokens[$stackPtr]['line']) {
44+
$end = $phpcsFile->findEndOfStatement($stackPtr);
45+
for ($i = ($end + 1); $i < ($phpcsFile->numTokens - 1); $i++) {
46+
if ($tokens[$i]['line'] === $tokens[$end]['line']) {
4647
continue;
4748
}
4849

src/Standards/PSR2/Tests/Namespaces/NamespaceDeclarationUnitTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ namespace Vendor\Package;
1111

1212
use BarClass as Bar;
1313

14+
namespace Vendor
15+
\Package;
16+
17+
namespace Vendor\
18+
Package;
19+
namespace Vendor\
20+
21+
Package;
1422
namespace Vendor\Package;

src/Standards/PSR2/Tests/Namespaces/NamespaceDeclarationUnitTest.inc.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,14 @@ namespace Vendor\Package;
1111

1212
use BarClass as Bar;
1313

14+
namespace Vendor
15+
\Package;
16+
17+
namespace Vendor\
18+
Package;
19+
20+
namespace Vendor\
21+
22+
Package;
23+
1424
namespace Vendor\Package;

src/Standards/PSR2/Tests/Namespaces/NamespaceDeclarationUnitTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ class NamespaceDeclarationUnitTest extends AbstractSniffUnitTest
2626
public function getErrorList()
2727
{
2828
return [
29-
6 => 1,
30-
9 => 1,
29+
6 => 1,
30+
9 => 1,
31+
17 => 1,
32+
19 => 1,
3133
];
3234

3335
}//end getErrorList()

0 commit comments

Comments
 (0)