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 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
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
92 changes: 92 additions & 0 deletions Magento/Sniffs/Classes/ObjectInstantiationSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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
{
/**
* Violation severity.
*
* @var int
*/
protected $severity = 8;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we control severity just by ruleset.xml?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can, but I don't know whether should we.
Anyway, severity in ruleset will override the severity declared in the sniff.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would remove all severity declarations from sniffs for unification. We don't use it in core and here it is also overridden.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved severities to ruleset.xml


/**
* 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],
$this->severity
);
}
}
}
31 changes: 31 additions & 0 deletions Magento/Sniffs/CodeAnalysis/EmptyBlockSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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_CLASS,
T_ABSTRACT,
T_FUNCTION,
T_INTERFACE,
T_TRAIT
]
);
}
}
62 changes: 62 additions & 0 deletions Magento/Sniffs/Exceptions/DirectThrowSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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
{
/**
* Violation severity.
*
* @var int
*/
protected $severity = 8;

/**
* 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,
$this->severity
);
}
}
}
72 changes: 72 additions & 0 deletions Magento/Sniffs/Exceptions/NamespaceSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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
{
/**
* Violation severity.
*
* @var int
*/
protected $severity = 10;

/**
* 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,
$this->severity
);
}
}
}
}
Loading