Skip to content

#142 : [New Rule] No imports from static tests namespaces #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Magento2/Sniffs/Namespaces/UseDeclarationSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have UseDeclarationSniff.md file that will describe the issue behind

/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Sniffs\Namespaces;

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

/**
* Detects static test namespace.
*/
class UseDeclarationSniff implements Sniff
{

/**
* @var string
*/
private $_prohibitNamespace = 'Magento\Tests';

/**
* @var string
*/
protected $warningMessage = 'Incorrect namespace has been imported.';

/**
* @var string
*/
protected $warningCode = 'WrongImportNamespaces';

/**
* @inheritdoc
*/
public function register()
{
return [T_USE];
}

/**
* @inheritdoc
*/
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) {
$phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode);
}
}
}
4 changes: 4 additions & 0 deletions Magento2/Tests/Namespaces/UseDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
use Magento\Tests;
use Magento\Foo\Tests as FakeTest;
use Magento\Real\Classes;
30 changes: 30 additions & 0 deletions Magento2/Tests/Namespaces/UseDeclarationUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Tests\Namespaces;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Class InterfaceNameUnitTest
*/
class UseDeclarationUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList()
{
return [];
}

/**
* @inheritdoc
*/
public function getWarningList()
{
return [2 => 1];
}
}
4 changes: 4 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@
<exclude-pattern>*Test.php</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="Magento2.Namespaces.UseDeclaration">
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.NamingConvention.InterfaceName">
<severity>8</severity>
<type>warning</type>
Expand Down