-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathLiteralNamespacesSniff.php
67 lines (59 loc) · 1.72 KB
/
LiteralNamespacesSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Sniffs\PHP;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Custom phpcs sniff to detect usages of literal class and interface names.
*/
class LiteralNamespacesSniff implements Sniff
{
/**
* @var string
*/
private $literalNamespacePattern = '/^[\\\]{0,2}[A-Z][A-Za-z]+([\\\]{1,2}[A-Z][A-Za-z]+){2,}(?!\\\+)$/';
/**
* @var array
*/
private $classNames = [];
/**
* @inheritdoc
*/
public function register()
{
return [
T_CONSTANT_ENCAPSED_STRING,
T_DOUBLE_QUOTED_STRING,
];
}
/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($phpcsFile->findPrevious(T_STRING_CONCAT, $stackPtr, $stackPtr - 3) ||
$phpcsFile->findNext(T_STRING_CONCAT, $stackPtr, $stackPtr + 3)
) {
return;
}
$content = trim($tokens[$stackPtr]['content'], "\"'");
// replace double slashes from class name for avoiding problems with class autoload
if (strpos($content, '\\') !== false) {
$content = preg_replace('|\\\{2,}|', '\\', $content);
}
if (preg_match($this->literalNamespacePattern, $content) === 1) {
$fix = $phpcsFile->addFixableWarning(
"Use ::class notation instead.",
$stackPtr,
'LiteralClassUsage'
);
if ($fix) {
$phpcsFile->fixer->replaceToken($stackPtr, '\\' . trim($content, '\\') . '::class');
}
}
}
}