From 9185a9f89eeb53bf9919757fa0e1d80c312e9cbc Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Sun, 13 Oct 2019 18:36:53 +0530 Subject: [PATCH 1/5] import static test ruleset --- .../Sniffs/Namespaces/UseDeclarationSniff.php | 51 +++++++++++++++++++ .../Namespaces/UseDeclarationUnitTest.inc | 4 ++ .../Namespaces/UseDeclarationUnitTest.php | 30 +++++++++++ Magento2/ruleset.xml | 4 ++ 4 files changed, 89 insertions(+) create mode 100644 Magento2/Sniffs/Namespaces/UseDeclarationSniff.php create mode 100644 Magento2/Tests/Namespaces/UseDeclarationUnitTest.inc create mode 100644 Magento2/Tests/Namespaces/UseDeclarationUnitTest.php diff --git a/Magento2/Sniffs/Namespaces/UseDeclarationSniff.php b/Magento2/Sniffs/Namespaces/UseDeclarationSniff.php new file mode 100644 index 00000000..b7b52b8b --- /dev/null +++ b/Magento2/Sniffs/Namespaces/UseDeclarationSniff.php @@ -0,0 +1,51 @@ +findNext([T_COMMA, T_SEMICOLON, T_OPEN_USE_GROUP, T_CLOSE_TAG], ($stackPtr + 1)); + $getTokenAsContent = $phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr)); + if (strpos($getTokenAsContent, $this->_prohibitNamespace) !== false) { + $phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode); + } + } +} diff --git a/Magento2/Tests/Namespaces/UseDeclarationUnitTest.inc b/Magento2/Tests/Namespaces/UseDeclarationUnitTest.inc new file mode 100644 index 00000000..553f55ec --- /dev/null +++ b/Magento2/Tests/Namespaces/UseDeclarationUnitTest.inc @@ -0,0 +1,4 @@ + 1]; + } +} diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 9d65e166..8f6f495f 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -189,6 +189,10 @@ *Test.php */tests/* + + 8 + warning + 8 warning From 80bfe89d15f2540c69f337ca78f5e6b7c3704531 Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Thu, 17 Oct 2019 00:23:26 +0530 Subject: [PATCH 2/5] Readme file and feedback changes --- .../Namespaces/ImportsFromTestNamespaceSniff.md | 13 +++++++++++++ ...nSniff.php => ImportsFromTestNamespaceSniff.php} | 10 +++++----- ...est.inc => ImportsFromTestNamespaceUnitTest.inc} | 2 +- ...est.php => ImportsFromTestNamespaceUnitTest.php} | 2 +- Magento2/ruleset.xml | 2 +- 5 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.md rename Magento2/Sniffs/Namespaces/{UseDeclarationSniff.php => ImportsFromTestNamespaceSniff.php} (72%) rename Magento2/Tests/Namespaces/{UseDeclarationUnitTest.inc => ImportsFromTestNamespaceUnitTest.inc} (69%) rename Magento2/Tests/Namespaces/{UseDeclarationUnitTest.php => ImportsFromTestNamespaceUnitTest.php} (86%) 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/UseDeclarationSniff.php b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php similarity index 72% rename from Magento2/Sniffs/Namespaces/UseDeclarationSniff.php rename to Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php index b7b52b8b..b1078611 100644 --- a/Magento2/Sniffs/Namespaces/UseDeclarationSniff.php +++ b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php @@ -11,23 +11,23 @@ /** * Detects static test namespace. */ -class UseDeclarationSniff implements Sniff +class ImportsFromTestNamespaceSniff implements Sniff { /** * @var string */ - private $_prohibitNamespace = 'Magento\Tests'; + private $prohibitNamespace = 'Magento\Tests'; /** * @var string */ - protected $warningMessage = 'Incorrect namespace has been imported.'; + protected $warningMessage = 'Application modules should not use classed from test modules.'; /** * @var string */ - protected $warningCode = 'WrongImportNamespaces'; + protected $warningCode = 'WrongImport'; /** * @inheritdoc @@ -44,7 +44,7 @@ public function process(File $phpcsFile, $stackPtr) { $next = $phpcsFile->findNext([T_COMMA, T_SEMICOLON, T_OPEN_USE_GROUP, T_CLOSE_TAG], ($stackPtr + 1)); $getTokenAsContent = $phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr)); - if (strpos($getTokenAsContent, $this->_prohibitNamespace) !== false) { + if (strpos($getTokenAsContent, $this->prohibitNamespace) !== false) { $phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode); } } diff --git a/Magento2/Tests/Namespaces/UseDeclarationUnitTest.inc b/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc similarity index 69% rename from Magento2/Tests/Namespaces/UseDeclarationUnitTest.inc rename to Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc index 553f55ec..13c889c8 100644 --- a/Magento2/Tests/Namespaces/UseDeclarationUnitTest.inc +++ b/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc @@ -1,4 +1,4 @@ *Test.php */tests/* - + 8 warning From 975977da7f566c0445c85623f72990d96d05130f Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Sun, 17 Nov 2019 18:52:08 +0530 Subject: [PATCH 3/5] Sniff added for grouped namespace --- .../ImportsFromTestNamespaceSniff.php | 19 +++++++++++++++++++ .../ImportsFromTestNamespaceUnitTest.inc | 4 +++- .../ImportsFromTestNamespaceUnitTest.php | 5 ++++- Magento2/ruleset.xml | 2 +- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php index b1078611..8d976b4b 100644 --- a/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php +++ b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php @@ -7,6 +7,7 @@ use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Util\Tokens; /** * Detects static test namespace. @@ -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); + } } } diff --git a/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc b/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc index 13c889c8..50d339a1 100644 --- a/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc +++ b/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc @@ -1,4 +1,6 @@ 1]; + return [ + 2 => 1, + 5 => 1 + ]; } } diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 9810723b..70c26495 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -189,7 +189,7 @@ *Test.php */tests/* - + 8 warning From 199057a8ca960e0bb191ec8fd7a409fbd91c8a51 Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Wed, 4 Dec 2019 09:00:32 +0200 Subject: [PATCH 4/5] magento/magento-coding-standard#149 No imports from test namespace Exclude running for tests --- Magento2/ruleset.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 70c26495..c4fc6580 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -192,6 +192,11 @@ 8 warning + */_files/* + */Fixtures/* + */Test/* + *Test.php + */tests/* 8 From 4040cd53b27dd10379341fdcad3df726a956691e Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Wed, 11 Dec 2019 13:53:27 -0600 Subject: [PATCH 5/5] magento/magento-coding-standard#142: [New Rule] No imports from static tests namespaces --- Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php index 8d976b4b..3e5dbce7 100644 --- a/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php +++ b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php @@ -14,7 +14,6 @@ */ class ImportsFromTestNamespaceSniff implements Sniff { - /** * @var string */ @@ -53,7 +52,7 @@ public function process(File $phpcsFile, $stackPtr) && $tokens[$next]['code'] !== T_SEMICOLON && $tokens[$next]['code'] !== T_CLOSE_TAG ) { - $baseUse = rtrim($phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr))); + $baseUse = rtrim($phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr))); $baseUse = str_replace('use \\', '', $baseUse); $closingCurly = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($next + 1)); do { @@ -64,7 +63,7 @@ public function process(File $phpcsFile, $stackPtr) $phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode); return; } - } while ($next != false); + } while ($next !== false); } } }