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