-
Notifications
You must be signed in to change notification settings - Fork 160
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
1c22ba7
MM-4941: [EQP][Sniffs Consolidation] Create new GitHub repo and move …
lenaorobei 8b81307
MM-4941: [EQP][Sniffs Consolidation] Create new GitHub repo and move …
lenaorobei bb77a39
MM-4941: [EQP][Sniffs Consolidation] Create new GitHub repo and move …
lenaorobei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor/* | ||
/bin/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function register() | ||
{ | ||
return array_merge( | ||
parent::register(), | ||
[ | ||
T_FUNCTION, | ||
T_TRAIT | ||
] | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.