-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathTableNameSniff.php
236 lines (215 loc) · 6.71 KB
/
TableNameSniff.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);
namespace Magento2\Sniffs\Legacy;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Coverage of obsolete table names usage
*/
class TableNameSniff implements Sniff
{
/**
* Methods which receive table names as parameters, with the argument position in which table names are passed
*
* @var array
*/
private $argPositionInMethods = [
'getTableName' => [0],
'_setMainTable' => [0],
'setMainTable' => [0],
'getTable' => [0],
'setTable' => [0],
'getTableRow' => [0],
'deleteTableRow' => [0],
'updateTableRow' => [0],
'updateTable' => [0],
'tableExists' => [0],
'joinField' => [1],
'joinTable' => [0],
'getFkName' => [0, 2],
'getIdxName' => [0],
'addVirtualGridColumn' => [1],
];
/**
* String representation of error.
*
* @var string
*/
private const ERROR_MESSAGE = 'Legacy table names with slash must be fixed to direct table names. Found: %s';
/**
* Error violation code.
*
* @var string
*/
private const ERROR_CODE = 'FoundLegacyTableName';
/**
* @inheritdoc
*/
public function register()
{
return [
T_OBJECT_OPERATOR,
T_VARIABLE,
T_DOUBLE_ARROW
];
}
/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['code'] === T_OBJECT_OPERATOR) {
$this->checkOccurrencesInMethods($phpcsFile, $stackPtr, $tokens);
} elseif ($tokens[$stackPtr]['code'] === T_DOUBLE_ARROW) {
$this->checkOccurrencesInArray($phpcsFile, $stackPtr, $tokens);
} else {
$this->checkOccurrencesInProperty($phpcsFile, $stackPtr, $tokens);
}
}
/**
* Check if passed file is a resource but not a collection
*
* @param string $filePath
* @return bool
*/
private function isResourceButNotCollection(string $filePath): bool
{
$filePath = str_replace('\\', '/', $filePath);
$parts = explode('/', $filePath);
return array_search('Resource', $parts) !== false && array_search('Collection.php', $parts) === false;
}
/**
* Check references to table names in methods
*
* @param File $phpcsFile
* @param int $stackPtr
* @param array $tokens
*/
private function checkOccurrencesInMethods(File $phpcsFile, int $stackPtr, array $tokens): void
{
$methodNamePos = $phpcsFile->findNext(T_STRING, $stackPtr + 1);
$methodName = $tokens[$methodNamePos]['content'];
if (array_key_exists($methodName, $this->argPositionInMethods) === false) {
return;
}
$firstArgumentPos = $phpcsFile->findNext([T_CONSTANT_ENCAPSED_STRING, T_VARIABLE], $methodNamePos + 1);
foreach ($this->argPositionInMethods[$methodName] as $argPosition) {
$paramPos = $firstArgumentPos;
for ($i = 0; $i < $argPosition; $i++) {
$paramPos = $phpcsFile->findNext(
[T_CONSTANT_ENCAPSED_STRING, T_VARIABLE],
$paramPos + 1,
$phpcsFile->findNext(T_CLOSE_PARENTHESIS, $paramPos + 1)
);
}
if (strpos($tokens[$paramPos]['content'], '/') !== false) {
$phpcsFile->addError(
sprintf(
self::ERROR_MESSAGE,
$tokens[$paramPos]['content'],
),
$paramPos,
self::ERROR_CODE
);
}
}
if ($this->isResourceButNotCollection($phpcsFile->getFilename())) {
if ($tokens[$stackPtr]['content'] !== '_init') {
return;
}
$paramPos = $phpcsFile->findNext(T_PARAM_NAME, $stackPtr + 1);
if (strpos($tokens[$paramPos]['content'], '/') !== false) {
$phpcsFile->addError(
sprintf(
self::ERROR_MESSAGE,
$tokens[$paramPos]['content'],
),
$paramPos,
self::ERROR_CODE
);
}
}
}
/**
* Check references to table names in the $_aggregationTable property
*
* @param File $phpcsFile
* @param int $stackPtr
* @param array $tokens
*/
private function checkOccurrencesInProperty(File $phpcsFile, int $stackPtr, array $tokens): void
{
if ($methodName = $tokens[$stackPtr]['content'] !== '$_aggregationTable') {
return;
}
$tableNamePos = $phpcsFile->findNext(
T_CONSTANT_ENCAPSED_STRING,
$stackPtr + 1,
$phpcsFile->findEndOfStatement($stackPtr + 1)
);
if (strpos($tokens[$tableNamePos]['content'], '/') !== false) {
$phpcsFile->addError(
sprintf(
self::ERROR_MESSAGE,
$tokens[$tableNamePos]['content'],
),
$tableNamePos,
self::ERROR_CODE
);
}
}
/**
* Check references to table names in arrays
*
* @param File $phpcsFile
* @param int $stackPtr
* @param array $tokens
*/
private function checkOccurrencesInArray(File $phpcsFile, int $stackPtr, array $tokens): void
{
$aliasPos = $phpcsFile->findPrevious(
T_WHITESPACE,
$stackPtr - 1,
null,
true,
);
$alias = trim($tokens[$aliasPos]['content'], '\'"');
if ($this->endsWith($alias, '_table') === false) {
return;
}
$tableNamePos = $phpcsFile->findNext(
T_CONSTANT_ENCAPSED_STRING,
$aliasPos + 1
);
if (strpos($tokens[$tableNamePos]['content'], '/') !== false) {
$phpcsFile->addError(
sprintf(
self::ERROR_MESSAGE,
$tokens[$tableNamePos]['content'],
),
$tableNamePos,
self::ERROR_CODE
);
}
}
/**
* Checks if $haystack ends with $needle
*
* @param string $haystack
* @param string $needle
* @return bool
*/
private function endsWith(string $haystack, string $needle): bool
{
$length = strlen($needle);
if ($length === 0) {
return true;
}
return substr($haystack, -$length) === $needle;
}
}