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 |
||
0 ignored issues
–
show
|
|||
23 | { |
||
24 | |||
25 | /* The testVersion configuration variable may be in any of the following formats: |
||
26 | * 1) Omitted/empty, in which case no version is specified. This effectively |
||
27 | * disables all the checks provided by this standard. |
||
28 | * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
||
29 | * the code will run on that version of PHP (no deprecated features or newer |
||
30 | * features being used). |
||
31 | * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
||
32 | * on all PHP versions in that range, and that it doesn't use any features that |
||
33 | * were deprecated by the final version in the list, or which were not available |
||
34 | * for the first version in the list. |
||
35 | * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
||
36 | * would be treated as invalid, and ignored. |
||
37 | * This standard doesn't support checking against PHP4, so the minimum version that |
||
38 | * is recognised is "5.0". |
||
39 | */ |
||
40 | |||
41 | private function getTestVersion() |
||
42 | { |
||
43 | /** |
||
44 | * var $arrTestVersions will hold an array containing min/max version of PHP |
||
45 | * that we are checking against (see above). If only a single version |
||
46 | * number is specified, then this is used as both the min and max. |
||
47 | */ |
||
48 | static $arrTestVersions = array(); |
||
49 | |||
50 | $testVersion = trim(PHP_CodeSniffer::getConfigData('testVersion')); |
||
51 | |||
52 | if (!isset($arrTestVersions[$testVersion]) && !empty($testVersion)) { |
||
53 | |||
54 | $arrTestVersions[$testVersion] = array(null, null); |
||
55 | if (preg_match('/^\d+\.\d+$/', $testVersion)) { |
||
56 | $arrTestVersions[$testVersion] = array($testVersion, $testVersion); |
||
57 | } |
||
58 | elseif (preg_match('/^(\d+\.\d+)\s*-\s*(\d+\.\d+)$/', $testVersion, |
||
59 | $matches)) |
||
60 | { |
||
61 | if (version_compare($matches[1], $matches[2], '>')) { |
||
62 | trigger_error("Invalid range in testVersion setting: '" |
||
63 | . $testVersion . "'", E_USER_WARNING); |
||
64 | } |
||
65 | else { |
||
66 | $arrTestVersions[$testVersion] = array($matches[1], $matches[2]); |
||
67 | } |
||
68 | } |
||
69 | elseif (!$testVersion == '') { |
||
70 | trigger_error("Invalid testVersion setting: '" . $testVersion |
||
71 | . "'", E_USER_WARNING); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | if (isset($arrTestVersions[$testVersion])) { |
||
76 | return $arrTestVersions[$testVersion]; |
||
77 | } |
||
78 | else { |
||
79 | return array(null, null); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | View Code Duplication | public function supportsAbove($phpVersion) |
|
0 ignored issues
–
show
This method seems to be duplicated in 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. ![]() |
|||
84 | { |
||
85 | $testVersion = $this->getTestVersion(); |
||
86 | $testVersion = $testVersion[1]; |
||
87 | |||
88 | if (is_null($testVersion) |
||
89 | || version_compare($testVersion, $phpVersion) >= 0 |
||
90 | ) { |
||
91 | return true; |
||
92 | } else { |
||
93 | return false; |
||
94 | } |
||
95 | }//end supportsAbove() |
||
96 | |||
97 | View Code Duplication | public function supportsBelow($phpVersion) |
|
0 ignored issues
–
show
This method seems to be duplicated in 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. ![]() |
|||
98 | { |
||
99 | $testVersion = $this->getTestVersion(); |
||
100 | $testVersion = $testVersion[0]; |
||
101 | |||
102 | if (!is_null($testVersion) |
||
103 | && version_compare($testVersion, $phpVersion) <= 0 |
||
104 | ) { |
||
105 | return true; |
||
106 | } else { |
||
107 | return false; |
||
108 | } |
||
109 | }//end supportsBelow() |
||
110 | |||
111 | /** |
||
112 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
113 | * |
||
114 | * Returns FALSE on error or if there are no implemented interface names. |
||
115 | * |
||
116 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
117 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
118 | * that, this method can be removed and call to it replaced with |
||
119 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
120 | * |
||
121 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
122 | * @param int $stackPtr The position of the class token. |
||
123 | * |
||
124 | * @return array|false |
||
125 | */ |
||
126 | public function findImplementedInterfaceNames($phpcsFile, $stackPtr) |
||
127 | { |
||
128 | $tokens = $phpcsFile->getTokens(); |
||
129 | |||
130 | // Check for the existence of the token. |
||
131 | if (isset($tokens[$stackPtr]) === false) { |
||
132 | return false; |
||
133 | } |
||
134 | |||
135 | if ($tokens[$stackPtr]['code'] !== T_CLASS) { |
||
136 | return false; |
||
137 | } |
||
138 | |||
139 | if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
||
140 | return false; |
||
141 | } |
||
142 | |||
143 | $classOpenerIndex = $tokens[$stackPtr]['scope_opener']; |
||
144 | $implementsIndex = $phpcsFile->findNext(T_IMPLEMENTS, $stackPtr, $classOpenerIndex); |
||
145 | if ($implementsIndex === false) { |
||
146 | return false; |
||
147 | } |
||
148 | |||
149 | $find = array( |
||
150 | T_NS_SEPARATOR, |
||
151 | T_STRING, |
||
152 | T_WHITESPACE, |
||
153 | T_COMMA, |
||
154 | ); |
||
155 | |||
156 | $end = $phpcsFile->findNext($find, ($implementsIndex + 1), ($classOpenerIndex + 1), true); |
||
157 | $name = $phpcsFile->getTokensAsString(($implementsIndex + 1), ($end - $implementsIndex - 1)); |
||
158 | $name = trim($name); |
||
159 | |||
160 | if ($name === '') { |
||
161 | return false; |
||
162 | } else { |
||
163 | $names = explode(',', $name); |
||
164 | $names = array_map('trim', $names); |
||
165 | return $names; |
||
166 | } |
||
167 | |||
168 | }//end findImplementedInterfaceNames() |
||
169 | |||
170 | |||
171 | /** |
||
172 | * Checks if a function call has parameters. |
||
173 | * |
||
174 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
175 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
176 | * |
||
177 | * @link https://github.com/wimg/PHPCompatibility/issues/120 |
||
178 | * @link https://github.com/wimg/PHPCompatibility/issues/152 |
||
179 | * |
||
180 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
181 | * @param int $stackPtr The position of the function call token. |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
186 | { |
||
187 | $tokens = $phpcsFile->getTokens(); |
||
188 | |||
189 | // Check for the existence of the token. |
||
190 | if (isset($tokens[$stackPtr]) === false) { |
||
191 | return false; |
||
192 | } |
||
193 | |||
194 | if ($tokens[$stackPtr]['code'] !== T_STRING) { |
||
195 | return false; |
||
196 | } |
||
197 | |||
198 | // Next non-empty token should be the open parenthesis. |
||
199 | $openParenthesis = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
||
200 | if ($openParenthesis === false || $tokens[$openParenthesis]['code'] !== T_OPEN_PARENTHESIS) { |
||
201 | return false; |
||
202 | } |
||
203 | |||
204 | if (isset($tokens[$openParenthesis]['parenthesis_closer']) === false) { |
||
205 | return false; |
||
206 | } |
||
207 | |||
208 | $closeParenthesis = $tokens[$openParenthesis]['parenthesis_closer']; |
||
209 | $nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $openParenthesis + 1, $closeParenthesis + 1, true); |
||
210 | |||
211 | if ($nextNonEmpty === $closeParenthesis) { |
||
212 | // No parameters. |
||
213 | return false; |
||
214 | } |
||
215 | |||
216 | return true; |
||
217 | } |
||
218 | |||
219 | |||
220 | /** |
||
221 | * Count the number of parameters a function call has been passed. |
||
222 | * |
||
223 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
224 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
225 | * |
||
226 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
227 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
228 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
229 | * |
||
230 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
231 | * @param int $stackPtr The position of the function call token. |
||
232 | * |
||
233 | * @return int |
||
234 | */ |
||
235 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
236 | { |
||
237 | if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) { |
||
238 | return 0; |
||
239 | } |
||
240 | |||
241 | // Ok, we know we have a T_STRING with parameters and valid open & close parenthesis. |
||
242 | $tokens = $phpcsFile->getTokens(); |
||
243 | |||
244 | $openParenthesis = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
||
245 | $closeParenthesis = $tokens[$openParenthesis]['parenthesis_closer']; |
||
246 | |||
247 | // Which nesting level is the one we are interested in ? |
||
248 | $nestedParenthesisCount = 1; |
||
249 | if (isset($tokens[$openParenthesis]['nested_parenthesis'])) { |
||
250 | $nestedParenthesisCount = count($tokens[$openParenthesis]['nested_parenthesis']) + 1; |
||
251 | } |
||
252 | |||
253 | $nextComma = $openParenthesis; |
||
254 | $cnt = 0; |
||
255 | while ($nextComma = $phpcsFile->findNext(array(T_COMMA, T_CLOSE_PARENTHESIS), $nextComma + 1, $closeParenthesis + 1)) { |
||
256 | // Ignore comma's at a lower nesting level. |
||
257 | if ( |
||
258 | $tokens[$nextComma]['type'] == 'T_COMMA' |
||
259 | && |
||
260 | isset($tokens[$nextComma]['nested_parenthesis']) |
||
261 | && |
||
262 | count($tokens[$nextComma]['nested_parenthesis']) != $nestedParenthesisCount |
||
263 | ) { |
||
264 | continue; |
||
265 | } |
||
266 | |||
267 | // Ignore closing parenthesis if not 'ours'. |
||
268 | if ($tokens[$nextComma]['type'] == 'T_CLOSE_PARENTHESIS' && $nextComma != $closeParenthesis) { |
||
269 | continue; |
||
270 | } |
||
271 | |||
272 | $cnt++; |
||
273 | } |
||
274 | |||
275 | return $cnt; |
||
276 | } |
||
277 | |||
278 | |||
279 | /** |
||
280 | * Verify whether a token is within a class scope. |
||
281 | * |
||
282 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
283 | * @param int $stackPtr The position of the token. |
||
284 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
285 | * scope or also accept interfaces and traits |
||
286 | * as scope. |
||
287 | * |
||
288 | * @return bool True if within class scope, false otherwise. |
||
289 | */ |
||
290 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
291 | { |
||
292 | $tokens = $phpcsFile->getTokens(); |
||
293 | |||
294 | // Check for the existence of the token. |
||
295 | if (isset($tokens[$stackPtr]) === false) { |
||
296 | return false; |
||
297 | } |
||
298 | |||
299 | // No conditions = no scope. |
||
300 | if (empty($tokens[$stackPtr]['conditions'])) { |
||
301 | return false; |
||
302 | } |
||
303 | |||
304 | $validScope = array(T_CLASS); |
||
305 | if ($strict === false) { |
||
306 | $validScope[] = T_INTERFACE; |
||
307 | $validScope[] = T_TRAIT; |
||
308 | } |
||
309 | |||
310 | // Check for class scope. |
||
311 | foreach ($tokens[$stackPtr]['conditions'] as $pointer => $type) { |
||
312 | if (in_array($type, $validScope, true)) { |
||
313 | return true; |
||
314 | } |
||
315 | } |
||
316 | |||
317 | return false; |
||
318 | } |
||
319 | |||
320 | |||
321 | /** |
||
322 | * Returns the fully qualified class name for a new class instantiation. |
||
323 | * |
||
324 | * Returns an empty string if the class name could not be reliably inferred. |
||
325 | * |
||
326 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
327 | * @param int $stackPtr The position of a T_NEW token. |
||
328 | * |
||
329 | * @return string |
||
330 | */ |
||
331 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
332 | { |
||
333 | $tokens = $phpcsFile->getTokens(); |
||
334 | |||
335 | // Check for the existence of the token. |
||
336 | if (isset($tokens[$stackPtr]) === false) { |
||
337 | return ''; |
||
338 | } |
||
339 | |||
340 | if ($tokens[$stackPtr]['code'] !== T_NEW) { |
||
341 | return ''; |
||
342 | } |
||
343 | |||
344 | $find = array( |
||
345 | T_NS_SEPARATOR, |
||
346 | T_STRING, |
||
347 | T_NAMESPACE, |
||
348 | T_WHITESPACE, |
||
349 | ); |
||
350 | |||
351 | $start = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
||
352 | $end = $phpcsFile->findNext($find, ($start + 1), null, true, null, true); |
||
353 | $className = $phpcsFile->getTokensAsString($start, ($end - $start)); |
||
354 | $className = trim($className); |
||
355 | |||
356 | return $this->getFQName($phpcsFile, $stackPtr, $className); |
||
357 | } |
||
358 | |||
359 | |||
360 | /** |
||
361 | * Returns the fully qualified name of the class that the specified class extends. |
||
362 | * |
||
363 | * Returns an empty string if the class does not extend another class or if |
||
364 | * the class name could not be reliably inferred. |
||
365 | * |
||
366 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
367 | * @param int $stackPtr The position of a T_CLASS token. |
||
368 | * |
||
369 | * @return string |
||
370 | */ |
||
371 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
372 | { |
||
373 | $tokens = $phpcsFile->getTokens(); |
||
374 | |||
375 | // Check for the existence of the token. |
||
376 | if (isset($tokens[$stackPtr]) === false) { |
||
377 | return ''; |
||
378 | } |
||
379 | |||
380 | if ($tokens[$stackPtr]['code'] !== T_CLASS) { |
||
381 | return ''; |
||
382 | } |
||
383 | |||
384 | $extends = $phpcsFile->findExtendedClassName($stackPtr); |
||
385 | if (empty($extends) || is_string($extends) === false) { |
||
386 | return ''; |
||
387 | } |
||
388 | |||
389 | return $this->getFQName($phpcsFile, $stackPtr, $extends); |
||
390 | } |
||
391 | |||
392 | |||
393 | /** |
||
394 | * Returns the class name for the static usage of a class. |
||
395 | * This can be a call to a method, the use of a property or constant. |
||
396 | * |
||
397 | * Returns an empty string if the class name could not be reliably inferred. |
||
398 | * |
||
399 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
400 | * @param int $stackPtr The position of a T_NEW token. |
||
401 | * |
||
402 | * @return string |
||
403 | */ |
||
404 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
405 | { |
||
406 | $tokens = $phpcsFile->getTokens(); |
||
407 | |||
408 | // Check for the existence of the token. |
||
409 | if (isset($tokens[$stackPtr]) === false) { |
||
410 | return ''; |
||
411 | } |
||
412 | |||
413 | if ($tokens[$stackPtr]['code'] !== T_DOUBLE_COLON) { |
||
414 | return ''; |
||
415 | } |
||
416 | |||
417 | // Nothing to do if 'parent' or 'static' as we don't know how far the class tree extends. |
||
418 | if (in_array($tokens[$stackPtr - 1]['code'], array(T_PARENT, T_STATIC), true)) { |
||
419 | return ''; |
||
420 | } |
||
421 | |||
422 | // Get the classname from the class declaration if self is used. |
||
423 | if ($tokens[$stackPtr - 1]['code'] === T_SELF) { |
||
424 | $classDeclarationPtr = $phpcsFile->findPrevious(T_CLASS, $stackPtr - 1); |
||
425 | if ($classDeclarationPtr === false) { |
||
426 | return ''; |
||
427 | } |
||
428 | $className = $phpcsFile->getDeclarationName($classDeclarationPtr); |
||
429 | return $this->getFQName($phpcsFile, $classDeclarationPtr, $className); |
||
430 | } |
||
431 | |||
432 | $find = array( |
||
433 | T_NS_SEPARATOR, |
||
434 | T_STRING, |
||
435 | T_NAMESPACE, |
||
436 | T_WHITESPACE, |
||
437 | ); |
||
438 | |||
439 | $start = ($phpcsFile->findPrevious($find, $stackPtr - 1, null, true, null, true) + 1); |
||
440 | $className = $phpcsFile->getTokensAsString($start, ($stackPtr - $start)); |
||
441 | $className = trim($className); |
||
442 | |||
443 | return $this->getFQName($phpcsFile, $stackPtr, $className); |
||
444 | } |
||
445 | |||
446 | |||
447 | /** |
||
448 | * Get the Fully Qualified name for a class/function/constant etc. |
||
449 | * |
||
450 | * Checks if a class/function/constant name is already fully qualified and |
||
451 | * if not, enrich it with the relevant namespace information. |
||
452 | * |
||
453 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
454 | * @param int $stackPtr The position of the token. |
||
455 | * @param string $name The class / function / constant name. |
||
456 | * |
||
457 | * @return string |
||
458 | */ |
||
459 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
460 | { |
||
461 | if (strpos($name, '\\' ) === 0) { |
||
462 | // Already fully qualified. |
||
463 | return $name; |
||
464 | } |
||
465 | |||
466 | // Remove the namespace keyword if used. |
||
467 | if (strpos($name, 'namespace\\') === 0) { |
||
468 | $name = substr($name, 10); |
||
469 | } |
||
470 | |||
471 | $namespace = $this->determineNamespace($phpcsFile, $stackPtr); |
||
472 | |||
473 | if ($namespace === '') { |
||
474 | return '\\' . $name; |
||
475 | } |
||
476 | else { |
||
477 | return '\\' . $namespace . '\\' . $name; |
||
478 | } |
||
479 | } |
||
480 | |||
481 | |||
482 | /** |
||
483 | * Is the class/function/constant name namespaced or global ? |
||
484 | * |
||
485 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
486 | * I.e. should always start with a `\` ! |
||
487 | * |
||
488 | * @return bool True if namespaced, false if global. |
||
489 | */ |
||
490 | public function isNamespaced($FQName) { |
||
491 | if (strpos($FQName, '\\') !== 0) { |
||
492 | throw new PHP_CodeSniffer_Exception('$FQName must be a fully qualified name'); |
||
493 | } |
||
494 | |||
495 | return (strpos(substr($FQName, 1), '\\') !== false); |
||
496 | } |
||
497 | |||
498 | |||
499 | /** |
||
500 | * Determine the namespace name an arbitrary token lives in. |
||
501 | * |
||
502 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
503 | * @param int $stackPtr The token position for which to determine the namespace. |
||
504 | * |
||
505 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
506 | */ |
||
507 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
508 | { |
||
509 | $tokens = $phpcsFile->getTokens(); |
||
510 | |||
511 | // Check for the existence of the token. |
||
512 | if (isset($tokens[$stackPtr]) === false) { |
||
513 | return ''; |
||
514 | } |
||
515 | |||
516 | // Check for scoped namespace {}. |
||
517 | if (empty($tokens[$stackPtr]['conditions']) === false) { |
||
518 | foreach ($tokens[$stackPtr]['conditions'] as $pointer => $type) { |
||
519 | if ($type === T_NAMESPACE) { |
||
520 | $namespace = $this->getDeclaredNamespaceName($phpcsFile, $pointer); |
||
521 | if ($namespace !== false) { |
||
522 | return $namespace; |
||
523 | } |
||
524 | } |
||
525 | break; // We only need to check the highest level condition. |
||
526 | } |
||
527 | } |
||
528 | |||
529 | /* |
||
530 | * Not in a scoped namespace, so let's see if we can find a non-scoped namespace instead. |
||
531 | * Keeping in mind that: |
||
532 | * - there can be multiple non-scoped namespaces in a file (bad practice, but it happens). |
||
533 | * - the namespace keyword can also be used as part of a function/method call and such. |
||
534 | * - that a non-named namespace resolves to the global namespace. |
||
535 | */ |
||
536 | $previousNSToken = $stackPtr; |
||
537 | $namespace = false; |
||
538 | do { |
||
539 | $previousNSToken = $phpcsFile->findPrevious(T_NAMESPACE, $previousNSToken -1); |
||
540 | |||
541 | // Stop if we encounter a scoped namespace declaration as we already know we're not in one. |
||
542 | if (empty($tokens[$previousNSToken]['scope_condition']) === false && $tokens[$previousNSToken]['scope_condition'] = $previousNSToken) { |
||
543 | break; |
||
544 | } |
||
545 | $namespace = $this->getDeclaredNamespaceName($phpcsFile, $previousNSToken); |
||
546 | |||
547 | } while ($namespace === false && $previousNSToken !== false); |
||
548 | |||
549 | // If we still haven't got a namespace, return an empty string. |
||
550 | if ($namespace === false) { |
||
551 | return ''; |
||
552 | } |
||
553 | else { |
||
554 | return $namespace; |
||
555 | } |
||
556 | } |
||
557 | |||
558 | /** |
||
559 | * Get the complete namespace name for a namespace declaration. |
||
560 | * |
||
561 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
562 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
563 | * |
||
564 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
565 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
566 | * |
||
567 | * @return string|false Namespace name or false if not a namespace declaration. |
||
568 | * Namespace name can be an empty string for global namespace declaration. |
||
569 | */ |
||
570 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
||
571 | { |
||
572 | $tokens = $phpcsFile->getTokens(); |
||
573 | |||
574 | // Check for the existence of the token. |
||
575 | if ($stackPtr === false || isset($tokens[$stackPtr]) === false) { |
||
576 | return false; |
||
577 | } |
||
578 | |||
579 | if ($tokens[$stackPtr]['code'] !== T_NAMESPACE) { |
||
580 | return false; |
||
581 | } |
||
582 | |||
583 | if ($tokens[$stackPtr + 1]['code'] === T_NS_SEPARATOR) { |
||
584 | // Not a namespace declaration, but use of, i.e. namespace\someFunction(); |
||
585 | return false; |
||
586 | } |
||
587 | |||
588 | $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
||
589 | if ($tokens[$nextToken]['code'] === T_OPEN_CURLY_BRACKET) { |
||
590 | // Declaration for global namespace when using multiple namespaces in a file. |
||
591 | // I.e.: namespace {} |
||
592 | return ''; |
||
593 | } |
||
594 | |||
595 | // Ok, this should be a namespace declaration, so get all the parts together. |
||
596 | $validTokens = array( |
||
597 | T_STRING, |
||
598 | T_NS_SEPARATOR, |
||
599 | T_WHITESPACE, |
||
600 | ); |
||
601 | |||
602 | $namespaceName = ''; |
||
603 | while(in_array($tokens[$nextToken]['code'], $validTokens, true) === true) { |
||
604 | $namespaceName .= trim($tokens[$nextToken]['content']); |
||
605 | $nextToken++; |
||
606 | } |
||
607 | |||
608 | return $namespaceName; |
||
609 | } |
||
610 | |||
611 | |||
612 | /** |
||
613 | * Returns the method parameters for the specified T_FUNCTION token. |
||
614 | * |
||
615 | * Each parameter is in the following format: |
||
616 | * |
||
617 | * <code> |
||
618 | * 0 => array( |
||
619 | * 'name' => '$var', // The variable name. |
||
620 | * 'pass_by_reference' => false, // Passed by reference. |
||
621 | * 'type_hint' => string, // Type hint for array or custom type |
||
622 | * ) |
||
623 | * </code> |
||
624 | * |
||
625 | * Parameters with default values have an additional array index of |
||
626 | * 'default' with the value of the default as a string. |
||
627 | * |
||
628 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
629 | * class, but with some improvements which were only introduced in PHPCS 2.7. |
||
630 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
631 | * that, this method can be removed and calls to it replaced with |
||
632 | * `$phpcsFile->getMethodParameters($stackPtr)` calls.}} |
||
633 | * |
||
634 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
635 | * @param int $stackPtr The position in the stack of the T_FUNCTION token |
||
636 | * to acquire the parameters for. |
||
637 | * |
||
638 | * @return array |
||
639 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
640 | * type T_FUNCTION. |
||
641 | */ |
||
642 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
643 | { |
||
644 | $tokens = $phpcsFile->getTokens(); |
||
645 | |||
646 | // Check for the existence of the token. |
||
647 | if (isset($tokens[$stackPtr]) === false) { |
||
648 | return false; |
||
649 | } |
||
650 | |||
651 | if ($tokens[$stackPtr]['code'] !== T_FUNCTION) { |
||
652 | throw new PHP_CodeSniffer_Exception('$stackPtr must be of type T_FUNCTION'); |
||
653 | } |
||
654 | |||
655 | $opener = $tokens[$stackPtr]['parenthesis_opener']; |
||
656 | $closer = $tokens[$stackPtr]['parenthesis_closer']; |
||
657 | |||
658 | $vars = array(); |
||
659 | $currVar = null; |
||
660 | $paramStart = ($opener + 1); |
||
661 | $defaultStart = null; |
||
662 | $paramCount = 0; |
||
663 | $passByReference = false; |
||
664 | $variableLength = false; |
||
665 | $typeHint = ''; |
||
666 | |||
667 | for ($i = $paramStart; $i <= $closer; $i++) { |
||
668 | // Check to see if this token has a parenthesis or bracket opener. If it does |
||
669 | // it's likely to be an array which might have arguments in it. This |
||
670 | // could cause problems in our parsing below, so lets just skip to the |
||
671 | // end of it. |
||
672 | View Code Duplication | if (isset($tokens[$i]['parenthesis_opener']) === true) { |
|
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. ![]() |
|||
673 | // Don't do this if it's the close parenthesis for the method. |
||
674 | if ($i !== $tokens[$i]['parenthesis_closer']) { |
||
675 | $i = ($tokens[$i]['parenthesis_closer'] + 1); |
||
676 | } |
||
677 | } |
||
678 | |||
679 | View Code Duplication | if (isset($tokens[$i]['bracket_opener']) === true) { |
|
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. ![]() |
|||
680 | // Don't do this if it's the close parenthesis for the method. |
||
681 | if ($i !== $tokens[$i]['bracket_closer']) { |
||
682 | $i = ($tokens[$i]['bracket_closer'] + 1); |
||
683 | } |
||
684 | } |
||
685 | |||
686 | switch ($tokens[$i]['code']) { |
||
687 | case T_BITWISE_AND: |
||
688 | $passByReference = true; |
||
689 | break; |
||
690 | case T_VARIABLE: |
||
691 | $currVar = $i; |
||
692 | break; |
||
693 | case T_ELLIPSIS: |
||
694 | $variableLength = true; |
||
695 | break; |
||
696 | case T_ARRAY_HINT: |
||
697 | case T_CALLABLE: |
||
698 | $typeHint = $tokens[$i]['content']; |
||
699 | break; |
||
700 | case T_SELF: |
||
701 | case T_PARENT: |
||
702 | case T_STATIC: |
||
703 | // Self is valid, the others invalid, but were probably intended as type hints. |
||
704 | if (isset($defaultStart) === false) { |
||
705 | $typeHint = $tokens[$i]['content']; |
||
706 | } |
||
707 | break; |
||
708 | case T_STRING: |
||
709 | // This is a string, so it may be a type hint, but it could |
||
710 | // also be a constant used as a default value. |
||
711 | $prevComma = false; |
||
712 | View Code Duplication | for ($t = $i; $t >= $opener; $t--) { |
|
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. ![]() |
|||
713 | if ($tokens[$t]['code'] === T_COMMA) { |
||
714 | $prevComma = $t; |
||
715 | break; |
||
716 | } |
||
717 | } |
||
718 | |||
719 | if ($prevComma !== false) { |
||
720 | $nextEquals = false; |
||
721 | View Code Duplication | for ($t = $prevComma; $t < $i; $t++) { |
|
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. ![]() |
|||
722 | if ($tokens[$t]['code'] === T_EQUAL) { |
||
723 | $nextEquals = $t; |
||
724 | break; |
||
725 | } |
||
726 | } |
||
727 | |||
728 | if ($nextEquals !== false) { |
||
729 | break; |
||
730 | } |
||
731 | } |
||
732 | |||
733 | if ($defaultStart === null) { |
||
734 | $typeHint .= $tokens[$i]['content']; |
||
735 | } |
||
736 | break; |
||
737 | case T_NS_SEPARATOR: |
||
738 | // Part of a type hint or default value. |
||
739 | if ($defaultStart === null) { |
||
740 | $typeHint .= $tokens[$i]['content']; |
||
741 | } |
||
742 | break; |
||
743 | case T_CLOSE_PARENTHESIS: |
||
744 | case T_COMMA: |
||
745 | // If it's null, then there must be no parameters for this |
||
746 | // method. |
||
747 | if ($currVar === null) { |
||
748 | continue; |
||
749 | } |
||
750 | |||
751 | $vars[$paramCount] = array(); |
||
752 | $vars[$paramCount]['name'] = $tokens[$currVar]['content']; |
||
753 | |||
754 | if ($defaultStart !== null) { |
||
755 | $vars[$paramCount]['default'] |
||
756 | = $phpcsFile->getTokensAsString( |
||
757 | $defaultStart, |
||
758 | ($i - $defaultStart) |
||
759 | ); |
||
760 | } |
||
761 | |||
762 | $rawContent = trim($phpcsFile->getTokensAsString($paramStart, ($i - $paramStart))); |
||
763 | |||
764 | $vars[$paramCount]['pass_by_reference'] = $passByReference; |
||
765 | $vars[$paramCount]['variable_length'] = $variableLength; |
||
766 | $vars[$paramCount]['type_hint'] = $typeHint; |
||
767 | $vars[$paramCount]['raw'] = $rawContent; |
||
768 | |||
769 | // Reset the vars, as we are about to process the next parameter. |
||
770 | $defaultStart = null; |
||
771 | $paramStart = ($i + 1); |
||
772 | $passByReference = false; |
||
773 | $variableLength = false; |
||
774 | $typeHint = ''; |
||
775 | |||
776 | $paramCount++; |
||
777 | break; |
||
778 | case T_EQUAL: |
||
779 | $defaultStart = ($i + 1); |
||
780 | break; |
||
781 | }//end switch |
||
782 | }//end for |
||
783 | |||
784 | return $vars; |
||
785 | |||
786 | }//end getMethodParameters() |
||
787 | |||
788 | }//end class |
||
789 |
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.