Skip to content

Commit

Permalink
Readme file and feedback changes
Browse files Browse the repository at this point in the history
  • Loading branch information
konarshankar07 committed Oct 16, 2019
1 parent 9185a9f commit 80bfe89
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
13 changes: 13 additions & 0 deletions Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
use Magento\Tests;
use Magento\Foo\Tests as FakeTest;
use Magento\Real\Classes;
use Magento\Real\Classes;
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Class InterfaceNameUnitTest
*/
class UseDeclarationUnitTest extends AbstractSniffUnitTest
class ImportsFromTestNamespaceUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
Expand Down
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.UseDeclaration">
<rule ref="Magento2.Namespaces.ImportsFromTestNamespaceSniff">
<severity>8</severity>
<type>warning</type>
</rule>
Expand Down

0 comments on commit 80bfe89

Please sign in to comment.