Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PHPCompatibility_Sniff often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PHPCompatibility_Sniff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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() |
||
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) |
|
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) |
|
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()) |
||
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) |
||
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) { |
||
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) { |
||
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) |
||
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 |
|
1 ignored issue
–
show
|
|||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) { |
||
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) |
||
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 ) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
1447 | |||
1448 | |||
1449 | /** |
||
1450 | * Returns the name of the class that the specified class extends. |
||
1451 | * |
||
1452 | * Returns FALSE on error or if there is no extended class name. |
||
1453 | * |
||
1454 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
1455 | * class, but with some improvements which have been introduced in |
||
1456 | * PHPCS 2.8.0. |
||
1457 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/commit/0011d448119d4c568e3ac1f825ae78815bf2cc34}. |
||
1458 | * |
||
1459 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
1460 | * that, this method can be removed and calls to it replaced with |
||
1461 | * `$phpcsFile->findExtendedClassName($stackPtr)` calls. |
||
1462 | * |
||
1463 | * Last synced with PHPCS version: PHPCS 2.9.0 at commit b940fb7dca8c2a37f0514161b495363e5b36d879}} |
||
1464 | * |
||
1465 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1466 | * @param int $stackPtr The position in the stack of the |
||
1467 | * class token. |
||
1468 | * |
||
1469 | * @return string|false |
||
1470 | */ |
||
1471 | public function findExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1517 | |||
1518 | |||
1519 | /** |
||
1520 | * Get the hash algorithm name from the parameter in a hash function call. |
||
1521 | * |
||
1522 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1523 | * @param int $stackPtr The position of the T_STRING function token. |
||
1524 | * |
||
1525 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
1526 | * function call or false if it was not. |
||
1527 | */ |
||
1528 | public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1563 | |||
1564 | }//end class |
||
1565 |
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.