Skip to content

Commit 1c22ba7

Browse files
committed
MM-4941: [EQP][Sniffs Consolidation] Create new GitHub repo and move MEQP2 sniffs
- Decoupled MEQP2 sniffs from MEQP coding standard - Skipped false-positive and dynamic sniffs for now
1 parent 9612ff2 commit 1c22ba7

File tree

70 files changed

+6143
-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

+6143
-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,92 @@
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+
* Violation severity.
18+
*
19+
* @var int
20+
*/
21+
protected $severity = 8;
22+
23+
/**
24+
* String representation of warning.
25+
*
26+
* @var string
27+
*/
28+
protected $warningMessage = 'Direct object instantiation (object of %s) is discouraged in Magento 2.';
29+
30+
/**
31+
* Warning violation code.
32+
*
33+
* @var string
34+
*/
35+
protected $warningCode = 'FoundDirectInstantiation';
36+
37+
/**
38+
* List of tokens which declares left bound of current scope.
39+
*
40+
* @var array
41+
*/
42+
protected $leftRangeTokens = [
43+
T_WHITESPACE,
44+
T_THROW
45+
];
46+
47+
/**
48+
* List of tokens which declares right bound of current scope.
49+
*
50+
* @var array
51+
*/
52+
protected $rightRangeTokens = [
53+
T_STRING,
54+
T_SELF,
55+
T_STATIC,
56+
T_VARIABLE,
57+
T_NS_SEPARATOR
58+
];
59+
60+
/**
61+
* @inheritdoc
62+
*/
63+
public function register()
64+
{
65+
return [T_NEW];
66+
}
67+
68+
/**
69+
* @inheritdoc
70+
*/
71+
public function process(File $phpcsFile, $stackPtr)
72+
{
73+
$tokens = $phpcsFile->getTokens();
74+
$leftLimit = $phpcsFile->findPrevious($this->leftRangeTokens, $stackPtr - 1, null, true);
75+
$findThrow = $phpcsFile->findPrevious(T_THROW, $stackPtr - 1, $leftLimit);
76+
if (!$findThrow) {
77+
$classNameStart = $phpcsFile->findNext($this->rightRangeTokens, $stackPtr + 1);
78+
$classNameEnd = $phpcsFile->findNext($this->rightRangeTokens, $classNameStart + 1, null, true);
79+
$className = '';
80+
for ($i = $classNameStart; $i < $classNameEnd; $i++) {
81+
$className .= $tokens[$i]['content'];
82+
}
83+
$phpcsFile->addWarning(
84+
$this->warningMessage,
85+
$classNameStart,
86+
$this->warningCode,
87+
[$className],
88+
$this->severity
89+
);
90+
}
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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_CLASS,
24+
T_ABSTRACT,
25+
T_FUNCTION,
26+
T_INTERFACE,
27+
T_TRAIT
28+
]
29+
);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
* Violation severity.
18+
*
19+
* @var int
20+
*/
21+
protected $severity = 8;
22+
23+
/**
24+
* String representation of warning.
25+
*/
26+
// phpcs:ignore Generic.Files.LineLength.TooLong
27+
protected $warningMessage = 'Direct throw of generic Exception is discouraged. Use context specific instead.';
28+
29+
/**
30+
* Warning violation code.
31+
*
32+
* @var string
33+
*/
34+
protected $warningCode = 'FoundDirectThrow';
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function register()
40+
{
41+
return [T_THROW];
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function process(File $phpcsFile, $stackPtr)
48+
{
49+
$tokens = $phpcsFile->getTokens();
50+
$endOfStatement = $phpcsFile->findEndOfStatement($stackPtr);
51+
$posOfException = $phpcsFile->findNext(T_STRING, $stackPtr, $endOfStatement);
52+
if ($tokens[$posOfException]['content'] === 'Exception') {
53+
$phpcsFile->addWarning(
54+
$this->warningMessage,
55+
$stackPtr,
56+
$this->warningCode,
57+
$posOfException,
58+
$this->severity
59+
);
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
* Violation severity.
18+
*
19+
* @var int
20+
*/
21+
protected $severity = 10;
22+
23+
/**
24+
* String representation of error.
25+
*
26+
* @var string
27+
*/
28+
protected $errorMessage = 'Namespace for %s class is not specified.';
29+
30+
/**
31+
* Error violation code.
32+
*
33+
* @var string
34+
*/
35+
protected $errorCode = 'NotFoundNamespace';
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
public function register()
41+
{
42+
return [T_CATCH, T_THROW];
43+
}
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
public function process(File $phpcsFile, $stackPtr)
49+
{
50+
if ($phpcsFile->findNext(T_NAMESPACE, 0) === false) {
51+
return;
52+
}
53+
54+
$tokens = $phpcsFile->getTokens();
55+
$endOfStatement = $phpcsFile->findEndOfStatement($stackPtr);
56+
$posOfExceptionClassName = $phpcsFile->findNext(T_STRING, $stackPtr, $endOfStatement);
57+
$posOfNsSeparator = $phpcsFile->findNext(T_NS_SEPARATOR, $stackPtr, $posOfExceptionClassName);
58+
if ($posOfNsSeparator === false && $posOfExceptionClassName !== false) {
59+
$exceptionClassName = trim($tokens[$posOfExceptionClassName]['content']);
60+
$posOfClassInUse = $phpcsFile->findNext(T_STRING, 0, $stackPtr, false, $exceptionClassName);
61+
if ($posOfClassInUse === false || $tokens[$posOfClassInUse]['level'] != 0) {
62+
$phpcsFile->addError(
63+
$this->errorMessage,
64+
$stackPtr,
65+
$this->errorCode,
66+
$exceptionClassName,
67+
$this->severity
68+
);
69+
}
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)