Skip to content

Commit 2017437

Browse files
committed
PSR12/CompoundNamespaceDepth: fix sniff to work with the PHP 8 identifier tokens
The existing unit tests already contain tests covering this change. The tests also include a test which is a parse error in PHP >= 8.0. The current fix will handle this the same in both PHP < 8 as well as PHP 8.0+ and will throw the expected errors even so. The parse error in PHP 8 has been annotated in the test case file.
1 parent d70c2a4 commit 2017437

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/Standards/PSR12/Sniffs/Namespaces/CompoundNamespaceDepthSniff.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public function process(File $phpcsFile, $stackPtr)
6262
continue;
6363
}
6464

65+
if ($tokens[$i]['code'] === T_NAME_FULLY_QUALIFIED || $tokens[$i]['code'] === T_NAME_QUALIFIED) {
66+
$depth += substr_count($tokens[$i]['content'], '\\');
67+
continue;
68+
}
69+
6570
if ($i === $end || $tokens[$i]['code'] === T_COMMA) {
6671
// End of a namespace.
6772
if ($depth > $this->maxDepth) {
@@ -72,7 +77,7 @@ public function process(File $phpcsFile, $stackPtr)
7277

7378
$depth = 1;
7479
}
75-
}
80+
}//end for
7681

7782
}//end process()
7883

src/Standards/PSR12/Tests/Namespaces/CompoundNamespaceDepthUnitTest.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use Vendor\Package\SomeNamespace\{
1212
ClassZ,
1313
};
1414

15+
// Parse error in PHP 8.0+, but will be detected correctly cross-version anyway.
1516
use Vendor\Package\SomeNamespace\{
1617
SubnamespaceOne /* comment */
1718
\AnotherNamespace // comment

src/Standards/PSR12/Tests/Namespaces/CompoundNamespaceDepthUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public function getErrorList()
3232
{
3333
return [
3434
10 => 1,
35-
18 => 1,
36-
21 => 1,
35+
19 => 1,
36+
22 => 1,
3737
];
3838

3939
}//end getErrorList()

0 commit comments

Comments
 (0)