From 80bfe89d15f2540c69f337ca78f5e6b7c3704531 Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Thu, 17 Oct 2019 00:23:26 +0530 Subject: [PATCH] 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