Skip to content

Commit 975977d

Browse files
Sniff added for grouped namespace
1 parent 80bfe89 commit 975977d

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use PHP_CodeSniffer\Sniffs\Sniff;
99
use PHP_CodeSniffer\Files\File;
10+
use PHP_CodeSniffer\Util\Tokens;
1011

1112
/**
1213
* Detects static test namespace.
@@ -43,9 +44,27 @@ public function register()
4344
public function process(File $phpcsFile, $stackPtr)
4445
{
4546
$next = $phpcsFile->findNext([T_COMMA, T_SEMICOLON, T_OPEN_USE_GROUP, T_CLOSE_TAG], ($stackPtr + 1));
47+
$tokens = $phpcsFile->getTokens();
4648
$getTokenAsContent = $phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr));
4749
if (strpos($getTokenAsContent, $this->prohibitNamespace) !== false) {
4850
$phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode);
4951
}
52+
if ($next !== false
53+
&& $tokens[$next]['code'] !== T_SEMICOLON
54+
&& $tokens[$next]['code'] !== T_CLOSE_TAG
55+
) {
56+
$baseUse = rtrim($phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr)));
57+
$baseUse = str_replace('use \\', '', $baseUse);
58+
$closingCurly = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($next + 1));
59+
do {
60+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), $closingCurly, true);
61+
$groupedAsContent = $baseUse. $tokens[$next]['content'];
62+
$next = $phpcsFile->findNext(T_COMMA, ($next + 1), $closingCurly);
63+
if (strpos($groupedAsContent, $this->prohibitNamespace) !== false) {
64+
$phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode);
65+
return;
66+
}
67+
} while ($next != false);
68+
}
5069
}
5170
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
22
use Magento\Tests;
33
use Magento\Foo\Tests as FakeTest;
4-
use Magento\Real\Classes;
4+
use Magento\Real\Classes;
5+
use \Magento\{Tests\String, Tests\Int};
6+
use \Magento\{Foo\string, Bar\float};

Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function getErrorList()
2525
*/
2626
public function getWarningList()
2727
{
28-
return [2 => 1];
28+
return [
29+
2 => 1,
30+
5 => 1
31+
];
2932
}
3033
}

Magento2/ruleset.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@
189189
<exclude-pattern>*Test.php</exclude-pattern>
190190
<exclude-pattern>*/tests/*</exclude-pattern>
191191
</rule>
192-
<rule ref="Magento2.Namespaces.ImportsFromTestNamespaceSniff">
192+
<rule ref="Magento2.Namespaces.ImportsFromTestNamespace">
193193
<severity>8</severity>
194194
<type>warning</type>
195195
</rule>

0 commit comments

Comments
 (0)