-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
PassedParameters.php
349 lines (314 loc) · 14.5 KB
/
PassedParameters.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2020 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/
namespace PHPCSUtils\Utils;
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Utils\Arrays;
use PHPCSUtils\Utils\GetTokensAsString;
/**
* Utility functions to retrieve information about parameters passed to function calls,
* array declarations, isset and unset constructs.
*
* @since 1.0.0
*/
class PassedParameters
{
/**
* The token types these methods can handle.
*
* @since 1.0.0
*
* @var array <int|string> => <irrelevant>
*/
private static $allowedConstructs = [
\T_STRING => true,
\T_VARIABLE => true,
\T_SELF => true,
\T_STATIC => true,
\T_ARRAY => true,
\T_OPEN_SHORT_ARRAY => true,
\T_ISSET => true,
\T_UNSET => true,
// BC for various short array tokenizer issues. See the Arrays class for more details.
\T_OPEN_SQUARE_BRACKET => true,
];
/**
* Tokens which are considered stop point, either because they are the end
* of the parameter (comma) or because we need to skip over them.
*
* @since 1.0.0
*
* @var array <int|string> => <int|string>
*/
private static $callParsingStopPoints = [
\T_COMMA => \T_COMMA,
\T_OPEN_SHORT_ARRAY => \T_OPEN_SHORT_ARRAY,
\T_OPEN_SQUARE_BRACKET => \T_OPEN_SQUARE_BRACKET,
\T_OPEN_PARENTHESIS => \T_OPEN_PARENTHESIS,
\T_DOC_COMMENT_OPEN_TAG => \T_DOC_COMMENT_OPEN_TAG,
];
/**
* Checks if any parameters have been passed.
*
* - If passed a `T_STRING`, `T_NAME_FULLY_QUALIFIED`, `T_NAME_RELATIVE`, `T_NAME_QUALIFIED`
* or `T_VARIABLE` stack pointer, it will treat it as a function call.
* If a `T_STRING` or `T_VARIABLE` which is *not* a function call is passed, the behaviour is
* undetermined.
* - If passed a `T_SELF` or `T_STATIC` stack pointer, it will accept it as a
* function call when used like `new self()`.
* - If passed a `T_ARRAY` or `T_OPEN_SHORT_ARRAY` stack pointer, it will detect
* whether the array has values or is empty.
* - If passed a `T_ISSET` or `T_UNSET` stack pointer, it will detect whether those
* language constructs have "parameters".
*
* @since 1.0.0
* @since 1.0.0-alpha4 Added support for PHP 8.0 identifier name tokenization.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position of the `T_STRING`, PHP 8.0 identifier
* name token, `T_VARIABLE`, `T_ARRAY`, `T_OPEN_SHORT_ARRAY`,
* `T_ISSET`, or `T_UNSET` token.
*
* @return bool
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the token passed is not one of the
* accepted types or doesn't exist.
*/
public static function hasParameters(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]) === false
|| (isset(self::$allowedConstructs[$tokens[$stackPtr]['code']]) === false
// Allow for the PHP 8.0 identifier name tokens.
&& isset(Collections::nameTokens()[$tokens[$stackPtr]['code']]) === false)
) {
throw new RuntimeException(
'The hasParameters() method expects a function call, array, isset or unset token to be passed.'
);
}
if ($tokens[$stackPtr]['code'] === \T_SELF || $tokens[$stackPtr]['code'] === \T_STATIC) {
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if ($tokens[$prev]['code'] !== \T_NEW) {
throw new RuntimeException(
'The hasParameters() method expects a function call, array, isset or unset token to be passed.'
);
}
}
if (($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY
|| $tokens[$stackPtr]['code'] === \T_OPEN_SQUARE_BRACKET)
&& Arrays::isShortArray($phpcsFile, $stackPtr) === false
) {
throw new RuntimeException(
'The hasParameters() method expects a function call, array, isset or unset token to be passed.'
);
}
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($next === false) {
return false;
}
// Deal with short array syntax.
if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY
|| $tokens[$stackPtr]['code'] === \T_OPEN_SQUARE_BRACKET
) {
if ($next === $tokens[$stackPtr]['bracket_closer']) {
// No parameters.
return false;
}
return true;
}
// Deal with function calls, long arrays, isset and unset.
// Next non-empty token should be the open parenthesis.
if ($tokens[$next]['code'] !== \T_OPEN_PARENTHESIS) {
return false;
}
if (isset($tokens[$next]['parenthesis_closer']) === false) {
return false;
}
$closeParenthesis = $tokens[$next]['parenthesis_closer'];
$nextNextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), ($closeParenthesis + 1), true);
if ($nextNextNonEmpty === $closeParenthesis) {
// No parameters.
return false;
}
return true;
}
/**
* Get information on all parameters passed.
*
* See {@see PassedParameters::hasParameters()} for information on the supported constructs.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position of the `T_STRING`, PHP 8.0 identifier
* name token, `T_VARIABLE`, `T_ARRAY`, `T_OPEN_SHORT_ARRAY`,
* `T_ISSET`, or `T_UNSET` token.
*
* @return array A multi-dimentional array information on each parameter/array item.
* The information gathered about each parameter/array item is in the following format:
* ```php
* 1 => array(
* 'start' => int, // The stack pointer to the first token in the parameter/array item.
* 'end' => int, // The stack pointer to the last token in the parameter/array item.
* 'raw' => string, // A string with the contents of all tokens between `start` and `end`.
* 'clean' => string, // Same as `raw`, but all comment tokens have been stripped out.
* )
* ```
* _Note: The array starts at index 1._
* If no parameters/array items are found, an empty array will be returned.
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the token passed is not one of the
* accepted types or doesn't exist.
*/
public static function getParameters(File $phpcsFile, $stackPtr)
{
if (self::hasParameters($phpcsFile, $stackPtr) === false) {
return [];
}
// Ok, we know we have a valid token with parameters and valid open & close brackets/parenthesis.
$tokens = $phpcsFile->getTokens();
// Mark the beginning and end tokens.
if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY
|| $tokens[$stackPtr]['code'] === \T_OPEN_SQUARE_BRACKET
) {
$opener = $stackPtr;
$closer = $tokens[$stackPtr]['bracket_closer'];
} else {
$opener = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
$closer = $tokens[$opener]['parenthesis_closer'];
}
$parameters = [];
$nextComma = $opener;
$paramStart = ($opener + 1);
$cnt = 1;
$stopPoints = self::$callParsingStopPoints + Tokens::$scopeOpeners;
$stopPoints[] = $tokens[$closer]['code'];
while (($nextComma = $phpcsFile->findNext($stopPoints, ($nextComma + 1), ($closer + 1))) !== false) {
// Ignore anything within square brackets.
if (isset($tokens[$nextComma]['bracket_opener'], $tokens[$nextComma]['bracket_closer'])
&& $nextComma === $tokens[$nextComma]['bracket_opener']
) {
$nextComma = $tokens[$nextComma]['bracket_closer'];
continue;
}
// Skip past nested arrays, function calls and arbitrary groupings.
if ($tokens[$nextComma]['code'] === \T_OPEN_PARENTHESIS
&& isset($tokens[$nextComma]['parenthesis_closer'])
) {
$nextComma = $tokens[$nextComma]['parenthesis_closer'];
continue;
}
// Skip past closures, anonymous classes and anything else scope related.
if (isset($tokens[$nextComma]['scope_condition'], $tokens[$nextComma]['scope_closer'])
&& $tokens[$nextComma]['scope_condition'] === $nextComma
) {
$nextComma = $tokens[$nextComma]['scope_closer'];
continue;
}
// Skip over potentially large docblocks.
if ($tokens[$nextComma]['code'] === \T_DOC_COMMENT_OPEN_TAG
&& isset($tokens[$nextComma]['comment_closer'])
) {
$nextComma = $tokens[$nextComma]['comment_closer'];
continue;
}
if ($tokens[$nextComma]['code'] !== \T_COMMA
&& $tokens[$nextComma]['code'] !== $tokens[$closer]['code']
) {
// Just in case.
continue;
}
// Ok, we've reached the end of the parameter.
$paramEnd = ($nextComma - 1);
$parameters[$cnt]['start'] = $paramStart;
$parameters[$cnt]['end'] = $paramEnd;
$parameters[$cnt]['raw'] = \trim(GetTokensAsString::normal($phpcsFile, $paramStart, $paramEnd));
$parameters[$cnt]['clean'] = \trim(GetTokensAsString::noComments($phpcsFile, $paramStart, $paramEnd));
// Check if there are more tokens before the closing parenthesis.
// Prevents function calls with trailing comma's from setting an extra parameter:
// `functionCall( $param1, $param2, );`.
$hasNextParam = $phpcsFile->findNext(
Tokens::$emptyTokens,
($nextComma + 1),
$closer,
true
);
if ($hasNextParam === false) {
break;
}
// Prepare for the next parameter.
$paramStart = ($nextComma + 1);
$cnt++;
}
return $parameters;
}
/**
* Get information on a specific parameter passed.
*
* See {@see PassedParameters::hasParameters()} for information on the supported constructs.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position of the `T_STRING`, PHP 8.0 identifier
* name token, `T_VARIABLE`, `T_ARRAY`, `T_OPEN_SHORT_ARRAY`,
* `T_ISSET`, or `T_UNSET` token.
* @param int $paramOffset The 1-based index position of the parameter to retrieve.
*
* @return array|false Array with information on the parameter/array item at the specified offset.
* Or `FALSE` if the specified parameter/array item is not found.
* The format of the return value is:
* ```php
* array(
* 'start' => int, // The stack pointer to the first token in the parameter/array item.
* 'end' => int, // The stack pointer to the last token in the parameter/array item.
* 'raw' => string, // A string with the contents of all tokens between `start` and `end`.
* 'clean' => string, // Same as `raw`, but all comment tokens have been stripped out.
* )
* ```
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the token passed is not one of the
* accepted types or doesn't exist.
*/
public static function getParameter(File $phpcsFile, $stackPtr, $paramOffset)
{
$parameters = self::getParameters($phpcsFile, $stackPtr);
if (isset($parameters[$paramOffset]) === false) {
return false;
}
return $parameters[$paramOffset];
}
/**
* Count the number of parameters which have been passed.
*
* See {@see PassedParameters::hasParameters()} for information on the supported constructs.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position of the `T_STRING`, PHP 8.0 identifier
* name token, `T_VARIABLE`, `T_ARRAY`, `T_OPEN_SHORT_ARRAY`,
* `T_ISSET`, or `T_UNSET` token.
*
* @return int
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the token passed is not one of the
* accepted types or doesn't exist.
*/
public static function getParameterCount(File $phpcsFile, $stackPtr)
{
if (self::hasParameters($phpcsFile, $stackPtr) === false) {
return 0;
}
return \count(self::getParameters($phpcsFile, $stackPtr));
}
}