1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniff. |
4
|
|
|
* |
5
|
|
|
* @category PHP |
6
|
|
|
* @package PHPCompatibility |
7
|
|
|
* @author Wim Godden <[email protected]> |
8
|
|
|
* @copyright 2014 Cu.be Solutions bvba |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* PHPCompatibility_Sniff. |
13
|
|
|
* |
14
|
|
|
* @category PHP |
15
|
|
|
* @package PHPCompatibility |
16
|
|
|
* @author Wim Godden <[email protected]> |
17
|
|
|
* @copyright 2014 Cu.be Solutions bvba |
18
|
|
|
*/ |
19
|
|
|
abstract class PHPCompatibility_Sniff implements PHP_CodeSniffer_Sniff |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
const REGEX_COMPLEX_VARS = '`(?:(\{)?(?<!\\\\)\$)?(\{)?(?<!\\\\)\$(\{)?(?P<varname>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:->\$?(?P>varname)|\[[^\]]+\]|::\$?(?P>varname)|\([^\)]*\))*(?(3)\}|)(?(2)\}|)(?(1)\}|)`'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* List of superglobals as an array of strings. |
26
|
|
|
* |
27
|
|
|
* Used by the ParameterShadowSuperGlobals and ForbiddenClosureUseVariableNames sniffs. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $superglobals = array( |
32
|
|
|
'$GLOBALS', |
33
|
|
|
'$_SERVER', |
34
|
|
|
'$_GET', |
35
|
|
|
'$_POST', |
36
|
|
|
'$_FILES', |
37
|
|
|
'$_COOKIE', |
38
|
|
|
'$_SESSION', |
39
|
|
|
'$_REQUEST', |
40
|
|
|
'$_ENV', |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* List of functions using hash algorithm as parameter (always the first parameter). |
45
|
|
|
* |
46
|
|
|
* Used by the new/removed hash algorithm sniffs. |
47
|
|
|
* Key is the function name, value is the 1-based parameter position in the function call. |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $hashAlgoFunctions = array( |
52
|
|
|
'hash_file' => 1, |
53
|
|
|
'hash_hmac_file' => 1, |
54
|
|
|
'hash_hmac' => 1, |
55
|
|
|
'hash_init' => 1, |
56
|
|
|
'hash_pbkdf2' => 1, |
57
|
|
|
'hash' => 1, |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* List of functions which take an ini directive as parameter (always the first parameter). |
62
|
|
|
* |
63
|
|
|
* Used by the new/removed ini directives sniffs. |
64
|
|
|
* Key is the function name, value is the 1-based parameter position in the function call. |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected $iniFunctions = array( |
69
|
|
|
'ini_get' => 1, |
70
|
|
|
'ini_set' => 1, |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Internal cache of the results of the `getFunctionCallParameters()` method. |
75
|
|
|
* |
76
|
|
|
* The results are stored in a multi-dimensional array by filename and $stackPtr of |
77
|
|
|
* the function call/array token. |
78
|
|
|
* |
79
|
|
|
* @var array |
80
|
|
|
*/ |
81
|
|
|
private $getFunctionCallParametersCache = array(); |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the testVersion configuration variable. |
86
|
|
|
* |
87
|
|
|
* The testVersion configuration variable may be in any of the following formats: |
88
|
|
|
* 1) Omitted/empty, in which case no version is specified. This effectively |
89
|
|
|
* disables all the checks for new PHP features provided by this standard. |
90
|
|
|
* 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
91
|
|
|
* the code will run on that version of PHP (no deprecated features or newer |
92
|
|
|
* features being used). |
93
|
|
|
* 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
94
|
|
|
* on all PHP versions in that range, and that it doesn't use any features that |
95
|
|
|
* were deprecated by the final version in the list, or which were not available |
96
|
|
|
* for the first version in the list. |
97
|
|
|
* We accept ranges where one of the components is missing, e.g. "-5.6" means |
98
|
|
|
* all versions up to PHP 5.6, and "7.0-" means all versions above PHP 7.0. |
99
|
|
|
* PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
100
|
|
|
* would be treated as invalid, and ignored. |
101
|
|
|
* |
102
|
|
|
* @return array $arrTestVersions will hold an array containing min/max version |
103
|
|
|
* of PHP that we are checking against (see above). If only a |
104
|
|
|
* single version number is specified, then this is used as |
105
|
|
|
* both the min and max. |
106
|
|
|
* |
107
|
|
|
* @throws PHP_CodeSniffer_Exception If testVersion is invalid. |
108
|
|
|
*/ |
109
|
|
|
private function getTestVersion() |
110
|
|
|
{ |
111
|
|
|
static $arrTestVersions = array(); |
112
|
|
|
|
113
|
|
|
$testVersion = trim(PHP_CodeSniffer::getConfigData('testVersion')); |
114
|
|
|
|
115
|
|
|
if (isset($arrTestVersions[$testVersion]) === false && empty($testVersion) === false) { |
116
|
|
|
|
117
|
|
|
$arrTestVersions[$testVersion] = array(null, null); |
118
|
|
|
if (preg_match('/^\d+\.\d+$/', $testVersion)) { |
119
|
|
|
$arrTestVersions[$testVersion] = array($testVersion, $testVersion); |
120
|
|
|
|
121
|
|
|
} elseif (preg_match('/^(\d+\.\d+)\s*-\s*(\d+\.\d+)$/', $testVersion, $matches)) { |
122
|
|
|
if (version_compare($matches[1], $matches[2], '>')) { |
123
|
|
|
trigger_error( |
124
|
|
|
"Invalid range in testVersion setting: '" . $testVersion . "'", |
125
|
|
|
E_USER_WARNING |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
} else { |
129
|
|
|
$arrTestVersions[$testVersion] = array($matches[1], $matches[2]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} elseif (preg_match('/^\d+\.\d+-$/', $testVersion)) { |
133
|
|
|
// If no upper-limit is set, we set the max version to 99.9. |
134
|
|
|
// This is *probably* safe... :-) |
135
|
|
|
$arrTestVersions[$testVersion] = array(substr($testVersion, 0, -1), '99.9'); |
136
|
|
|
|
137
|
|
|
} elseif (preg_match('/^-\d+\.\d+$/', $testVersion)) { |
138
|
|
|
// If no lower-limit is set, we set the min version to 4.0. |
139
|
|
|
// Whilst development focuses on PHP 5 and above, we also accept |
140
|
|
|
// sniffs for PHP 4, so we include that as the minimum. |
141
|
|
|
// (It makes no sense to support PHP 3 as this was effectively a |
142
|
|
|
// different language). |
143
|
|
|
$arrTestVersions[$testVersion] = array('4.0', substr($testVersion, 1)); |
144
|
|
|
|
145
|
|
|
} elseif ($testVersion !== '') { |
146
|
|
|
trigger_error( |
147
|
|
|
"Invalid testVersion setting: '" . $testVersion . "'", |
148
|
|
|
E_USER_WARNING |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (isset($arrTestVersions[$testVersion])) { |
154
|
|
|
return $arrTestVersions[$testVersion]; |
155
|
|
|
} else { |
156
|
|
|
return array(null, null); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Check whether a specific PHP version is equal to or higher than the maximum |
163
|
|
|
* supported PHP version as provided by the user in `testVersion`. |
164
|
|
|
* |
165
|
|
|
* Should be used when sniffing for *old* PHP features (deprecated/removed). |
166
|
|
|
* |
167
|
|
|
* @param string $phpVersion A PHP version number in 'major.minor' format. |
168
|
|
|
* |
169
|
|
|
* @return bool True if testVersion has not been provided or if the PHP version |
170
|
|
|
* is equal to or higher than the highest supported PHP version |
171
|
|
|
* in testVersion. False otherwise. |
172
|
|
|
*/ |
173
|
|
View Code Duplication |
public function supportsAbove($phpVersion) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
$testVersion = $this->getTestVersion(); |
176
|
|
|
$testVersion = $testVersion[1]; |
177
|
|
|
|
178
|
|
|
if (is_null($testVersion) |
179
|
|
|
|| version_compare($testVersion, $phpVersion) >= 0 |
180
|
|
|
) { |
181
|
|
|
return true; |
182
|
|
|
} else { |
183
|
|
|
return false; |
184
|
|
|
} |
185
|
|
|
}//end supportsAbove() |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Check whether a specific PHP version is equal to or lower than the minimum |
190
|
|
|
* supported PHP version as provided by the user in `testVersion`. |
191
|
|
|
* |
192
|
|
|
* Should be used when sniffing for *new* PHP features. |
193
|
|
|
* |
194
|
|
|
* @param string $phpVersion A PHP version number in 'major.minor' format. |
195
|
|
|
* |
196
|
|
|
* @return bool True if the PHP version is equal to or lower than the lowest |
197
|
|
|
* supported PHP version in testVersion. |
198
|
|
|
* False otherwise or if no testVersion is provided. |
199
|
|
|
*/ |
200
|
|
View Code Duplication |
public function supportsBelow($phpVersion) |
|
|
|
|
201
|
|
|
{ |
202
|
|
|
$testVersion = $this->getTestVersion(); |
203
|
|
|
$testVersion = $testVersion[0]; |
204
|
|
|
|
205
|
|
|
if (is_null($testVersion) === false |
206
|
|
|
&& version_compare($testVersion, $phpVersion) <= 0 |
207
|
|
|
) { |
208
|
|
|
return true; |
209
|
|
|
} else { |
210
|
|
|
return false; |
211
|
|
|
} |
212
|
|
|
}//end supportsBelow() |
213
|
|
|
|
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Add a PHPCS message to the output stack as either a warning or an error. |
217
|
|
|
* |
218
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file the message applies to. |
219
|
|
|
* @param string $message The message. |
220
|
|
|
* @param int $stackPtr The position of the token |
221
|
|
|
* the message relates to. |
222
|
|
|
* @param bool $isError Whether to report the message as an |
223
|
|
|
* 'error' or 'warning'. |
224
|
|
|
* Defaults to true (error). |
225
|
|
|
* @param string $code The error code for the message. |
226
|
|
|
* Defaults to 'Found'. |
227
|
|
|
* @param array $data Optional input for the data replacements. |
228
|
|
|
* |
229
|
|
|
* @return void |
230
|
|
|
*/ |
231
|
|
|
public function addMessage($phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array()) |
232
|
|
|
{ |
233
|
|
|
if ($isError === true) { |
234
|
|
|
$phpcsFile->addError($message, $stackPtr, $code, $data); |
235
|
|
|
} else { |
236
|
|
|
$phpcsFile->addWarning($message, $stackPtr, $code, $data); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Convert an arbitrary string to an alphanumeric string with underscores. |
243
|
|
|
* |
244
|
|
|
* Pre-empt issues with arbitrary strings being used as error codes in XML and PHP. |
245
|
|
|
* |
246
|
|
|
* @param string $baseString Arbitrary string. |
247
|
|
|
* |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
public function stringToErrorCode($baseString) |
251
|
|
|
{ |
252
|
|
|
return preg_replace('`[^a-z0-9_]`i', '_', strtolower($baseString)); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Strip quotes surrounding an arbitrary string. |
258
|
|
|
* |
259
|
|
|
* Intended for use with the content of a T_CONSTANT_ENCAPSED_STRING / T_DOUBLE_QUOTED_STRING. |
260
|
|
|
* |
261
|
|
|
* @param string $string The raw string. |
262
|
|
|
* |
263
|
|
|
* @return string String without quotes around it. |
264
|
|
|
*/ |
265
|
|
|
public function stripQuotes($string) |
266
|
|
|
{ |
267
|
|
|
return preg_replace('`^([\'"])(.*)\1$`Ds', '$2', $string); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Strip variables from an arbitrary double quoted string. |
273
|
|
|
* |
274
|
|
|
* Intended for use with the content of a T_DOUBLE_QUOTED_STRING. |
275
|
|
|
* |
276
|
|
|
* @param string $string The raw string. |
277
|
|
|
* |
278
|
|
|
* @return string String without variables in it. |
279
|
|
|
*/ |
280
|
|
|
public function stripVariables($string) |
281
|
|
|
{ |
282
|
|
|
if (strpos($string, '$') === false) { |
283
|
|
|
return $string; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return preg_replace(self::REGEX_COMPLEX_VARS, '', $string); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Make all top level array keys in an array lowercase. |
292
|
|
|
* |
293
|
|
|
* @param array $array Initial array. |
294
|
|
|
* |
295
|
|
|
* @return array Same array, but with all lowercase top level keys. |
296
|
|
|
*/ |
297
|
|
|
public function arrayKeysToLowercase($array) |
298
|
|
|
{ |
299
|
|
|
$keys = array_keys($array); |
300
|
|
|
$keys = array_map('strtolower', $keys); |
301
|
|
|
return array_combine($keys, $array); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Returns the name(s) of the interface(s) that the specified class implements. |
307
|
|
|
* |
308
|
|
|
* Returns FALSE on error or if there are no implemented interface names. |
309
|
|
|
* |
310
|
|
|
* {@internal Duplicate of same method as introduced in PHPCS 2.7. |
311
|
|
|
* This method also includes an improvement we use which was only introduced |
312
|
|
|
* in PHPCS 2.8.0, so only defer to upstream for higher versions. |
313
|
|
|
* Once the minimum supported PHPCS version for this sniff library goes beyond |
314
|
|
|
* that, this method can be removed and calls to it replaced with |
315
|
|
|
* `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
316
|
|
|
* |
317
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
318
|
|
|
* @param int $stackPtr The position of the class token. |
319
|
|
|
* |
320
|
|
|
* @return array|false |
321
|
|
|
*/ |
322
|
|
|
public function findImplementedInterfaceNames(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
323
|
|
|
{ |
324
|
|
|
if (version_compare(PHP_CodeSniffer::VERSION, '2.7.1', '>') === true) { |
325
|
|
|
return $phpcsFile->findImplementedInterfaceNames($stackPtr); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
$tokens = $phpcsFile->getTokens(); |
329
|
|
|
|
330
|
|
|
// Check for the existence of the token. |
331
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
332
|
|
|
return false; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
View Code Duplication |
if ($tokens[$stackPtr]['code'] !== T_CLASS |
|
|
|
|
336
|
|
|
&& $tokens[$stackPtr]['type'] !== 'T_ANON_CLASS' |
337
|
|
|
) { |
338
|
|
|
return false; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
342
|
|
|
return false; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$classOpenerIndex = $tokens[$stackPtr]['scope_opener']; |
346
|
|
|
$implementsIndex = $phpcsFile->findNext(T_IMPLEMENTS, $stackPtr, $classOpenerIndex); |
347
|
|
|
if ($implementsIndex === false) { |
348
|
|
|
return false; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
$find = array( |
352
|
|
|
T_NS_SEPARATOR, |
353
|
|
|
T_STRING, |
354
|
|
|
T_WHITESPACE, |
355
|
|
|
T_COMMA, |
356
|
|
|
); |
357
|
|
|
|
358
|
|
|
$end = $phpcsFile->findNext($find, ($implementsIndex + 1), ($classOpenerIndex + 1), true); |
359
|
|
|
$name = $phpcsFile->getTokensAsString(($implementsIndex + 1), ($end - $implementsIndex - 1)); |
360
|
|
|
$name = trim($name); |
361
|
|
|
|
362
|
|
|
if ($name === '') { |
363
|
|
|
return false; |
364
|
|
|
} else { |
365
|
|
|
$names = explode(',', $name); |
366
|
|
|
$names = array_map('trim', $names); |
367
|
|
|
return $names; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
}//end findImplementedInterfaceNames() |
371
|
|
|
|
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Checks if a function call has parameters. |
375
|
|
|
* |
376
|
|
|
* Expects to be passed the T_STRING stack pointer for the function call. |
377
|
|
|
* If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
378
|
|
|
* |
379
|
|
|
* Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, it |
380
|
|
|
* will detect whether the array has values or is empty. |
381
|
|
|
* |
382
|
|
|
* @link https://github.com/wimg/PHPCompatibility/issues/120 |
383
|
|
|
* @link https://github.com/wimg/PHPCompatibility/issues/152 |
384
|
|
|
* |
385
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
386
|
|
|
* @param int $stackPtr The position of the function call token. |
387
|
|
|
* |
388
|
|
|
* @return bool |
389
|
|
|
*/ |
390
|
|
|
public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
391
|
|
|
{ |
392
|
|
|
$tokens = $phpcsFile->getTokens(); |
393
|
|
|
|
394
|
|
|
// Check for the existence of the token. |
395
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
396
|
|
|
return false; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
// Is this one of the tokens this function handles ? |
400
|
|
|
if (in_array($tokens[$stackPtr]['code'], array(T_STRING, T_ARRAY, T_OPEN_SHORT_ARRAY), true) === false) { |
401
|
|
|
return false; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
$nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
405
|
|
|
|
406
|
|
|
// Deal with short array syntax. |
407
|
|
|
if ($tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY) { |
408
|
|
|
if (isset($tokens[$stackPtr]['bracket_closer']) === false) { |
409
|
|
|
return false; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
if ($nextNonEmpty === $tokens[$stackPtr]['bracket_closer']) { |
413
|
|
|
// No parameters. |
414
|
|
|
return false; |
415
|
|
|
} else { |
416
|
|
|
return true; |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
// Deal with function calls & long arrays. |
421
|
|
|
// Next non-empty token should be the open parenthesis. |
422
|
|
|
if ($nextNonEmpty === false && $tokens[$nextNonEmpty]['code'] !== T_OPEN_PARENTHESIS) { |
423
|
|
|
return false; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
if (isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false) { |
427
|
|
|
return false; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
$closeParenthesis = $tokens[$nextNonEmpty]['parenthesis_closer']; |
431
|
|
|
$nextNextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $nextNonEmpty + 1, $closeParenthesis + 1, true); |
432
|
|
|
|
433
|
|
|
if ($nextNextNonEmpty === $closeParenthesis) { |
434
|
|
|
// No parameters. |
435
|
|
|
return false; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
return true; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Count the number of parameters a function call has been passed. |
444
|
|
|
* |
445
|
|
|
* Expects to be passed the T_STRING stack pointer for the function call. |
446
|
|
|
* If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
447
|
|
|
* |
448
|
|
|
* Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
449
|
|
|
* it will return the number of values in the array. |
450
|
|
|
* |
451
|
|
|
* @link https://github.com/wimg/PHPCompatibility/issues/111 |
452
|
|
|
* @link https://github.com/wimg/PHPCompatibility/issues/114 |
453
|
|
|
* @link https://github.com/wimg/PHPCompatibility/issues/151 |
454
|
|
|
* |
455
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
456
|
|
|
* @param int $stackPtr The position of the function call token. |
457
|
|
|
* |
458
|
|
|
* @return int |
459
|
|
|
*/ |
460
|
|
|
public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
461
|
|
|
{ |
462
|
|
|
if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) { |
463
|
|
|
return 0; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
return count($this->getFunctionCallParameters($phpcsFile, $stackPtr)); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Get information on all parameters passed to a function call. |
472
|
|
|
* |
473
|
|
|
* Expects to be passed the T_STRING stack pointer for the function call. |
474
|
|
|
* If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
475
|
|
|
* |
476
|
|
|
* Will return an multi-dimentional array with the start token pointer, end token |
477
|
|
|
* pointer and raw parameter value for all parameters. Index will be 1-based. |
478
|
|
|
* If no parameters are found, will return an empty array. |
479
|
|
|
* |
480
|
|
|
* Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
481
|
|
|
* it will tokenize the values / key/value pairs contained in the array call. |
482
|
|
|
* |
483
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
484
|
|
|
* @param int $stackPtr The position of the function call token. |
485
|
|
|
* |
486
|
|
|
* @return array |
487
|
|
|
*/ |
488
|
|
|
public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
489
|
|
|
{ |
490
|
|
|
$currentFile = $phpcsFile->getFilename(); |
491
|
|
|
|
492
|
|
|
// Check if this function call/array has been parsed before and if so, use the cached results. |
493
|
|
|
if (isset($this->getFunctionCallParametersCache[$currentFile][$stackPtr]) === true) { |
494
|
|
|
return $this->getFunctionCallParametersCache[$currentFile][$stackPtr]; |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) { |
498
|
|
|
$this->getFunctionCallParametersCache[$currentFile][$stackPtr] = array(); |
499
|
|
|
return array(); |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
// Ok, we know we have a T_STRING, T_ARRAY or T_OPEN_SHORT_ARRAY with parameters |
503
|
|
|
// and valid open & close brackets/parenthesis. |
504
|
|
|
$tokens = $phpcsFile->getTokens(); |
505
|
|
|
|
506
|
|
|
// Mark the beginning and end tokens. |
507
|
|
|
if ($tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY) { |
508
|
|
|
$opener = $stackPtr; |
509
|
|
|
$closer = $tokens[$stackPtr]['bracket_closer']; |
510
|
|
|
|
511
|
|
|
$nestedParenthesisCount = 0; |
512
|
|
|
|
513
|
|
|
} else { |
514
|
|
|
$opener = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
515
|
|
|
$closer = $tokens[$opener]['parenthesis_closer']; |
516
|
|
|
|
517
|
|
|
$nestedParenthesisCount = 1; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
// Which nesting level is the one we are interested in ? |
521
|
|
View Code Duplication |
if (isset($tokens[$opener]['nested_parenthesis'])) { |
|
|
|
|
522
|
|
|
$nestedParenthesisCount += count($tokens[$opener]['nested_parenthesis']); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
$parameters = array(); |
526
|
|
|
$nextComma = $opener; |
527
|
|
|
$paramStart = $opener + 1; |
528
|
|
|
$cnt = 1; |
529
|
|
|
while (($nextComma = $phpcsFile->findNext(array(T_COMMA, $tokens[$closer]['code'], T_OPEN_SHORT_ARRAY), $nextComma + 1, $closer + 1)) !== false) { |
530
|
|
|
// Ignore anything within short array definition brackets. |
531
|
|
|
if ($tokens[$nextComma]['type'] === 'T_OPEN_SHORT_ARRAY' |
532
|
|
|
&& (isset($tokens[$nextComma]['bracket_opener']) |
533
|
|
|
&& $tokens[$nextComma]['bracket_opener'] === $nextComma) |
534
|
|
|
&& isset($tokens[$nextComma]['bracket_closer']) |
535
|
|
|
) { |
536
|
|
|
// Skip forward to the end of the short array definition. |
537
|
|
|
$nextComma = $tokens[$nextComma]['bracket_closer']; |
538
|
|
|
continue; |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
// Ignore comma's at a lower nesting level. |
542
|
|
|
if ($tokens[$nextComma]['type'] === 'T_COMMA' |
543
|
|
|
&& isset($tokens[$nextComma]['nested_parenthesis']) |
544
|
|
|
&& count($tokens[$nextComma]['nested_parenthesis']) !== $nestedParenthesisCount |
545
|
|
|
) { |
546
|
|
|
continue; |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
// Ignore closing parenthesis/bracket if not 'ours'. |
550
|
|
|
if ($tokens[$nextComma]['type'] === $tokens[$closer]['type'] && $nextComma !== $closer) { |
551
|
|
|
continue; |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
// Ok, we've reached the end of the parameter. |
555
|
|
|
$parameters[$cnt]['start'] = $paramStart; |
556
|
|
|
$parameters[$cnt]['end'] = $nextComma - 1; |
557
|
|
|
$parameters[$cnt]['raw'] = trim($phpcsFile->getTokensAsString($paramStart, ($nextComma - $paramStart))); |
558
|
|
|
|
559
|
|
|
// Check if there are more tokens before the closing parenthesis. |
560
|
|
|
// Prevents code like the following from setting a third parameter: |
561
|
|
|
// functionCall( $param1, $param2, ); |
|
|
|
|
562
|
|
|
$hasNextParam = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $nextComma + 1, $closer, true, null, true); |
563
|
|
|
if ($hasNextParam === false) { |
564
|
|
|
break; |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
// Prepare for the next parameter. |
568
|
|
|
$paramStart = $nextComma + 1; |
569
|
|
|
$cnt++; |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
// Cache the results. |
573
|
|
|
$this->getFunctionCallParametersCache[$currentFile][$stackPtr] = $parameters; |
574
|
|
|
|
575
|
|
|
return $parameters; |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* Get information on a specific parameter passed to a function call. |
581
|
|
|
* |
582
|
|
|
* Expects to be passed the T_STRING stack pointer for the function call. |
583
|
|
|
* If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
584
|
|
|
* |
585
|
|
|
* Will return a array with the start token pointer, end token pointer and the raw value |
586
|
|
|
* of the parameter at a specific offset. |
587
|
|
|
* If the specified parameter is not found, will return false. |
588
|
|
|
* |
589
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
590
|
|
|
* @param int $stackPtr The position of the function call token. |
591
|
|
|
* @param int $paramOffset The 1-based index position of the parameter to retrieve. |
592
|
|
|
* |
593
|
|
|
* @return array|false |
594
|
|
|
*/ |
595
|
|
|
public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
596
|
|
|
{ |
597
|
|
|
$parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); |
598
|
|
|
|
599
|
|
|
if (isset($parameters[$paramOffset]) === false) { |
600
|
|
|
return false; |
601
|
|
|
} else { |
602
|
|
|
return $parameters[$paramOffset]; |
603
|
|
|
} |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
|
607
|
|
|
/** |
608
|
|
|
* Verify whether a token is within a scoped condition. |
609
|
|
|
* |
610
|
|
|
* If the optional $validScopes parameter has been passed, the function |
611
|
|
|
* will check that the token has at least one condition which is of a |
612
|
|
|
* type defined in $validScopes. |
613
|
|
|
* |
614
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
615
|
|
|
* @param int $stackPtr The position of the token. |
616
|
|
|
* @param array|int $validScopes Optional. Array of valid scopes |
617
|
|
|
* or int value of a valid scope. |
618
|
|
|
* Pass the T_.. constant(s) for the |
619
|
|
|
* desired scope to this parameter. |
620
|
|
|
* |
621
|
|
|
* @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
622
|
|
|
* If the $scopeTypes are set: True if *one* of the conditions is a |
623
|
|
|
* valid scope, false otherwise. |
624
|
|
|
*/ |
625
|
|
|
public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
626
|
|
|
{ |
627
|
|
|
$tokens = $phpcsFile->getTokens(); |
628
|
|
|
|
629
|
|
|
// Check for the existence of the token. |
630
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
631
|
|
|
return false; |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
// No conditions = no scope. |
635
|
|
|
if (empty($tokens[$stackPtr]['conditions'])) { |
636
|
|
|
return false; |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
// Ok, there are conditions, do we have to check for specific ones ? |
640
|
|
|
if (isset($validScopes) === false) { |
641
|
|
|
return true; |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
return $phpcsFile->hasCondition($stackPtr, $validScopes); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Verify whether a token is within a class scope. |
650
|
|
|
* |
651
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
652
|
|
|
* @param int $stackPtr The position of the token. |
653
|
|
|
* @param bool $strict Whether to strictly check for the T_CLASS |
654
|
|
|
* scope or also accept interfaces and traits |
655
|
|
|
* as scope. |
656
|
|
|
* |
657
|
|
|
* @return bool True if within class scope, false otherwise. |
658
|
|
|
*/ |
659
|
|
|
public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
660
|
|
|
{ |
661
|
|
|
$validScopes = array(T_CLASS); |
662
|
|
|
if (defined('T_ANON_CLASS') === true) { |
663
|
|
|
$validScopes[] = T_ANON_CLASS; |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
if ($strict === false) { |
667
|
|
|
$validScopes[] = T_INTERFACE; |
668
|
|
|
$validScopes[] = T_TRAIT; |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
return $phpcsFile->hasCondition($stackPtr, $validScopes); |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* Verify whether a token is within a scoped use statement. |
677
|
|
|
* |
678
|
|
|
* PHPCS cross-version compatibility method. |
679
|
|
|
* |
680
|
|
|
* In PHPCS 1.x no conditions are set for a scoped use statement. |
681
|
|
|
* This method works around that limitation. |
682
|
|
|
* |
683
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
684
|
|
|
* @param int $stackPtr The position of the token. |
685
|
|
|
* |
686
|
|
|
* @return bool True if within use scope, false otherwise. |
687
|
|
|
*/ |
688
|
|
|
public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
689
|
|
|
{ |
690
|
|
|
static $isLowPHPCS, $ignoreTokens; |
691
|
|
|
|
692
|
|
|
if (isset($isLowPHPCS) === false) { |
693
|
|
|
$isLowPHPCS = version_compare(PHP_CodeSniffer::VERSION, '2.3.0', '<'); |
694
|
|
|
} |
695
|
|
|
if (isset($ignoreTokens) === false) { |
696
|
|
|
$ignoreTokens = PHP_CodeSniffer_Tokens::$emptyTokens; |
697
|
|
|
$ignoreTokens[T_STRING] = T_STRING; |
698
|
|
|
$ignoreTokens[T_AS] = T_AS; |
699
|
|
|
$ignoreTokens[T_PUBLIC] = T_PUBLIC; |
700
|
|
|
$ignoreTokens[T_PROTECTED] = T_PROTECTED; |
701
|
|
|
$ignoreTokens[T_PRIVATE] = T_PRIVATE; |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
// PHPCS 2.0. |
705
|
|
|
if ($isLowPHPCS === false) { |
706
|
|
|
return $phpcsFile->hasCondition($stackPtr, T_USE); |
707
|
|
|
} else { |
708
|
|
|
// PHPCS 1.x. |
709
|
|
|
$tokens = $phpcsFile->getTokens(); |
710
|
|
|
$maybeCurlyOpen = $phpcsFile->findPrevious($ignoreTokens, ($stackPtr - 1), null, true); |
711
|
|
|
if ($tokens[$maybeCurlyOpen]['code'] === T_OPEN_CURLY_BRACKET) { |
712
|
|
|
$maybeUseStatement = $phpcsFile->findPrevious($ignoreTokens, ($maybeCurlyOpen - 1), null, true); |
713
|
|
|
if ($tokens[$maybeUseStatement]['code'] === T_USE) { |
714
|
|
|
return true; |
715
|
|
|
} |
716
|
|
|
} |
717
|
|
|
return false; |
718
|
|
|
} |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* Returns the fully qualified class name for a new class instantiation. |
724
|
|
|
* |
725
|
|
|
* Returns an empty string if the class name could not be reliably inferred. |
726
|
|
|
* |
727
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
728
|
|
|
* @param int $stackPtr The position of a T_NEW token. |
729
|
|
|
* |
730
|
|
|
* @return string |
731
|
|
|
*/ |
732
|
|
|
public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
733
|
|
|
{ |
734
|
|
|
$tokens = $phpcsFile->getTokens(); |
735
|
|
|
|
736
|
|
|
// Check for the existence of the token. |
737
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
738
|
|
|
return ''; |
739
|
|
|
} |
740
|
|
|
|
741
|
|
|
if ($tokens[$stackPtr]['code'] !== T_NEW) { |
742
|
|
|
return ''; |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
$start = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
746
|
|
|
if ($start === false) { |
747
|
|
|
return ''; |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
// Bow out if the next token is a variable as we don't know where it was defined. |
751
|
|
|
if ($tokens[$start]['code'] === T_VARIABLE) { |
752
|
|
|
return ''; |
753
|
|
|
} |
754
|
|
|
|
755
|
|
|
// Bow out if the next token is the class keyword. |
756
|
|
|
if ($tokens[$start]['type'] === 'T_ANON_CLASS' || $tokens[$start]['code'] === T_CLASS) { |
757
|
|
|
return ''; |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
$find = array( |
761
|
|
|
T_NS_SEPARATOR, |
762
|
|
|
T_STRING, |
763
|
|
|
T_NAMESPACE, |
764
|
|
|
T_WHITESPACE, |
765
|
|
|
); |
766
|
|
|
|
767
|
|
|
$end = $phpcsFile->findNext($find, ($start + 1), null, true, null, true); |
768
|
|
|
$className = $phpcsFile->getTokensAsString($start, ($end - $start)); |
769
|
|
|
$className = trim($className); |
770
|
|
|
|
771
|
|
|
return $this->getFQName($phpcsFile, $stackPtr, $className); |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
|
775
|
|
|
/** |
776
|
|
|
* Returns the fully qualified name of the class that the specified class extends. |
777
|
|
|
* |
778
|
|
|
* Returns an empty string if the class does not extend another class or if |
779
|
|
|
* the class name could not be reliably inferred. |
780
|
|
|
* |
781
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
782
|
|
|
* @param int $stackPtr The position of a T_CLASS token. |
783
|
|
|
* |
784
|
|
|
* @return string |
785
|
|
|
*/ |
786
|
|
|
public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
787
|
|
|
{ |
788
|
|
|
$tokens = $phpcsFile->getTokens(); |
789
|
|
|
|
790
|
|
|
// Check for the existence of the token. |
791
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
792
|
|
|
return ''; |
793
|
|
|
} |
794
|
|
|
|
795
|
|
View Code Duplication |
if ($tokens[$stackPtr]['code'] !== T_CLASS && $tokens[$stackPtr]['type'] !== 'T_ANON_CLASS') { |
|
|
|
|
796
|
|
|
return ''; |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
$extends = $this->findExtendedClassName($phpcsFile, $stackPtr); |
800
|
|
|
if (empty($extends) || is_string($extends) === false) { |
801
|
|
|
return ''; |
802
|
|
|
} |
803
|
|
|
|
804
|
|
|
return $this->getFQName($phpcsFile, $stackPtr, $extends); |
805
|
|
|
} |
806
|
|
|
|
807
|
|
|
|
808
|
|
|
/** |
809
|
|
|
* Returns the class name for the static usage of a class. |
810
|
|
|
* This can be a call to a method, the use of a property or constant. |
811
|
|
|
* |
812
|
|
|
* Returns an empty string if the class name could not be reliably inferred. |
813
|
|
|
* |
814
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
815
|
|
|
* @param int $stackPtr The position of a T_NEW token. |
816
|
|
|
* |
817
|
|
|
* @return string |
818
|
|
|
*/ |
819
|
|
|
public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
820
|
|
|
{ |
821
|
|
|
$tokens = $phpcsFile->getTokens(); |
822
|
|
|
|
823
|
|
|
// Check for the existence of the token. |
824
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
825
|
|
|
return ''; |
826
|
|
|
} |
827
|
|
|
|
828
|
|
|
if ($tokens[$stackPtr]['code'] !== T_DOUBLE_COLON) { |
829
|
|
|
return ''; |
830
|
|
|
} |
831
|
|
|
|
832
|
|
|
// Nothing to do if previous token is a variable as we don't know where it was defined. |
833
|
|
|
if ($tokens[$stackPtr - 1]['code'] === T_VARIABLE) { |
834
|
|
|
return ''; |
835
|
|
|
} |
836
|
|
|
|
837
|
|
|
// Nothing to do if 'parent' or 'static' as we don't know how far the class tree extends. |
838
|
|
|
if (in_array($tokens[$stackPtr - 1]['code'], array(T_PARENT, T_STATIC), true)) { |
839
|
|
|
return ''; |
840
|
|
|
} |
841
|
|
|
|
842
|
|
|
// Get the classname from the class declaration if self is used. |
843
|
|
|
if ($tokens[$stackPtr - 1]['code'] === T_SELF) { |
844
|
|
|
$classDeclarationPtr = $phpcsFile->findPrevious(T_CLASS, $stackPtr - 1); |
845
|
|
|
if ($classDeclarationPtr === false) { |
846
|
|
|
return ''; |
847
|
|
|
} |
848
|
|
|
$className = $phpcsFile->getDeclarationName($classDeclarationPtr); |
849
|
|
|
return $this->getFQName($phpcsFile, $classDeclarationPtr, $className); |
850
|
|
|
} |
851
|
|
|
|
852
|
|
|
$find = array( |
853
|
|
|
T_NS_SEPARATOR, |
854
|
|
|
T_STRING, |
855
|
|
|
T_NAMESPACE, |
856
|
|
|
T_WHITESPACE, |
857
|
|
|
); |
858
|
|
|
|
859
|
|
|
$start = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true, null, true); |
860
|
|
|
if ($start === false || isset($tokens[($start + 1)]) === false) { |
861
|
|
|
return ''; |
862
|
|
|
} |
863
|
|
|
|
864
|
|
|
$start = ($start + 1); |
865
|
|
|
$className = $phpcsFile->getTokensAsString($start, ($stackPtr - $start)); |
866
|
|
|
$className = trim($className); |
867
|
|
|
|
868
|
|
|
return $this->getFQName($phpcsFile, $stackPtr, $className); |
869
|
|
|
} |
870
|
|
|
|
871
|
|
|
|
872
|
|
|
/** |
873
|
|
|
* Get the Fully Qualified name for a class/function/constant etc. |
874
|
|
|
* |
875
|
|
|
* Checks if a class/function/constant name is already fully qualified and |
876
|
|
|
* if not, enrich it with the relevant namespace information. |
877
|
|
|
* |
878
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
879
|
|
|
* @param int $stackPtr The position of the token. |
880
|
|
|
* @param string $name The class / function / constant name. |
881
|
|
|
* |
882
|
|
|
* @return string |
883
|
|
|
*/ |
884
|
|
|
public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
885
|
|
|
{ |
886
|
|
|
if (strpos($name, '\\') === 0) { |
887
|
|
|
// Already fully qualified. |
888
|
|
|
return $name; |
889
|
|
|
} |
890
|
|
|
|
891
|
|
|
// Remove the namespace keyword if used. |
892
|
|
|
if (strpos($name, 'namespace\\') === 0) { |
893
|
|
|
$name = substr($name, 10); |
894
|
|
|
} |
895
|
|
|
|
896
|
|
|
$namespace = $this->determineNamespace($phpcsFile, $stackPtr); |
897
|
|
|
|
898
|
|
|
if ($namespace === '') { |
899
|
|
|
return '\\' . $name; |
900
|
|
|
} else { |
901
|
|
|
return '\\' . $namespace . '\\' . $name; |
902
|
|
|
} |
903
|
|
|
} |
904
|
|
|
|
905
|
|
|
|
906
|
|
|
/** |
907
|
|
|
* Is the class/function/constant name namespaced or global ? |
908
|
|
|
* |
909
|
|
|
* @param string $FQName Fully Qualified name of a class, function etc. |
910
|
|
|
* I.e. should always start with a `\`. |
911
|
|
|
* |
912
|
|
|
* @return bool True if namespaced, false if global. |
913
|
|
|
*/ |
914
|
|
|
public function isNamespaced($FQName) |
915
|
|
|
{ |
916
|
|
|
if (strpos($FQName, '\\') !== 0) { |
917
|
|
|
throw new PHP_CodeSniffer_Exception('$FQName must be a fully qualified name'); |
918
|
|
|
} |
919
|
|
|
|
920
|
|
|
return (strpos(substr($FQName, 1), '\\') !== false); |
921
|
|
|
} |
922
|
|
|
|
923
|
|
|
|
924
|
|
|
/** |
925
|
|
|
* Determine the namespace name an arbitrary token lives in. |
926
|
|
|
* |
927
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
928
|
|
|
* @param int $stackPtr The token position for which to determine the namespace. |
929
|
|
|
* |
930
|
|
|
* @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
931
|
|
|
*/ |
932
|
|
|
public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
933
|
|
|
{ |
934
|
|
|
$tokens = $phpcsFile->getTokens(); |
935
|
|
|
|
936
|
|
|
// Check for the existence of the token. |
937
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
938
|
|
|
return ''; |
939
|
|
|
} |
940
|
|
|
|
941
|
|
|
// Check for scoped namespace {}. |
942
|
|
|
if (empty($tokens[$stackPtr]['conditions']) === false) { |
943
|
|
|
$namespacePtr = $phpcsFile->getCondition($stackPtr, T_NAMESPACE); |
944
|
|
|
if ($namespacePtr !== false) { |
945
|
|
|
$namespace = $this->getDeclaredNamespaceName($phpcsFile, $namespacePtr); |
946
|
|
|
if ($namespace !== false) { |
947
|
|
|
return $namespace; |
948
|
|
|
} |
949
|
|
|
|
950
|
|
|
// We are in a scoped namespace, but couldn't determine the name. Searching for a global namespace is futile. |
951
|
|
|
return ''; |
952
|
|
|
} |
953
|
|
|
} |
954
|
|
|
|
955
|
|
|
/* |
956
|
|
|
* Not in a scoped namespace, so let's see if we can find a non-scoped namespace instead. |
957
|
|
|
* Keeping in mind that: |
958
|
|
|
* - there can be multiple non-scoped namespaces in a file (bad practice, but it happens). |
959
|
|
|
* - the namespace keyword can also be used as part of a function/method call and such. |
960
|
|
|
* - that a non-named namespace resolves to the global namespace. |
961
|
|
|
*/ |
962
|
|
|
$previousNSToken = $stackPtr; |
963
|
|
|
$namespace = false; |
964
|
|
|
do { |
965
|
|
|
$previousNSToken = $phpcsFile->findPrevious(T_NAMESPACE, ($previousNSToken - 1)); |
966
|
|
|
|
967
|
|
|
// Stop if we encounter a scoped namespace declaration as we already know we're not in one. |
968
|
|
|
if (empty($tokens[$previousNSToken]['scope_condition']) === false && $tokens[$previousNSToken]['scope_condition'] === $previousNSToken) { |
969
|
|
|
break; |
970
|
|
|
} |
971
|
|
|
|
972
|
|
|
$namespace = $this->getDeclaredNamespaceName($phpcsFile, $previousNSToken); |
973
|
|
|
|
974
|
|
|
} while ($namespace === false && $previousNSToken !== false); |
975
|
|
|
|
976
|
|
|
// If we still haven't got a namespace, return an empty string. |
977
|
|
|
if ($namespace === false) { |
978
|
|
|
return ''; |
979
|
|
|
} else { |
980
|
|
|
return $namespace; |
981
|
|
|
} |
982
|
|
|
} |
983
|
|
|
|
984
|
|
|
/** |
985
|
|
|
* Get the complete namespace name for a namespace declaration. |
986
|
|
|
* |
987
|
|
|
* For hierarchical namespaces, the name will be composed of several tokens, |
988
|
|
|
* i.e. MyProject\Sub\Level which will be returned together as one string. |
989
|
|
|
* |
990
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
991
|
|
|
* @param int|bool $stackPtr The position of a T_NAMESPACE token. |
992
|
|
|
* |
993
|
|
|
* @return string|false Namespace name or false if not a namespace declaration. |
994
|
|
|
* Namespace name can be an empty string for global namespace declaration. |
995
|
|
|
*/ |
996
|
|
|
public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
997
|
|
|
{ |
998
|
|
|
$tokens = $phpcsFile->getTokens(); |
999
|
|
|
|
1000
|
|
|
// Check for the existence of the token. |
1001
|
|
View Code Duplication |
if ($stackPtr === false || isset($tokens[$stackPtr]) === false) { |
|
|
|
|
1002
|
|
|
return false; |
1003
|
|
|
} |
1004
|
|
|
|
1005
|
|
|
if ($tokens[$stackPtr]['code'] !== T_NAMESPACE) { |
1006
|
|
|
return false; |
1007
|
|
|
} |
1008
|
|
|
|
1009
|
|
|
if ($tokens[($stackPtr + 1)]['code'] === T_NS_SEPARATOR) { |
1010
|
|
|
// Not a namespace declaration, but use of, i.e. namespace\someFunction(); |
1011
|
|
|
return false; |
1012
|
|
|
} |
1013
|
|
|
|
1014
|
|
|
$nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
1015
|
|
|
if ($tokens[$nextToken]['code'] === T_OPEN_CURLY_BRACKET) { |
1016
|
|
|
// Declaration for global namespace when using multiple namespaces in a file. |
1017
|
|
|
// I.e.: namespace {} |
1018
|
|
|
return ''; |
1019
|
|
|
} |
1020
|
|
|
|
1021
|
|
|
// Ok, this should be a namespace declaration, so get all the parts together. |
1022
|
|
|
$validTokens = array( |
1023
|
|
|
T_STRING => true, |
1024
|
|
|
T_NS_SEPARATOR => true, |
1025
|
|
|
T_WHITESPACE => true, |
1026
|
|
|
); |
1027
|
|
|
|
1028
|
|
|
$namespaceName = ''; |
1029
|
|
|
while (isset($validTokens[$tokens[$nextToken]['code']]) === true) { |
1030
|
|
|
$namespaceName .= trim($tokens[$nextToken]['content']); |
1031
|
|
|
$nextToken++; |
1032
|
|
|
} |
1033
|
|
|
|
1034
|
|
|
return $namespaceName; |
1035
|
|
|
} |
1036
|
|
|
|
1037
|
|
|
|
1038
|
|
|
/** |
1039
|
|
|
* Get the stack pointer for a return type token for a given function. |
1040
|
|
|
* |
1041
|
|
|
* Compatible layer for older PHPCS versions which don't recognize |
1042
|
|
|
* return type hints correctly. |
1043
|
|
|
* |
1044
|
|
|
* Expects to be passed T_RETURN_TYPE, T_FUNCTION or T_CLOSURE token. |
1045
|
|
|
* |
1046
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
1047
|
|
|
* @param int $stackPtr The position of the token. |
1048
|
|
|
* |
1049
|
|
|
* @return int|false Stack pointer to the return type token or false if |
1050
|
|
|
* no return type was found or the passed token was |
1051
|
|
|
* not of the correct type. |
1052
|
|
|
*/ |
1053
|
|
|
public function getReturnTypeHintToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
1054
|
|
|
{ |
1055
|
|
|
$tokens = $phpcsFile->getTokens(); |
1056
|
|
|
|
1057
|
|
|
if (defined('T_RETURN_TYPE') && $tokens[$stackPtr]['code'] === T_RETURN_TYPE) { |
1058
|
|
|
return $tokens[$stackPtr]['code']; |
1059
|
|
|
} |
1060
|
|
|
|
1061
|
|
View Code Duplication |
if ($tokens[$stackPtr]['code'] !== T_FUNCTION && $tokens[$stackPtr]['code'] !== T_CLOSURE) { |
|
|
|
|
1062
|
|
|
return false; |
1063
|
|
|
} |
1064
|
|
|
|
1065
|
|
|
if (isset($tokens[$stackPtr]['parenthesis_closer'], $tokens[$stackPtr]['scope_opener']) === false |
1066
|
|
|
|| ($tokens[$stackPtr]['parenthesis_closer'] + 1) === $tokens[$stackPtr]['scope_opener'] |
1067
|
|
|
) { |
1068
|
|
|
return false; |
1069
|
|
|
} |
1070
|
|
|
|
1071
|
|
|
$hasColon = $phpcsFile->findNext(array(T_COLON, T_INLINE_ELSE), ($tokens[$stackPtr]['parenthesis_closer'] + 1), $tokens[$stackPtr]['scope_opener']); |
1072
|
|
|
if ($hasColon === false) { |
1073
|
|
|
return false; |
1074
|
|
|
} |
1075
|
|
|
|
1076
|
|
|
// `self` and `callable` are not being recognized as return types in PHPCS < 2.6.0. |
1077
|
|
|
$unrecognizedTypes = array( |
1078
|
|
|
T_CALLABLE, |
1079
|
|
|
T_SELF, |
1080
|
|
|
); |
1081
|
|
|
|
1082
|
|
|
// Return types are not recognized at all in PHPCS < 2.4.0. |
1083
|
|
|
if (defined('T_RETURN_TYPE') === false) { |
1084
|
|
|
$unrecognizedTypes[] = T_ARRAY; |
1085
|
|
|
$unrecognizedTypes[] = T_STRING; |
1086
|
|
|
} |
1087
|
|
|
|
1088
|
|
|
return $phpcsFile->findNext($unrecognizedTypes, ($hasColon + 1), $tokens[$stackPtr]['scope_opener']); |
1089
|
|
|
} |
1090
|
|
|
|
1091
|
|
|
|
1092
|
|
|
/** |
1093
|
|
|
* Check whether a T_VARIABLE token is a class property declaration. |
1094
|
|
|
* |
1095
|
|
|
* Compatibility layer for PHPCS cross-version compatibility |
1096
|
|
|
* as PHPCS 2.4.0 - 2.7.1 does not have good enough support for |
1097
|
|
|
* anonymous classes. Along the same lines, the`getMemberProperties()` |
1098
|
|
|
* method does not support the `var` prefix. |
1099
|
|
|
* |
1100
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
1101
|
|
|
* @param int $stackPtr The position in the stack of the |
1102
|
|
|
* T_VARIABLE token to verify. |
1103
|
|
|
* |
1104
|
|
|
* @return bool |
1105
|
|
|
*/ |
1106
|
|
|
public function isClassProperty(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
1107
|
|
|
{ |
1108
|
|
|
$tokens = $phpcsFile->getTokens(); |
1109
|
|
|
|
1110
|
|
View Code Duplication |
if (isset($tokens[$stackPtr]) === false || $tokens[$stackPtr]['code'] !== T_VARIABLE) { |
|
|
|
|
1111
|
|
|
return false; |
1112
|
|
|
} |
1113
|
|
|
|
1114
|
|
|
// Note: interfaces can not declare properties. |
1115
|
|
|
$validScopes = array( |
1116
|
|
|
'T_CLASS' => true, |
1117
|
|
|
'T_ANON_CLASS' => true, |
1118
|
|
|
'T_TRAIT' => true, |
1119
|
|
|
); |
1120
|
|
|
if ($this->validDirectScope($phpcsFile, $stackPtr, $validScopes) === true) { |
1121
|
|
|
// Make sure it's not a method parameter. |
1122
|
|
|
if (empty($tokens[$stackPtr]['nested_parenthesis']) === true) { |
1123
|
|
|
return true; |
1124
|
|
|
} |
1125
|
|
|
} |
1126
|
|
|
|
1127
|
|
|
return false; |
1128
|
|
|
} |
1129
|
|
|
|
1130
|
|
|
|
1131
|
|
|
/** |
1132
|
|
|
* Check whether a T_CONST token is a class constant declaration. |
1133
|
|
|
* |
1134
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
1135
|
|
|
* @param int $stackPtr The position in the stack of the |
1136
|
|
|
* T_CONST token to verify. |
1137
|
|
|
* |
1138
|
|
|
* @return bool |
1139
|
|
|
*/ |
1140
|
|
|
public function isClassConstant(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
1141
|
|
|
{ |
1142
|
|
|
$tokens = $phpcsFile->getTokens(); |
1143
|
|
|
|
1144
|
|
View Code Duplication |
if (isset($tokens[$stackPtr]) === false || $tokens[$stackPtr]['code'] !== T_CONST) { |
|
|
|
|
1145
|
|
|
return false; |
1146
|
|
|
} |
1147
|
|
|
|
1148
|
|
|
// Note: traits can not declare constants. |
1149
|
|
|
$validScopes = array( |
1150
|
|
|
'T_CLASS' => true, |
1151
|
|
|
'T_ANON_CLASS' => true, |
1152
|
|
|
'T_INTERFACE' => true, |
1153
|
|
|
); |
1154
|
|
|
if ($this->validDirectScope($phpcsFile, $stackPtr, $validScopes) === true) { |
1155
|
|
|
return true; |
1156
|
|
|
} |
1157
|
|
|
|
1158
|
|
|
return false; |
1159
|
|
|
} |
1160
|
|
|
|
1161
|
|
|
|
1162
|
|
|
/** |
1163
|
|
|
* Check whether the direct wrapping scope of a token is within a limited set of |
1164
|
|
|
* acceptable tokens. |
1165
|
|
|
* |
1166
|
|
|
* Used to check, for instance, if a T_CONST is a class constant. |
1167
|
|
|
* |
1168
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
1169
|
|
|
* @param int $stackPtr The position in the stack of the |
1170
|
|
|
* T_CONST token to verify. |
1171
|
|
|
* @param array $validScopes Array of token types. |
1172
|
|
|
* Keys should be the token types in string |
1173
|
|
|
* format to allow for newer token types. |
1174
|
|
|
* Value is irrelevant. |
1175
|
|
|
* |
1176
|
|
|
* @return bool |
1177
|
|
|
*/ |
1178
|
|
|
protected function validDirectScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes) |
1179
|
|
|
{ |
1180
|
|
|
$tokens = $phpcsFile->getTokens(); |
1181
|
|
|
|
1182
|
|
|
if (empty($tokens[$stackPtr]['conditions']) === true) { |
1183
|
|
|
return false; |
1184
|
|
|
} |
1185
|
|
|
|
1186
|
|
|
/* |
1187
|
|
|
* Check only the direct wrapping scope of the token. |
1188
|
|
|
*/ |
1189
|
|
|
$conditions = array_keys($tokens[$stackPtr]['conditions']); |
1190
|
|
|
$ptr = array_pop($conditions); |
1191
|
|
|
|
1192
|
|
|
if (isset($tokens[$ptr]) === false) { |
1193
|
|
|
return false; |
1194
|
|
|
} |
1195
|
|
|
|
1196
|
|
|
if (isset($validScopes[$tokens[$ptr]['type']]) === true) { |
1197
|
|
|
return true; |
1198
|
|
|
} |
1199
|
|
|
|
1200
|
|
|
return false; |
1201
|
|
|
} |
1202
|
|
|
|
1203
|
|
|
|
1204
|
|
|
/** |
1205
|
|
|
* Get an array of just the type hints from a function declaration. |
1206
|
|
|
* |
1207
|
|
|
* Expects to be passed T_FUNCTION or T_CLOSURE token. |
1208
|
|
|
* |
1209
|
|
|
* Strips potential nullable indicator and potential global namespace |
1210
|
|
|
* indicator from the type hints before returning them. |
1211
|
|
|
* |
1212
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
1213
|
|
|
* @param int $stackPtr The position of the token. |
1214
|
|
|
* |
1215
|
|
|
* @return array Array with type hints or an empty array if |
1216
|
|
|
* - the function does not have any parameters |
1217
|
|
|
* - no type hints were found |
1218
|
|
|
* - or the passed token was not of the correct type. |
1219
|
|
|
*/ |
1220
|
|
|
public function getTypeHintsFromFunctionDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
1221
|
|
|
{ |
1222
|
|
|
$tokens = $phpcsFile->getTokens(); |
1223
|
|
|
|
1224
|
|
View Code Duplication |
if ($tokens[$stackPtr]['code'] !== T_FUNCTION && $tokens[$stackPtr]['code'] !== T_CLOSURE) { |
|
|
|
|
1225
|
|
|
return array(); |
1226
|
|
|
} |
1227
|
|
|
|
1228
|
|
|
$parameters = $this->getMethodParameters($phpcsFile, $stackPtr); |
1229
|
|
|
if (empty($parameters) || is_array($parameters) === false) { |
1230
|
|
|
return array(); |
1231
|
|
|
} |
1232
|
|
|
|
1233
|
|
|
$typeHints = array(); |
1234
|
|
|
|
1235
|
|
|
foreach ($parameters as $param) { |
1236
|
|
|
if ($param['type_hint'] === '') { |
1237
|
|
|
continue; |
1238
|
|
|
} |
1239
|
|
|
|
1240
|
|
|
// Strip off potential nullable indication. |
1241
|
|
|
$typeHint = ltrim($param['type_hint'], '?'); |
1242
|
|
|
|
1243
|
|
|
// Strip off potential (global) namespace indication. |
1244
|
|
|
$typeHint = ltrim($typeHint, '\\'); |
1245
|
|
|
|
1246
|
|
|
if ($typeHint !== '') { |
1247
|
|
|
$typeHints[] = $typeHint; |
1248
|
|
|
} |
1249
|
|
|
} |
1250
|
|
|
|
1251
|
|
|
return $typeHints; |
1252
|
|
|
} |
1253
|
|
|
|
1254
|
|
|
|
1255
|
|
|
/** |
1256
|
|
|
* Returns the method parameters for the specified function token. |
1257
|
|
|
* |
1258
|
|
|
* Each parameter is in the following format: |
1259
|
|
|
* |
1260
|
|
|
* <code> |
1261
|
|
|
* 0 => array( |
1262
|
|
|
* 'token' => int, // The position of the var in the token stack. |
1263
|
|
|
* 'name' => '$var', // The variable name. |
1264
|
|
|
* 'content' => string, // The full content of the variable definition. |
1265
|
|
|
* 'pass_by_reference' => boolean, // Is the variable passed by reference? |
1266
|
|
|
* 'variable_length' => boolean, // Is the param of variable length through use of `...` ? |
1267
|
|
|
* 'type_hint' => string, // The type hint for the variable. |
1268
|
|
|
* 'nullable_type' => boolean, // Is the variable using a nullable type? |
1269
|
|
|
* ) |
1270
|
|
|
* </code> |
1271
|
|
|
* |
1272
|
|
|
* Parameters with default values have an additional array index of |
1273
|
|
|
* 'default' with the value of the default as a string. |
1274
|
|
|
* |
1275
|
|
|
* {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
1276
|
|
|
* class, but with some improvements which have been introduced in |
1277
|
|
|
* PHPCS 2.8.0. |
1278
|
|
|
* {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1117}, |
1279
|
|
|
* {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} and |
1280
|
|
|
* {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1293}. |
1281
|
|
|
* |
1282
|
|
|
* Once the minimum supported PHPCS version for this standard goes beyond |
1283
|
|
|
* that, this method can be removed and calls to it replaced with |
1284
|
|
|
* `$phpcsFile->getMethodParameters($stackPtr)` calls. |
1285
|
|
|
* |
1286
|
|
|
* NOTE: This version does not deal with the new T_NULLABLE token type. |
1287
|
|
|
* This token is included upstream only in 2.8.0+ and as we defer to upstream |
1288
|
|
|
* in that case, no need to deal with it here. |
1289
|
|
|
* |
1290
|
|
|
* Last synced with PHPCS version: PHPCS 2.9.0-alpha at commit f1511adad043edfd6d2e595e77385c32577eb2bc}} |
1291
|
|
|
* |
1292
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
1293
|
|
|
* @param int $stackPtr The position in the stack of the |
1294
|
|
|
* function token to acquire the |
1295
|
|
|
* parameters for. |
1296
|
|
|
* |
1297
|
|
|
* @return array|false |
1298
|
|
|
* @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
1299
|
|
|
* type T_FUNCTION or T_CLOSURE. |
1300
|
|
|
*/ |
1301
|
|
|
public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
1302
|
|
|
{ |
1303
|
|
|
if (version_compare(PHP_CodeSniffer::VERSION, '2.7.1', '>') === true) { |
1304
|
|
|
return $phpcsFile->getMethodParameters($stackPtr); |
1305
|
|
|
} |
1306
|
|
|
|
1307
|
|
|
$tokens = $phpcsFile->getTokens(); |
1308
|
|
|
|
1309
|
|
|
// Check for the existence of the token. |
1310
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
1311
|
|
|
return false; |
1312
|
|
|
} |
1313
|
|
|
|
1314
|
|
View Code Duplication |
if ($tokens[$stackPtr]['code'] !== T_FUNCTION && $tokens[$stackPtr]['code'] !== T_CLOSURE) { |
|
|
|
|
1315
|
|
|
throw new PHP_CodeSniffer_Exception('$stackPtr must be of type T_FUNCTION or T_CLOSURE'); |
1316
|
|
|
} |
1317
|
|
|
|
1318
|
|
|
$opener = $tokens[$stackPtr]['parenthesis_opener']; |
1319
|
|
|
$closer = $tokens[$stackPtr]['parenthesis_closer']; |
1320
|
|
|
|
1321
|
|
|
$vars = array(); |
1322
|
|
|
$currVar = null; |
1323
|
|
|
$paramStart = ($opener + 1); |
1324
|
|
|
$defaultStart = null; |
1325
|
|
|
$paramCount = 0; |
1326
|
|
|
$passByReference = false; |
1327
|
|
|
$variableLength = false; |
1328
|
|
|
$typeHint = ''; |
1329
|
|
|
$nullableType = false; |
1330
|
|
|
|
1331
|
|
|
for ($i = $paramStart; $i <= $closer; $i++) { |
1332
|
|
|
// Check to see if this token has a parenthesis or bracket opener. If it does |
1333
|
|
|
// it's likely to be an array which might have arguments in it. This |
1334
|
|
|
// could cause problems in our parsing below, so lets just skip to the |
1335
|
|
|
// end of it. |
1336
|
|
View Code Duplication |
if (isset($tokens[$i]['parenthesis_opener']) === true) { |
|
|
|
|
1337
|
|
|
// Don't do this if it's the close parenthesis for the method. |
1338
|
|
|
if ($i !== $tokens[$i]['parenthesis_closer']) { |
1339
|
|
|
$i = ($tokens[$i]['parenthesis_closer'] + 1); |
1340
|
|
|
} |
1341
|
|
|
} |
1342
|
|
|
|
1343
|
|
View Code Duplication |
if (isset($tokens[$i]['bracket_opener']) === true) { |
|
|
|
|
1344
|
|
|
// Don't do this if it's the close parenthesis for the method. |
1345
|
|
|
if ($i !== $tokens[$i]['bracket_closer']) { |
1346
|
|
|
$i = ($tokens[$i]['bracket_closer'] + 1); |
1347
|
|
|
} |
1348
|
|
|
} |
1349
|
|
|
|
1350
|
|
|
switch ($tokens[$i]['type']) { |
1351
|
|
|
case 'T_BITWISE_AND': |
1352
|
|
|
$passByReference = true; |
1353
|
|
|
break; |
1354
|
|
|
case 'T_VARIABLE': |
1355
|
|
|
$currVar = $i; |
1356
|
|
|
break; |
1357
|
|
|
case 'T_ELLIPSIS': |
1358
|
|
|
$variableLength = true; |
1359
|
|
|
break; |
1360
|
|
|
case 'T_ARRAY_HINT': |
1361
|
|
|
case 'T_CALLABLE': |
1362
|
|
|
$typeHint .= $tokens[$i]['content']; |
1363
|
|
|
break; |
1364
|
|
|
case 'T_SELF': |
1365
|
|
|
case 'T_PARENT': |
1366
|
|
|
case 'T_STATIC': |
1367
|
|
|
// Self is valid, the others invalid, but were probably intended as type hints. |
1368
|
|
|
if (isset($defaultStart) === false) { |
1369
|
|
|
$typeHint .= $tokens[$i]['content']; |
1370
|
|
|
} |
1371
|
|
|
break; |
1372
|
|
|
case 'T_STRING': |
1373
|
|
|
// This is a string, so it may be a type hint, but it could |
1374
|
|
|
// also be a constant used as a default value. |
1375
|
|
|
$prevComma = false; |
1376
|
|
View Code Duplication |
for ($t = $i; $t >= $opener; $t--) { |
|
|
|
|
1377
|
|
|
if ($tokens[$t]['code'] === T_COMMA) { |
1378
|
|
|
$prevComma = $t; |
1379
|
|
|
break; |
1380
|
|
|
} |
1381
|
|
|
} |
1382
|
|
|
|
1383
|
|
|
if ($prevComma !== false) { |
1384
|
|
|
$nextEquals = false; |
1385
|
|
View Code Duplication |
for ($t = $prevComma; $t < $i; $t++) { |
|
|
|
|
1386
|
|
|
if ($tokens[$t]['code'] === T_EQUAL) { |
1387
|
|
|
$nextEquals = $t; |
1388
|
|
|
break; |
1389
|
|
|
} |
1390
|
|
|
} |
1391
|
|
|
|
1392
|
|
|
if ($nextEquals !== false) { |
1393
|
|
|
break; |
1394
|
|
|
} |
1395
|
|
|
} |
1396
|
|
|
|
1397
|
|
|
if ($defaultStart === null) { |
1398
|
|
|
$typeHint .= $tokens[$i]['content']; |
1399
|
|
|
} |
1400
|
|
|
break; |
1401
|
|
|
case 'T_NS_SEPARATOR': |
1402
|
|
|
// Part of a type hint or default value. |
1403
|
|
|
if ($defaultStart === null) { |
1404
|
|
|
$typeHint .= $tokens[$i]['content']; |
1405
|
|
|
} |
1406
|
|
|
break; |
1407
|
|
|
case 'T_INLINE_THEN': |
1408
|
|
|
if ($defaultStart === null) { |
1409
|
|
|
$nullableType = true; |
1410
|
|
|
$typeHint .= $tokens[$i]['content']; |
1411
|
|
|
} |
1412
|
|
|
break; |
1413
|
|
|
case 'T_CLOSE_PARENTHESIS': |
1414
|
|
|
case 'T_COMMA': |
1415
|
|
|
// If it's null, then there must be no parameters for this |
1416
|
|
|
// method. |
1417
|
|
|
if ($currVar === null) { |
1418
|
|
|
continue; |
1419
|
|
|
} |
1420
|
|
|
|
1421
|
|
|
$vars[$paramCount] = array(); |
1422
|
|
|
$vars[$paramCount]['token'] = $currVar; |
1423
|
|
|
$vars[$paramCount]['name'] = $tokens[$currVar]['content']; |
1424
|
|
|
$vars[$paramCount]['content'] = trim($phpcsFile->getTokensAsString($paramStart, ($i - $paramStart))); |
1425
|
|
|
|
1426
|
|
|
if ($defaultStart !== null) { |
1427
|
|
|
$vars[$paramCount]['default'] |
1428
|
|
|
= trim($phpcsFile->getTokensAsString( |
1429
|
|
|
$defaultStart, |
1430
|
|
|
($i - $defaultStart) |
1431
|
|
|
)); |
1432
|
|
|
} |
1433
|
|
|
|
1434
|
|
|
$vars[$paramCount]['pass_by_reference'] = $passByReference; |
1435
|
|
|
$vars[$paramCount]['variable_length'] = $variableLength; |
1436
|
|
|
$vars[$paramCount]['type_hint'] = $typeHint; |
1437
|
|
|
$vars[$paramCount]['nullable_type'] = $nullableType; |
1438
|
|
|
|
1439
|
|
|
// Reset the vars, as we are about to process the next parameter. |
1440
|
|
|
$defaultStart = null; |
1441
|
|
|
$paramStart = ($i + 1); |
1442
|
|
|
$passByReference = false; |
1443
|
|
|
$variableLength = false; |
1444
|
|
|
$typeHint = ''; |
1445
|
|
|
$nullableType = false; |
1446
|
|
|
|
1447
|
|
|
$paramCount++; |
1448
|
|
|
break; |
1449
|
|
|
case 'T_EQUAL': |
1450
|
|
|
$defaultStart = ($i + 1); |
1451
|
|
|
break; |
1452
|
|
|
}//end switch |
1453
|
|
|
}//end for |
1454
|
|
|
|
1455
|
|
|
return $vars; |
1456
|
|
|
|
1457
|
|
|
}//end getMethodParameters() |
1458
|
|
|
|
1459
|
|
|
|
1460
|
|
|
/** |
1461
|
|
|
* Returns the name of the class that the specified class extends. |
1462
|
|
|
* |
1463
|
|
|
* Returns FALSE on error or if there is no extended class name. |
1464
|
|
|
* |
1465
|
|
|
* {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
1466
|
|
|
* class, but with some improvements which have been introduced in |
1467
|
|
|
* PHPCS 2.8.0. |
1468
|
|
|
* {@link https://github.com/squizlabs/PHP_CodeSniffer/commit/0011d448119d4c568e3ac1f825ae78815bf2cc34}. |
1469
|
|
|
* |
1470
|
|
|
* Once the minimum supported PHPCS version for this standard goes beyond |
1471
|
|
|
* that, this method can be removed and calls to it replaced with |
1472
|
|
|
* `$phpcsFile->findExtendedClassName($stackPtr)` calls. |
1473
|
|
|
* |
1474
|
|
|
* Last synced with PHPCS version: PHPCS 2.9.0 at commit b940fb7dca8c2a37f0514161b495363e5b36d879}} |
1475
|
|
|
* |
1476
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
1477
|
|
|
* @param int $stackPtr The position in the stack of the |
1478
|
|
|
* class token. |
1479
|
|
|
* |
1480
|
|
|
* @return string|false |
1481
|
|
|
*/ |
1482
|
|
|
public function findExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
1483
|
|
|
{ |
1484
|
|
|
if (version_compare(PHP_CodeSniffer::VERSION, '2.7.1', '>') === true) { |
1485
|
|
|
return $phpcsFile->findExtendedClassName($stackPtr); |
1486
|
|
|
} |
1487
|
|
|
|
1488
|
|
|
$tokens = $phpcsFile->getTokens(); |
1489
|
|
|
|
1490
|
|
|
// Check for the existence of the token. |
1491
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
1492
|
|
|
return false; |
1493
|
|
|
} |
1494
|
|
|
|
1495
|
|
View Code Duplication |
if ($tokens[$stackPtr]['code'] !== T_CLASS |
|
|
|
|
1496
|
|
|
&& $tokens[$stackPtr]['code'] !== T_ANON_CLASS |
1497
|
|
|
) { |
1498
|
|
|
return false; |
1499
|
|
|
} |
1500
|
|
|
|
1501
|
|
|
if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
1502
|
|
|
return false; |
1503
|
|
|
} |
1504
|
|
|
|
1505
|
|
|
$classCloserIndex = $tokens[$stackPtr]['scope_closer']; |
1506
|
|
|
$extendsIndex = $phpcsFile->findNext(T_EXTENDS, $stackPtr, $classCloserIndex); |
1507
|
|
|
if (false === $extendsIndex) { |
1508
|
|
|
return false; |
1509
|
|
|
} |
1510
|
|
|
|
1511
|
|
|
$find = array( |
1512
|
|
|
T_NS_SEPARATOR, |
1513
|
|
|
T_STRING, |
1514
|
|
|
T_WHITESPACE, |
1515
|
|
|
); |
1516
|
|
|
|
1517
|
|
|
$end = $phpcsFile->findNext($find, ($extendsIndex + 1), $classCloserIndex, true); |
1518
|
|
|
$name = $phpcsFile->getTokensAsString(($extendsIndex + 1), ($end - $extendsIndex - 1)); |
1519
|
|
|
$name = trim($name); |
1520
|
|
|
|
1521
|
|
|
if ($name === '') { |
1522
|
|
|
return false; |
1523
|
|
|
} |
1524
|
|
|
|
1525
|
|
|
return $name; |
1526
|
|
|
|
1527
|
|
|
}//end findExtendedClassName() |
1528
|
|
|
|
1529
|
|
|
|
1530
|
|
|
/** |
1531
|
|
|
* Get the hash algorithm name from the parameter in a hash function call. |
1532
|
|
|
* |
1533
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
1534
|
|
|
* @param int $stackPtr The position of the T_STRING function token. |
1535
|
|
|
* |
1536
|
|
|
* @return string|false The algorithm name without quotes if this was a relevant hash |
1537
|
|
|
* function call or false if it was not. |
1538
|
|
|
*/ |
1539
|
|
|
public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
1540
|
|
|
{ |
1541
|
|
|
$tokens = $phpcsFile->getTokens(); |
1542
|
|
|
|
1543
|
|
|
// Check for the existence of the token. |
1544
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
1545
|
|
|
return false; |
1546
|
|
|
} |
1547
|
|
|
|
1548
|
|
|
if ($tokens[$stackPtr]['code'] !== T_STRING) { |
1549
|
|
|
return false; |
1550
|
|
|
} |
1551
|
|
|
|
1552
|
|
|
$functionName = $tokens[$stackPtr]['content']; |
1553
|
|
|
$functionNameLc = strtolower($functionName); |
1554
|
|
|
|
1555
|
|
|
// Bow out if not one of the functions we're targetting. |
1556
|
|
|
if (isset($this->hashAlgoFunctions[$functionNameLc]) === false) { |
1557
|
|
|
return false; |
1558
|
|
|
} |
1559
|
|
|
|
1560
|
|
|
// Get the parameter from the function call which should contain the algorithm name. |
1561
|
|
|
$algoParam = $this->getFunctionCallParameter($phpcsFile, $stackPtr, $this->hashAlgoFunctions[$functionNameLc]); |
1562
|
|
|
if ($algoParam === false) { |
1563
|
|
|
return false; |
1564
|
|
|
} |
1565
|
|
|
|
1566
|
|
|
// Algorithm is a text string, so we need to remove the quotes. |
1567
|
|
|
$algo = strtolower(trim($algoParam['raw'])); |
1568
|
|
|
$algo = $this->stripQuotes($algo); |
1569
|
|
|
|
1570
|
|
|
return $algo; |
1571
|
|
|
} |
1572
|
|
|
|
1573
|
|
|
}//end class |
1574
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.