Skip to content

Commit

Permalink
Sniff added for grouped namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
konarshankar07 committed Nov 17, 2019
1 parent 80bfe89 commit 975977d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
19 changes: 19 additions & 0 deletions Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;

/**
* Detects static test namespace.
Expand Down Expand Up @@ -43,9 +44,27 @@ public function register()
public function process(File $phpcsFile, $stackPtr)
{
$next = $phpcsFile->findNext([T_COMMA, T_SEMICOLON, T_OPEN_USE_GROUP, T_CLOSE_TAG], ($stackPtr + 1));
$tokens = $phpcsFile->getTokens();
$getTokenAsContent = $phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr));
if (strpos($getTokenAsContent, $this->prohibitNamespace) !== false) {
$phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode);
}
if ($next !== false
&& $tokens[$next]['code'] !== T_SEMICOLON
&& $tokens[$next]['code'] !== T_CLOSE_TAG
) {
$baseUse = rtrim($phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr)));
$baseUse = str_replace('use \\', '', $baseUse);
$closingCurly = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($next + 1));
do {
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), $closingCurly, true);
$groupedAsContent = $baseUse. $tokens[$next]['content'];
$next = $phpcsFile->findNext(T_COMMA, ($next + 1), $closingCurly);
if (strpos($groupedAsContent, $this->prohibitNamespace) !== false) {
$phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode);
return;
}
} while ($next != false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use Magento\Tests;
use Magento\Foo\Tests as FakeTest;
use Magento\Real\Classes;
use Magento\Real\Classes;
use \Magento\{Tests\String, Tests\Int};
use \Magento\{Foo\string, Bar\float};
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function getErrorList()
*/
public function getWarningList()
{
return [2 => 1];
return [
2 => 1,
5 => 1
];
}
}
2 changes: 1 addition & 1 deletion Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
<exclude-pattern>*Test.php</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="Magento2.Namespaces.ImportsFromTestNamespaceSniff">
<rule ref="Magento2.Namespaces.ImportsFromTestNamespace">
<severity>8</severity>
<type>warning</type>
</rule>
Expand Down

0 comments on commit 975977d

Please sign in to comment.