Skip to content

MM-4941: [EQP][Sniffs Consolidation] Create new GitHub repo and move MEQP2 sniffs #1

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 3 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/*
/bin/*
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: php
php:
- 5.5
- 5.6
- 7.0
- 7.1
install: composer install --no-interaction --prefer-source
script:
- bin/phpunit
- bin/phpcs --standard=PSR2 Magento/ --extensions=php
84 changes: 84 additions & 0 deletions Magento/Sniffs/Classes/ObjectInstantiationSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sniffs\Classes;

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

/**
* Detects direct object instantiation via 'new' keyword.
*/
class ObjectInstantiationSniff implements Sniff
{
/**
* String representation of warning.
*
* @var string
*/
protected $warningMessage = 'Direct object instantiation (object of %s) is discouraged in Magento 2.';

/**
* Warning violation code.
*
* @var string
*/
protected $warningCode = 'FoundDirectInstantiation';

/**
* List of tokens which declares left bound of current scope.
*
* @var array
*/
protected $leftRangeTokens = [
T_WHITESPACE,
T_THROW
];

/**
* List of tokens which declares right bound of current scope.
*
* @var array
*/
protected $rightRangeTokens = [
T_STRING,
T_SELF,
T_STATIC,
T_VARIABLE,
T_NS_SEPARATOR
];

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

/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$leftLimit = $phpcsFile->findPrevious($this->leftRangeTokens, $stackPtr - 1, null, true);
$findThrow = $phpcsFile->findPrevious(T_THROW, $stackPtr - 1, $leftLimit);
if (!$findThrow) {
$classNameStart = $phpcsFile->findNext($this->rightRangeTokens, $stackPtr + 1);
$classNameEnd = $phpcsFile->findNext($this->rightRangeTokens, $classNameStart + 1, null, true);
$className = '';
for ($i = $classNameStart; $i < $classNameEnd; $i++) {
$className .= $tokens[$i]['content'];
}
$phpcsFile->addWarning(
$this->warningMessage,
$classNameStart,
$this->warningCode,
[$className]
);
}
}
}
28 changes: 28 additions & 0 deletions Magento/Sniffs/CodeAnalysis/EmptyBlockSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sniffs\CodeAnalysis;

use PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\EmptyStatementSniff;

/**
* Detects possible empty blocks.
*/
class EmptyBlockSniff extends EmptyStatementSniff
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks custom severity is not used. Do we want to have it set to more than 5?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The severity is declared in the ruleset.xml.

<rule ref="Magento.CodeAnalysis.EmptyBlock">
    <severity>8</severity>
    <type>warning</type>
</rule>

{
/**
* @inheritdoc
*/
public function register()
{
return array_merge(
parent::register(),
[
T_FUNCTION,
T_TRAIT
]
);
}
}
54 changes: 54 additions & 0 deletions Magento/Sniffs/Exceptions/DirectThrowSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sniffs\Exceptions;

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

/**
* Detects possible direct throws of Exceptions.
*/
class DirectThrowSniff implements Sniff
{
/**
* String representation of warning.
*/
// phpcs:ignore Generic.Files.LineLength.TooLong
protected $warningMessage = 'Direct throw of generic Exception is discouraged. Use context specific instead.';

/**
* Warning violation code.
*
* @var string
*/
protected $warningCode = 'FoundDirectThrow';

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

/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$endOfStatement = $phpcsFile->findEndOfStatement($stackPtr);
$posOfException = $phpcsFile->findNext(T_STRING, $stackPtr, $endOfStatement);
if ($tokens[$posOfException]['content'] === 'Exception') {
$phpcsFile->addWarning(
$this->warningMessage,
$stackPtr,
$this->warningCode,
$posOfException
);
}
}
}
64 changes: 64 additions & 0 deletions Magento/Sniffs/Exceptions/NamespaceSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sniffs\Exceptions;

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

/**
* Detects possible usage of exceptions without namespace declaration.
*/
class NamespaceSniff implements Sniff
{
/**
* String representation of error.
*
* @var string
*/
protected $errorMessage = 'Namespace for %s class is not specified.';

/**
* Error violation code.
*
* @var string
*/
protected $errorCode = 'NotFoundNamespace';

/**
* @inheritdoc
*/
public function register()
{
return [T_CATCH, T_THROW];
}

/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
if ($phpcsFile->findNext(T_NAMESPACE, 0) === false) {
return;
}

$tokens = $phpcsFile->getTokens();
$endOfStatement = $phpcsFile->findEndOfStatement($stackPtr);
$posOfExceptionClassName = $phpcsFile->findNext(T_STRING, $stackPtr, $endOfStatement);
$posOfNsSeparator = $phpcsFile->findNext(T_NS_SEPARATOR, $stackPtr, $posOfExceptionClassName);
if ($posOfNsSeparator === false && $posOfExceptionClassName !== false) {
$exceptionClassName = trim($tokens[$posOfExceptionClassName]['content']);
$posOfClassInUse = $phpcsFile->findNext(T_STRING, 0, $stackPtr, false, $exceptionClassName);
if ($posOfClassInUse === false || $tokens[$posOfClassInUse]['level'] != 0) {
$phpcsFile->addError(
$this->errorMessage,
$stackPtr,
$this->errorCode,
$exceptionClassName
);
}
}
}
}
115 changes: 115 additions & 0 deletions Magento/Sniffs/Legacy/MageEntitySniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sniffs\Legacy;

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

/**
* Detects typical Magento 1.x classes constructions.
*/
class MageEntitySniff implements Sniff
{
/**
* String representation of error.
*
* @var string
*/
protected $errorMessage = 'Possible Magento 2 design violation. Detected typical Magento 1.x construction "%s".';

/**
* Error violation code.
*
* @var string
*/
protected $errorCode = 'FoundLegacyEntity';

/**
* Legacy entity from Magento 1.
*
* @var string
*/
protected $legacyEntity = 'Mage';

/**
* Legacy prefixes from Magento 1.
*
* @var array
*/
protected $legacyPrefixes = [
'Mage_',
'Enterprise_'
];

/**
* @inheritdoc
*/
public function register()
{
return [
T_DOUBLE_COLON,
T_NEW
];
}

/**
* List of tokens for which we should find name before his position.
*
* @var array
*/
protected $nameBefore = [
T_DOUBLE_COLON
];

/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (in_array($tokens[$stackPtr]['code'], $this->nameBefore)) {
$oldPosition = $stackPtr;
$stackPtr = $phpcsFile->findPrevious(T_STRING, $stackPtr - 1, null, false, null, true);
if ($stackPtr === false) {
return;
}
$entityName = $tokens[$stackPtr]['content'];
$error = [$entityName . $tokens[$oldPosition]['content']];
} else {
$oldPosition = $stackPtr;
$stackPtr = $phpcsFile->findNext(T_STRING, $oldPosition + 1, null, false, null, true);
if ($stackPtr === false) {
return;
}
$entityName = $tokens[$stackPtr]['content'];
$error = [$tokens[$oldPosition]['content'] . ' ' . $entityName];
}
if ($entityName === $this->legacyEntity || $this->isPrefixLegacy($entityName)) {
$phpcsFile->addError(
$this->errorMessage,
$stackPtr,
$this->errorCode,
$error
);
}
}

/**
* Method checks if passed string contains legacy prefix from Magento 1.
*
* @param string $entityName
* @return bool
*/
private function isPrefixLegacy($entityName)
{
foreach ($this->legacyPrefixes as $entity) {
if (strpos($entityName, $entity) === 0) {
return true;
}
}
return false;
}
}
Loading