Skip to content

Commit 7630545

Browse files
authored
Merge pull request #1 from magento/MM-4941-move-MEQP2-sniffs
MM-4941: [EQP][Sniffs Consolidation] Create new GitHub repo and move MEQP2 sniffs
2 parents 9612ff2 + bb77a39 commit 7630545

File tree

70 files changed

+5996
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+5996
-1
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/*
2+
/bin/*

.travis.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: php
2+
php:
3+
- 5.5
4+
- 5.6
5+
- 7.0
6+
- 7.1
7+
install: composer install --no-interaction --prefer-source
8+
script:
9+
- bin/phpunit
10+
- bin/phpcs --standard=PSR2 Magento/ --extensions=php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sniffs\Classes;
7+
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHP_CodeSniffer\Files\File;
10+
11+
/**
12+
* Detects direct object instantiation via 'new' keyword.
13+
*/
14+
class ObjectInstantiationSniff implements Sniff
15+
{
16+
/**
17+
* String representation of warning.
18+
*
19+
* @var string
20+
*/
21+
protected $warningMessage = 'Direct object instantiation (object of %s) is discouraged in Magento 2.';
22+
23+
/**
24+
* Warning violation code.
25+
*
26+
* @var string
27+
*/
28+
protected $warningCode = 'FoundDirectInstantiation';
29+
30+
/**
31+
* List of tokens which declares left bound of current scope.
32+
*
33+
* @var array
34+
*/
35+
protected $leftRangeTokens = [
36+
T_WHITESPACE,
37+
T_THROW
38+
];
39+
40+
/**
41+
* List of tokens which declares right bound of current scope.
42+
*
43+
* @var array
44+
*/
45+
protected $rightRangeTokens = [
46+
T_STRING,
47+
T_SELF,
48+
T_STATIC,
49+
T_VARIABLE,
50+
T_NS_SEPARATOR
51+
];
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
public function register()
57+
{
58+
return [T_NEW];
59+
}
60+
61+
/**
62+
* @inheritdoc
63+
*/
64+
public function process(File $phpcsFile, $stackPtr)
65+
{
66+
$tokens = $phpcsFile->getTokens();
67+
$leftLimit = $phpcsFile->findPrevious($this->leftRangeTokens, $stackPtr - 1, null, true);
68+
$findThrow = $phpcsFile->findPrevious(T_THROW, $stackPtr - 1, $leftLimit);
69+
if (!$findThrow) {
70+
$classNameStart = $phpcsFile->findNext($this->rightRangeTokens, $stackPtr + 1);
71+
$classNameEnd = $phpcsFile->findNext($this->rightRangeTokens, $classNameStart + 1, null, true);
72+
$className = '';
73+
for ($i = $classNameStart; $i < $classNameEnd; $i++) {
74+
$className .= $tokens[$i]['content'];
75+
}
76+
$phpcsFile->addWarning(
77+
$this->warningMessage,
78+
$classNameStart,
79+
$this->warningCode,
80+
[$className]
81+
);
82+
}
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sniffs\CodeAnalysis;
7+
8+
use PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\EmptyStatementSniff;
9+
10+
/**
11+
* Detects possible empty blocks.
12+
*/
13+
class EmptyBlockSniff extends EmptyStatementSniff
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function register()
19+
{
20+
return array_merge(
21+
parent::register(),
22+
[
23+
T_FUNCTION,
24+
T_TRAIT
25+
]
26+
);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sniffs\Exceptions;
7+
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHP_CodeSniffer\Files\File;
10+
11+
/**
12+
* Detects possible direct throws of Exceptions.
13+
*/
14+
class DirectThrowSniff implements Sniff
15+
{
16+
/**
17+
* String representation of warning.
18+
*/
19+
// phpcs:ignore Generic.Files.LineLength.TooLong
20+
protected $warningMessage = 'Direct throw of generic Exception is discouraged. Use context specific instead.';
21+
22+
/**
23+
* Warning violation code.
24+
*
25+
* @var string
26+
*/
27+
protected $warningCode = 'FoundDirectThrow';
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
public function register()
33+
{
34+
return [T_THROW];
35+
}
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
public function process(File $phpcsFile, $stackPtr)
41+
{
42+
$tokens = $phpcsFile->getTokens();
43+
$endOfStatement = $phpcsFile->findEndOfStatement($stackPtr);
44+
$posOfException = $phpcsFile->findNext(T_STRING, $stackPtr, $endOfStatement);
45+
if ($tokens[$posOfException]['content'] === 'Exception') {
46+
$phpcsFile->addWarning(
47+
$this->warningMessage,
48+
$stackPtr,
49+
$this->warningCode,
50+
$posOfException
51+
);
52+
}
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sniffs\Exceptions;
7+
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHP_CodeSniffer\Files\File;
10+
11+
/**
12+
* Detects possible usage of exceptions without namespace declaration.
13+
*/
14+
class NamespaceSniff implements Sniff
15+
{
16+
/**
17+
* String representation of error.
18+
*
19+
* @var string
20+
*/
21+
protected $errorMessage = 'Namespace for %s class is not specified.';
22+
23+
/**
24+
* Error violation code.
25+
*
26+
* @var string
27+
*/
28+
protected $errorCode = 'NotFoundNamespace';
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function register()
34+
{
35+
return [T_CATCH, T_THROW];
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function process(File $phpcsFile, $stackPtr)
42+
{
43+
if ($phpcsFile->findNext(T_NAMESPACE, 0) === false) {
44+
return;
45+
}
46+
47+
$tokens = $phpcsFile->getTokens();
48+
$endOfStatement = $phpcsFile->findEndOfStatement($stackPtr);
49+
$posOfExceptionClassName = $phpcsFile->findNext(T_STRING, $stackPtr, $endOfStatement);
50+
$posOfNsSeparator = $phpcsFile->findNext(T_NS_SEPARATOR, $stackPtr, $posOfExceptionClassName);
51+
if ($posOfNsSeparator === false && $posOfExceptionClassName !== false) {
52+
$exceptionClassName = trim($tokens[$posOfExceptionClassName]['content']);
53+
$posOfClassInUse = $phpcsFile->findNext(T_STRING, 0, $stackPtr, false, $exceptionClassName);
54+
if ($posOfClassInUse === false || $tokens[$posOfClassInUse]['level'] != 0) {
55+
$phpcsFile->addError(
56+
$this->errorMessage,
57+
$stackPtr,
58+
$this->errorCode,
59+
$exceptionClassName
60+
);
61+
}
62+
}
63+
}
64+
}
+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sniffs\Legacy;
7+
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHP_CodeSniffer\Files\File;
10+
11+
/**
12+
* Detects typical Magento 1.x classes constructions.
13+
*/
14+
class MageEntitySniff implements Sniff
15+
{
16+
/**
17+
* String representation of error.
18+
*
19+
* @var string
20+
*/
21+
protected $errorMessage = 'Possible Magento 2 design violation. Detected typical Magento 1.x construction "%s".';
22+
23+
/**
24+
* Error violation code.
25+
*
26+
* @var string
27+
*/
28+
protected $errorCode = 'FoundLegacyEntity';
29+
30+
/**
31+
* Legacy entity from Magento 1.
32+
*
33+
* @var string
34+
*/
35+
protected $legacyEntity = 'Mage';
36+
37+
/**
38+
* Legacy prefixes from Magento 1.
39+
*
40+
* @var array
41+
*/
42+
protected $legacyPrefixes = [
43+
'Mage_',
44+
'Enterprise_'
45+
];
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
public function register()
51+
{
52+
return [
53+
T_DOUBLE_COLON,
54+
T_NEW
55+
];
56+
}
57+
58+
/**
59+
* List of tokens for which we should find name before his position.
60+
*
61+
* @var array
62+
*/
63+
protected $nameBefore = [
64+
T_DOUBLE_COLON
65+
];
66+
67+
/**
68+
* @inheritdoc
69+
*/
70+
public function process(File $phpcsFile, $stackPtr)
71+
{
72+
$tokens = $phpcsFile->getTokens();
73+
if (in_array($tokens[$stackPtr]['code'], $this->nameBefore)) {
74+
$oldPosition = $stackPtr;
75+
$stackPtr = $phpcsFile->findPrevious(T_STRING, $stackPtr - 1, null, false, null, true);
76+
if ($stackPtr === false) {
77+
return;
78+
}
79+
$entityName = $tokens[$stackPtr]['content'];
80+
$error = [$entityName . $tokens[$oldPosition]['content']];
81+
} else {
82+
$oldPosition = $stackPtr;
83+
$stackPtr = $phpcsFile->findNext(T_STRING, $oldPosition + 1, null, false, null, true);
84+
if ($stackPtr === false) {
85+
return;
86+
}
87+
$entityName = $tokens[$stackPtr]['content'];
88+
$error = [$tokens[$oldPosition]['content'] . ' ' . $entityName];
89+
}
90+
if ($entityName === $this->legacyEntity || $this->isPrefixLegacy($entityName)) {
91+
$phpcsFile->addError(
92+
$this->errorMessage,
93+
$stackPtr,
94+
$this->errorCode,
95+
$error
96+
);
97+
}
98+
}
99+
100+
/**
101+
* Method checks if passed string contains legacy prefix from Magento 1.
102+
*
103+
* @param string $entityName
104+
* @return bool
105+
*/
106+
private function isPrefixLegacy($entityName)
107+
{
108+
foreach ($this->legacyPrefixes as $entity) {
109+
if (strpos($entityName, $entity) === 0) {
110+
return true;
111+
}
112+
}
113+
return false;
114+
}
115+
}

0 commit comments

Comments
 (0)