-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathAutogeneratedClassNotInConstructorSniff.php
200 lines (180 loc) · 5.93 KB
/
AutogeneratedClassNotInConstructorSniff.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);
namespace Magento2\Sniffs\PHP;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Detects possible usage of 'var' language construction.
*/
class AutogeneratedClassNotInConstructorSniff implements Sniff
{
private const ERROR_CODE = 'AUTOGENERATED_CLASS_NOT_IN_CONSTRUCTOR';
/**
* @var array
*/
private $constructorParameters = [];
/**
* @var array
*/
private $uses = [];
/**
* @inheritdoc
*/
public function register()
{
return [T_FUNCTION, T_DOUBLE_COLON, T_USE];
}
/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
if ($phpcsFile->getTokens()[$stackPtr]['type'] === 'T_USE') {
$this->registerUses($phpcsFile, $stackPtr);
}
if ($phpcsFile->getTokens()[$stackPtr]['type'] === 'T_FUNCTION') {
$this->registerConstructorParameters($phpcsFile, $stackPtr);
}
if ($phpcsFile->getTokens()[$stackPtr]['type'] === 'T_DOUBLE_COLON') {
if (!$this->isObjectManagerGetInstance($phpcsFile, $stackPtr)) {
return;
}
$statementStart = $phpcsFile->findStartOfStatement($stackPtr);
$statementEnd = $phpcsFile->findEndOfStatement($stackPtr);
$equalsPtr = $phpcsFile->findNext(T_EQUAL, $statementStart, $statementEnd);
if (!$equalsPtr) {
return;
}
$variableInParameters = false;
if ($variable = $phpcsFile->findNext(T_VARIABLE, $equalsPtr, $statementEnd)) {
$variableName = $phpcsFile->getTokens()[$variable]['content'];
if ($variableName === '$this') {
$variableName = $this->getNext($phpcsFile, $variable, $statementEnd, T_STRING)['content'];
}
foreach ($this->constructorParameters as $parameter) {
$parameterName = $parameter['name'];
if ($this->variableName($parameterName) === $this->variableName($variableName)) {
$variableInParameters = true;
}
}
}
if (!$variableInParameters) {
$next = $stackPtr;
while ($next = $phpcsFile->findNext(T_DOUBLE_COLON, $next + 1, $statementEnd)) {
if ($this->getNext($phpcsFile, $next, $statementEnd, T_STRING)['content'] === 'class') {
$className = $this->getPrevious($phpcsFile, $next, T_STRING)['content'];
}
}
$className = $this->getClassNamespace($className);
$phpcsFile->addWarning(
sprintf("Class %s needs to be requested in constructor, " .
"otherwise compiler will not be able to find and generate these classes", $className),
$stackPtr,
self::ERROR_CODE
);
}
}
}
/**
* Check if it is a ObjectManager::getInstance
*
* @param File $phpcsFile
* @param int $stackPtr
* @return bool
*/
private function isObjectManagerGetInstance(File $phpcsFile, int $stackPtr): bool
{
$prev = $phpcsFile->findPrevious(T_STRING, $stackPtr - 1);
$next = $phpcsFile->findNext(T_STRING, $stackPtr + 1);
if ($prev &&
$next &&
$phpcsFile->getTokens()[$prev]['content'] === 'ObjectManager' &&
$phpcsFile->getTokens()[$next]['content'] === 'getInstance') {
return true;
}
return false;
}
/**
* Get the complete class namespace from the use's
*
* @param string $className
* @return string
*/
private function getClassNamespace(string $className): string
{
foreach ($this->uses as $use) {
if (end($use) === $className) {
$className = implode('/', $use);
}
}
return $className;
}
/**
* Register php uses
*
* @param File $phpcsFile
* @param int $stackPtr
*/
private function registerUses(File $phpcsFile, int $stackPtr): void
{
$useEnd = $phpcsFile->findEndOfStatement($stackPtr);
$use = [];
$usePosition = $stackPtr;
while ($usePosition = $phpcsFile->findNext(T_STRING, $usePosition + 1, $useEnd)) {
$use[] = $phpcsFile->getTokens()[$usePosition]['content'];
}
$this->uses [] = $use;
}
/**
* Register php constructor parameters
*
* @param File $phpcsFile
* @param int $stackPtr
*/
private function registerConstructorParameters(File $phpcsFile, int $stackPtr): void
{
$functionName = $phpcsFile->getDeclarationName($stackPtr);
if ($functionName == '__construct') {
$this->constructorParameters = $phpcsFile->getMethodParameters($stackPtr);
}
}
/**
* Get next token
*
* @param File $phpcsFile
* @param int $from
* @param int $to
* @param int|string|array $types
* @return mixed
*/
private function getNext(File $phpcsFile, int $from, int $to, $types)
{
return $phpcsFile->getTokens()[$phpcsFile->findNext($types, $from + 1, $to)];
}
/**
* Get previous token
*
* @param File $phpcsFile
* @param int $from
* @param int|string|array $types
* @return mixed
*/
private function getPrevious(File $phpcsFile, int $from, $types)
{
return $phpcsFile->getTokens()[$phpcsFile->findPrevious($types, $from - 1)];
}
/**
* Get name of the variable without $
*
* @param string $parameterName
* @return string
*/
protected function variableName(string $parameterName): string
{
return str_replace('$', '', $parameterName);
}
}