Skip to content

Commit

Permalink
import static test ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
konarshankar07 committed Oct 13, 2019
1 parent f062ab4 commit 9185a9f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
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
/**
* 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

0 comments on commit 9185a9f

Please sign in to comment.