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() |
||
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 | // If no upper-limit is set, we set the max version to 99.9. |
||
128 | // This is *probably* safe... :-) |
||
129 | $arrTestVersions[$testVersion] = array(substr($testVersion, 0, -1), '99.9'); |
||
130 | } |
||
131 | View Code Duplication | elseif (preg_match('/^-\d+\.\d+$/', $testVersion)) { |
|
1 ignored issue
–
show
|
|||
132 | // If no lower-limit is set, we set the min version to 4.0. |
||
133 | // Whilst development focuses on PHP 5 and above, we also accept |
||
134 | // sniffs for PHP 4, so we include that as the minimum. |
||
135 | // (It makes no sense to support PHP 3 as this was effectively a |
||
136 | // different language). |
||
137 | $arrTestVersions[$testVersion] = array('4.0', substr($testVersion, 1)); |
||
138 | } |
||
139 | elseif (!$testVersion == '') { |
||
140 | trigger_error("Invalid testVersion setting: '" . $testVersion |
||
141 | . "'", E_USER_WARNING); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | if (isset($arrTestVersions[$testVersion])) { |
||
146 | return $arrTestVersions[$testVersion]; |
||
147 | } |
||
148 | else { |
||
149 | return array(null, null); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | |||
154 | /** |
||
155 | * Check whether a specific PHP version is equal to or higher than the maximum |
||
156 | * supported PHP version as provided by the user in `testVersion`. |
||
157 | * |
||
158 | * Should be used when sniffing for *old* PHP features (deprecated/removed). |
||
159 | * |
||
160 | * @param string $phpVersion A PHP version number in 'major.minor' format. |
||
161 | * |
||
162 | * @return bool True if testVersion has not been provided or if the PHP version |
||
163 | * is equal to or higher than the highest supported PHP version |
||
164 | * in testVersion. False otherwise. |
||
165 | */ |
||
166 | View Code Duplication | public function supportsAbove($phpVersion) |
|
179 | |||
180 | |||
181 | /** |
||
182 | * Check whether a specific PHP version is equal to or lower than the minimum |
||
183 | * supported PHP version as provided by the user in `testVersion`. |
||
184 | * |
||
185 | * Should be used when sniffing for *new* PHP features. |
||
186 | * |
||
187 | * @param string $phpVersion A PHP version number in 'major.minor' format. |
||
188 | * |
||
189 | * @return bool True if the PHP version is equal to or lower than the lowest |
||
190 | * supported PHP version in testVersion. |
||
191 | * False otherwise or if no testVersion is provided. |
||
192 | */ |
||
193 | View Code Duplication | public function supportsBelow($phpVersion) |
|
206 | |||
207 | |||
208 | /** |
||
209 | * Add a PHPCS message to the output stack as either a warning or an error. |
||
210 | * |
||
211 | * @param PHP_CodeSniffer_File $phpcsFile The file the message applies to. |
||
212 | * @param string $message The message. |
||
213 | * @param int $stackPtr The position of the token |
||
214 | * the message relates to. |
||
215 | * @param bool $isError Whether to report the message as an |
||
216 | * 'error' or 'warning'. |
||
217 | * Defaults to true (error). |
||
218 | * @param string $code The error code for the message. |
||
219 | * Defaults to 'Found'. |
||
220 | * @param array $data Optional input for the data replacements. |
||
221 | * |
||
222 | * @return void |
||
223 | */ |
||
224 | public function addMessage($phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array()) |
||
232 | |||
233 | |||
234 | /** |
||
235 | * Convert an arbitrary string to an alphanumeric string with underscores. |
||
236 | * |
||
237 | * Pre-empt issues with arbitrary strings being used as error codes in XML and PHP. |
||
238 | * |
||
239 | * @param string $baseString Arbitrary string. |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | public function stringToErrorCode($baseString) |
||
247 | |||
248 | |||
249 | /** |
||
250 | * Strip quotes surrounding an arbitrary string. |
||
251 | * |
||
252 | * Intended for use with the content of a T_CONSTANT_ENCAPSED_STRING / T_DOUBLE_QUOTED_STRING. |
||
253 | * |
||
254 | * @param string $string The raw string. |
||
255 | * |
||
256 | * @return string String without quotes around it. |
||
257 | */ |
||
258 | public function stripQuotes($string) { |
||
261 | |||
262 | |||
263 | /** |
||
264 | * Strip variables from an arbitrary double quoted string. |
||
265 | * |
||
266 | * Intended for use with the content of a T_DOUBLE_QUOTED_STRING. |
||
267 | * |
||
268 | * @param string $string The raw string. |
||
269 | * |
||
270 | * @return string String without variables in it. |
||
271 | */ |
||
272 | public function stripVariables($string) { |
||
279 | |||
280 | |||
281 | /** |
||
282 | * Make all top level array keys in an array lowercase. |
||
283 | * |
||
284 | * @param array $array Initial array. |
||
285 | * |
||
286 | * @return array Same array, but with all lowercase top level keys. |
||
287 | */ |
||
288 | public function arrayKeysToLowercase($array) |
||
294 | |||
295 | |||
296 | /** |
||
297 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
298 | * |
||
299 | * Returns FALSE on error or if there are no implemented interface names. |
||
300 | * |
||
301 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
302 | * This method also includes an improvement we use which was only introduced |
||
303 | * in PHPCS 2.8.0, so only defer to upstream for higher versions. |
||
304 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
305 | * that, this method can be removed and calls to it replaced with |
||
306 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
307 | * |
||
308 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
309 | * @param int $stackPtr The position of the class token. |
||
310 | * |
||
311 | * @return array|false |
||
312 | */ |
||
313 | public function findImplementedInterfaceNames(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
362 | |||
363 | |||
364 | /** |
||
365 | * Checks if a function call has parameters. |
||
366 | * |
||
367 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
368 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
369 | * |
||
370 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, it |
||
371 | * will detect whether the array has values or is empty. |
||
372 | * |
||
373 | * @link https://github.com/wimg/PHPCompatibility/issues/120 |
||
374 | * @link https://github.com/wimg/PHPCompatibility/issues/152 |
||
375 | * |
||
376 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
377 | * @param int $stackPtr The position of the function call token. |
||
378 | * |
||
379 | * @return bool |
||
380 | */ |
||
381 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
432 | |||
433 | |||
434 | /** |
||
435 | * Count the number of parameters a function call has been passed. |
||
436 | * |
||
437 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
438 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
439 | * |
||
440 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
||
441 | * it will return the number of values in the array. |
||
442 | * |
||
443 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
444 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
445 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
446 | * |
||
447 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
448 | * @param int $stackPtr The position of the function call token. |
||
449 | * |
||
450 | * @return int |
||
451 | */ |
||
452 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
460 | |||
461 | |||
462 | /** |
||
463 | * Get information on all parameters passed to a function call. |
||
464 | * |
||
465 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
466 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
467 | * |
||
468 | * Will return an multi-dimentional array with the start token pointer, end token |
||
469 | * pointer and raw parameter value for all parameters. Index will be 1-based. |
||
470 | * If no parameters are found, will return an empty array. |
||
471 | * |
||
472 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
||
473 | * it will tokenize the values / key/value pairs contained in the array call. |
||
474 | * |
||
475 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
476 | * @param int $stackPtr The position of the function call token. |
||
477 | * |
||
478 | * @return array |
||
479 | */ |
||
480 | public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
563 | |||
564 | |||
565 | /** |
||
566 | * Get information on a specific parameter passed to a function call. |
||
567 | * |
||
568 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
569 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
570 | * |
||
571 | * Will return a array with the start token pointer, end token pointer and the raw value |
||
572 | * of the parameter at a specific offset. |
||
573 | * If the specified parameter is not found, will return false. |
||
574 | * |
||
575 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
576 | * @param int $stackPtr The position of the function call token. |
||
577 | * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
||
578 | * |
||
579 | * @return array|false |
||
580 | */ |
||
581 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
||
592 | |||
593 | |||
594 | /** |
||
595 | * Verify whether a token is within a scoped condition. |
||
596 | * |
||
597 | * If the optional $validScopes parameter has been passed, the function |
||
598 | * will check that the token has at least one condition which is of a |
||
599 | * type defined in $validScopes. |
||
600 | * |
||
601 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
602 | * @param int $stackPtr The position of the token. |
||
603 | * @param array|int $validScopes Optional. Array of valid scopes |
||
604 | * or int value of a valid scope. |
||
605 | * Pass the T_.. constant(s) for the |
||
606 | * desired scope to this parameter. |
||
607 | * |
||
608 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
609 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
610 | * valid scope, false otherwise. |
||
611 | */ |
||
612 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
633 | |||
634 | |||
635 | /** |
||
636 | * Verify whether a token is within a class scope. |
||
637 | * |
||
638 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
639 | * @param int $stackPtr The position of the token. |
||
640 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
641 | * scope or also accept interfaces and traits |
||
642 | * as scope. |
||
643 | * |
||
644 | * @return bool True if within class scope, false otherwise. |
||
645 | */ |
||
646 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
660 | |||
661 | |||
662 | /** |
||
663 | * Verify whether a token is within a scoped use statement. |
||
664 | * |
||
665 | * PHPCS cross-version compatibility method. |
||
666 | * |
||
667 | * In PHPCS 1.x no conditions are set for a scoped use statement. |
||
668 | * This method works around that limitation. |
||
669 | * |
||
670 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
671 | * @param int $stackPtr The position of the token. |
||
672 | * |
||
673 | * @return bool True if within use scope, false otherwise. |
||
674 | */ |
||
675 | public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
707 | |||
708 | |||
709 | /** |
||
710 | * Returns the fully qualified class name for a new class instantiation. |
||
711 | * |
||
712 | * Returns an empty string if the class name could not be reliably inferred. |
||
713 | * |
||
714 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
715 | * @param int $stackPtr The position of a T_NEW token. |
||
716 | * |
||
717 | * @return string |
||
718 | */ |
||
719 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
760 | |||
761 | |||
762 | /** |
||
763 | * Returns the fully qualified name of the class that the specified class extends. |
||
764 | * |
||
765 | * Returns an empty string if the class does not extend another class or if |
||
766 | * the class name could not be reliably inferred. |
||
767 | * |
||
768 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
769 | * @param int $stackPtr The position of a T_CLASS token. |
||
770 | * |
||
771 | * @return string |
||
772 | */ |
||
773 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
793 | |||
794 | |||
795 | /** |
||
796 | * Returns the class name for the static usage of a class. |
||
797 | * This can be a call to a method, the use of a property or constant. |
||
798 | * |
||
799 | * Returns an empty string if the class name could not be reliably inferred. |
||
800 | * |
||
801 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
802 | * @param int $stackPtr The position of a T_NEW token. |
||
803 | * |
||
804 | * @return string |
||
805 | */ |
||
806 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
856 | |||
857 | |||
858 | /** |
||
859 | * Get the Fully Qualified name for a class/function/constant etc. |
||
860 | * |
||
861 | * Checks if a class/function/constant name is already fully qualified and |
||
862 | * if not, enrich it with the relevant namespace information. |
||
863 | * |
||
864 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
865 | * @param int $stackPtr The position of the token. |
||
866 | * @param string $name The class / function / constant name. |
||
867 | * |
||
868 | * @return string |
||
869 | */ |
||
870 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
891 | |||
892 | |||
893 | /** |
||
894 | * Is the class/function/constant name namespaced or global ? |
||
895 | * |
||
896 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
897 | * I.e. should always start with a `\` ! |
||
898 | * |
||
899 | * @return bool True if namespaced, false if global. |
||
900 | */ |
||
901 | public function isNamespaced($FQName) { |
||
908 | |||
909 | |||
910 | /** |
||
911 | * Determine the namespace name an arbitrary token lives in. |
||
912 | * |
||
913 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
914 | * @param int $stackPtr The token position for which to determine the namespace. |
||
915 | * |
||
916 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
917 | */ |
||
918 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
970 | |||
971 | /** |
||
972 | * Get the complete namespace name for a namespace declaration. |
||
973 | * |
||
974 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
975 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
976 | * |
||
977 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
978 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
979 | * |
||
980 | * @return string|false Namespace name or false if not a namespace declaration. |
||
981 | * Namespace name can be an empty string for global namespace declaration. |
||
982 | */ |
||
983 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
||
1023 | |||
1024 | |||
1025 | /** |
||
1026 | * Get the stack pointer for a return type token for a given function. |
||
1027 | * |
||
1028 | * Compatible layer for older PHPCS versions which don't recognize |
||
1029 | * return type hints correctly. |
||
1030 | * |
||
1031 | * Expects to be passed T_RETURN_TYPE, T_FUNCTION or T_CLOSURE token. |
||
1032 | * |
||
1033 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
1034 | * @param int $stackPtr The position of the token. |
||
1035 | * |
||
1036 | * @return int|false Stack pointer to the return type token or false if |
||
1037 | * no return type was found or the passed token was |
||
1038 | * not of the correct type. |
||
1039 | */ |
||
1040 | public function getReturnTypeHintToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1077 | |||
1078 | |||
1079 | /** |
||
1080 | * Check whether a T_VARIABLE token is a class property declaration. |
||
1081 | * |
||
1082 | * Compatibility layer for PHPCS cross-version compatibility |
||
1083 | * as PHPCS 2.4.0 - 2.7.1 does not have good enough support for |
||
1084 | * anonymous classes. Along the same lines, the`getMemberProperties()` |
||
1085 | * method does not support the `var` prefix. |
||
1086 | * |
||
1087 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1088 | * @param int $stackPtr The position in the stack of the |
||
1089 | * T_VARIABLE token to verify. |
||
1090 | * |
||
1091 | * @return bool |
||
1092 | */ |
||
1093 | public function isClassProperty(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1116 | |||
1117 | |||
1118 | /** |
||
1119 | * Check whether a T_CONST token is a class constant declaration. |
||
1120 | * |
||
1121 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1122 | * @param int $stackPtr The position in the stack of the |
||
1123 | * T_CONST token to verify. |
||
1124 | * |
||
1125 | * @return bool |
||
1126 | */ |
||
1127 | public function isClassConstant(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1147 | |||
1148 | |||
1149 | /** |
||
1150 | * Check whether the direct wrapping scope of a token is within a limited set of |
||
1151 | * acceptable tokens. |
||
1152 | * |
||
1153 | * Used to check, for instance, if a T_CONST is a class constant. |
||
1154 | * |
||
1155 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1156 | * @param int $stackPtr The position in the stack of the |
||
1157 | * T_CONST token to verify. |
||
1158 | * @param array $validScopes Array of token types. |
||
1159 | * Keys should be the token types in string |
||
1160 | * format to allow for newer token types. |
||
1161 | * Value is irrelevant. |
||
1162 | * |
||
1163 | * @return bool |
||
1164 | */ |
||
1165 | protected function validDirectScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes) |
||
1189 | |||
1190 | |||
1191 | /** |
||
1192 | * Get an array of just the type hints from a function declaration. |
||
1193 | * |
||
1194 | * Expects to be passed T_FUNCTION or T_CLOSURE token. |
||
1195 | * |
||
1196 | * Strips potential nullable indicator and potential global namespace |
||
1197 | * indicator from the type hints before returning them. |
||
1198 | * |
||
1199 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
1200 | * @param int $stackPtr The position of the token. |
||
1201 | * |
||
1202 | * @return array Array with type hints or an empty array if |
||
1203 | * - the function does not have any parameters |
||
1204 | * - no type hints were found |
||
1205 | * - or the passed token was not of the correct type. |
||
1206 | */ |
||
1207 | public function getTypeHintsFromFunctionDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1240 | |||
1241 | |||
1242 | /** |
||
1243 | * Returns the method parameters for the specified function token. |
||
1244 | * |
||
1245 | * Each parameter is in the following format: |
||
1246 | * |
||
1247 | * <code> |
||
1248 | * 0 => array( |
||
1249 | * 'token' => int, // The position of the var in the token stack. |
||
1250 | * 'name' => '$var', // The variable name. |
||
1251 | * 'content' => string, // The full content of the variable definition. |
||
1252 | * 'pass_by_reference' => boolean, // Is the variable passed by reference? |
||
1253 | * 'variable_length' => boolean, // Is the param of variable length through use of `...` ? |
||
1254 | * 'type_hint' => string, // The type hint for the variable. |
||
1255 | * 'nullable_type' => boolean, // Is the variable using a nullable type? |
||
1256 | * ) |
||
1257 | * </code> |
||
1258 | * |
||
1259 | * Parameters with default values have an additional array index of |
||
1260 | * 'default' with the value of the default as a string. |
||
1261 | * |
||
1262 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
1263 | * class, but with some improvements which have been introduced in |
||
1264 | * PHPCS 2.8.0. |
||
1265 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1117}, |
||
1266 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} and |
||
1267 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1293}. |
||
1268 | * |
||
1269 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
1270 | * that, this method can be removed and calls to it replaced with |
||
1271 | * `$phpcsFile->getMethodParameters($stackPtr)` calls. |
||
1272 | * |
||
1273 | * NOTE: This version does not deal with the new T_NULLABLE token type. |
||
1274 | * This token is included upstream only in 2.8.0+ and as we defer to upstream |
||
1275 | * in that case, no need to deal with it here. |
||
1276 | * |
||
1277 | * Last synced with PHPCS version: PHPCS 2.9.0-alpha at commit f1511adad043edfd6d2e595e77385c32577eb2bc}} |
||
1278 | * |
||
1279 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1280 | * @param int $stackPtr The position in the stack of the |
||
1281 | * function token to acquire the |
||
1282 | * parameters for. |
||
1283 | * |
||
1284 | * @return array|false |
||
1285 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
1286 | * type T_FUNCTION or T_CLOSURE. |
||
1287 | */ |
||
1288 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1445 | |||
1446 | |||
1447 | /** |
||
1448 | * Returns the name of the class that the specified class extends. |
||
1449 | * |
||
1450 | * Returns FALSE on error or if there is no extended class name. |
||
1451 | * |
||
1452 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
1453 | * class, but with some improvements which have been introduced in |
||
1454 | * PHPCS 2.8.0. |
||
1455 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/commit/0011d448119d4c568e3ac1f825ae78815bf2cc34}. |
||
1456 | * |
||
1457 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
1458 | * that, this method can be removed and calls to it replaced with |
||
1459 | * `$phpcsFile->findExtendedClassName($stackPtr)` calls. |
||
1460 | * |
||
1461 | * Last synced with PHPCS version: PHPCS 2.9.0 at commit b940fb7dca8c2a37f0514161b495363e5b36d879}} |
||
1462 | * |
||
1463 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1464 | * @param int $stackPtr The position in the stack of the |
||
1465 | * class token. |
||
1466 | * |
||
1467 | * @return string|false |
||
1468 | */ |
||
1469 | public function findExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1515 | |||
1516 | |||
1517 | /** |
||
1518 | * Get the hash algorithm name from the parameter in a hash function call. |
||
1519 | * |
||
1520 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1521 | * @param int $stackPtr The position of the T_STRING function token. |
||
1522 | * |
||
1523 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
1524 | * function call or false if it was not. |
||
1525 | */ |
||
1526 | public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1561 | |||
1562 | }//end class |
||
1563 |
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.