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