-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathAutogeneratedClassNotInConstructorSniff.php
226 lines (204 loc) · 6.69 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?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 bad usages of ObjectManager in constructor.
*/
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->registerUse($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;
}
if (!$this->isVariableInConstructorParameters($phpcsFile, $equalsPtr, $statementEnd)) {
$className = $this->obtainClassToGetOrCreate($phpcsFile, $stackPtr, $statementEnd);
$phpcsFile->addError(
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);
return $prev &&
$next &&
$phpcsFile->getTokens()[$prev]['content'] === 'ObjectManager' &&
$phpcsFile->getTokens()[$next]['content'] === 'getInstance';
}
/**
* Get the complete class namespace from the use's
*
* @param string $className
* @return string
*/
private function getClassNamespace(string $className): string
{
foreach ($this->uses as $key => $use) {
if ($key === $className) {
return $use;
}
}
return $className;
}
/**
* Register php uses
*
* @param File $phpcsFile
* @param int $stackPtr
*/
private function registerUse(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'];
}
$key = end($use);
if ($phpcsFile->findNext(T_AS, $stackPtr, $useEnd)) {
$this->uses[$key] = implode("\\", array_slice($use, 0, count($use) - 1));
} else {
$this->uses[$key] = implode("\\", $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);
}
/**
* Checks if a variable is present in the constructor parameters
*
* @param File $phpcsFile
* @param int $equalsPtr
* @param int $statementEnd
* @return bool
*/
private function isVariableInConstructorParameters(File $phpcsFile, int $equalsPtr, int $statementEnd): bool
{
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)) {
return true;
}
}
}
return false;
}
/**
* Obtain the class inside ObjectManager::getInstance()->get|create()
*
* @param File $phpcsFile
* @param int $next
* @param int $statementEnd
* @return string
*/
private function obtainClassToGetOrCreate(File $phpcsFile, int $next, int $statementEnd): string
{
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'];
}
}
return $this->getClassNamespace($className);
}
}