diff --git a/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.md b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.md new file mode 100644 index 00000000..113e4ec6 --- /dev/null +++ b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.md @@ -0,0 +1,13 @@ +# Rule: Do not import from `Test` namespaces +## Background +Sometimes IDE imports the namespace with `Test` automatically for return data type like string, float etc or any other means. + +## Reasoning +Time to time we're getting issue with running tests on PRs in magento/magento2 repository because someone imported `\Magento\Tests\NamingConvention\true\string` by mistake. As result - we have "No build reports available" for "Database Compare build", "Functional Tests build", "Sample Data Tests build" while Static tests are shown as "failing" but in results - we don't really have reason + +## How it works +Any occurrence starts with `Magento\Tests` in import from the namespace will raise the warning. + +## How to fix + +Remove `Magento\Tests` from the imported namespaces diff --git a/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php new file mode 100644 index 00000000..3e5dbce7 --- /dev/null +++ b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php @@ -0,0 +1,69 @@ +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); + } + } +} diff --git a/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc b/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc new file mode 100644 index 00000000..50d339a1 --- /dev/null +++ b/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc @@ -0,0 +1,6 @@ + 1, + 5 => 1 + ]; + } +} diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 9d65e166..c4fc6580 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -189,6 +189,15 @@ *Test.php */tests/* + + 8 + warning + */_files/* + */Fixtures/* + */Test/* + *Test.php + */tests/* + 8 warning