-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathDiscouragedDependenciesSniff.php
276 lines (250 loc) · 7.87 KB
/
DiscouragedDependenciesSniff.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Copyright 2019 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento2\Sniffs\Classes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Detects explicit request of proxies and interceptors in constructors
*
* Search is in variable names and namespaces, including indirect namespaces from use statements
*/
class DiscouragedDependenciesSniff implements Sniff
{
private const CONSTRUCT_METHOD_NAME = '__construct';
/**
* String representation of warning.
*
* @var string
*/
protected $warningMessage = 'Proxies and interceptors MUST never be explicitly requested in constructors.';
/**
* Warning violation code.
*
* @var string
*/
protected $warningCode = 'ConstructorProxyInterceptor';
/**
* @var string[]
*/
private $aliases = [];
/**
* @var null|string
*/
private $currentFile = null;
/**
* @var string[]
*/
public $incorrectClassNames = ['proxy','interceptor'];
/**
* @inheritDoc
*/
public function register()
{
return [
T_USE,
T_FUNCTION
];
}
/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
// Clear down aliases when file under test changes
$currentFileName = $phpcsFile->getFilename();
if ($this->currentFile != $currentFileName) {
// Clear aliases
$this->aliases = [];
$this->currentFile = $currentFileName;
}
// Match use statements and constructor (latter matches T_FUNCTION to find constructors)
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['code'] == T_USE) {
$this->processUse($phpcsFile, $stackPtr, $tokens);
} elseif ($tokens[$stackPtr]['code'] == T_FUNCTION) {
$this->processFunction($phpcsFile, $stackPtr, $tokens);
}
}
/**
* Store plugin/proxy class names for use in matching constructor
*
* @param File $phpcsFile
* @param int $stackPtr
* @param array $tokens
*/
private function processUse(
File $phpcsFile,
$stackPtr,
$tokens
) {
// Find end of use statement and position of AS alias if exists
$endPos = $phpcsFile->findNext(T_SEMICOLON, $stackPtr);
$asPos = $phpcsFile->findNext(T_AS, $stackPtr, $endPos);
// Find whether this use statement includes any of the warning words
$includesWarnWord =
$this->includesWarnWordsInSTRINGs(
$phpcsFile,
$stackPtr,
min($asPos, $endPos),
$tokens,
$lastWord
);
if (! $includesWarnWord) {
return;
}
// If there is an alias then store this explicit alias for matching in constructor
if ($asPos) {
$aliasNamePos = $asPos + 2;
$this->aliases[] = strtolower($tokens[$aliasNamePos]['content']);
}
// Always store last word as alias for checking in constructor
$this->aliases[] = $lastWord;
}
/**
* If constructor, check for proxy/plugin names and warn
*
* @param File $phpcsFile
* @param int $stackPtr
* @param array $tokens
*/
private function processFunction(
File $phpcsFile,
$stackPtr,
$tokens
) {
// Find start and end of constructor signature based on brackets
if (! $this->getConstructorPosition($phpcsFile, $stackPtr, $tokens, $openParenth, $closeParenth)) {
return;
}
$positionInConstrSig = $openParenth;
$lastName = null;
do {
// Find next part of namespace (string) or variable name
$positionInConstrSig = $phpcsFile->findNext(
[T_STRING, T_VARIABLE],
$positionInConstrSig + 1,
$closeParenth
);
$currentTokenIsString = $tokens[$positionInConstrSig]['code'] == T_STRING;
if ($currentTokenIsString) {
// Remember string in case this is last before variable
$lastName = strtolower($tokens[$positionInConstrSig]['content']);
} else {
// If this is a variable, check last word for matches as was end of classname/alias
if ($lastName !== null) {
$namesToWarn = $this->mergedNamesToWarn(true);
if ($this->containsWord($namesToWarn, $lastName)) {
$phpcsFile->addError(
$this->warningMessage,
$positionInConstrSig,
$this->warningCode,
[$lastName]
);
}
$lastName = null;
}
}
} while ($positionInConstrSig !== false && $positionInConstrSig < $closeParenth);
}
/**
* Sets start and end of constructor signature or returns false
*
* @param File $phpcsFile
* @param int $stackPtr
* @param array $tokens
* @param int $openParenth
* @param int $closeParenth
*
* @return bool Whether a constructor
*/
private function getConstructorPosition(
File $phpcsFile,
$stackPtr,
array $tokens,
&$openParenth,
&$closeParenth
) {
$methodNamePos = $phpcsFile->findNext(T_STRING, $stackPtr - 1);
if ($methodNamePos === false) {
return false;
}
// There is a method name
if ($tokens[$methodNamePos]['content'] != self::CONSTRUCT_METHOD_NAME) {
return false;
}
// KNOWN: There is a constructor, so get position
$openParenth = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $methodNamePos);
$closeParenth = $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $openParenth);
if ($openParenth === false || $closeParenth === false) {
return false;
}
return true;
}
/**
* Whether $name exactly matches any of $haystacks
*
* @param array $haystacks
* @param string $name
*
* @return bool
*/
private function containsWord($haystacks, $name)
{
return in_array($name, $haystacks);
}
/**
* Whether warn words are included in STRING tokens in the given range
*
* Populates $lastWord in set to get usable name from namespace
*
* @param File $phpcsFile
* @param int $startPosition
* @param int $endPosition
* @param array $tokens
* @param string|null $lastWord
*
* @return bool
*/
private function includesWarnWordsInSTRINGs(
File $phpcsFile,
$startPosition,
$endPosition,
$tokens,
&$lastWord
) {
$includesWarnWord = false;
$currentPosition = $startPosition;
do {
$currentPosition = $phpcsFile->findNext(T_STRING, $currentPosition + 1, $endPosition);
if ($currentPosition !== false) {
$word = strtolower($tokens[$currentPosition]['content']);
if ($this->containsWord($this->mergedNamesToWarn(false), $word)) {
$includesWarnWord = true;
}
$lastWord = $word;
}
} while ($currentPosition !== false && $currentPosition < $endPosition);
return $includesWarnWord;
}
/**
* Get array of names that if matched should raise warning.
*
* Includes aliases if required
*
* @param bool $includeAliases
*
* @return array
*/
private function mergedNamesToWarn($includeAliases = false)
{
$namesToWarn = $this->incorrectClassNames;
if ($includeAliases) {
$namesToWarn = array_merge($namesToWarn, $this->aliases);
}
return $namesToWarn;
}
}