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 |
||
19 | abstract class PHPCompatibility_Sniff implements PHP_CodeSniffer_Sniff |
||
|
|||
20 | { |
||
21 | |||
22 | const REGEX_COMPLEX_VARS = '`(?:(\{)?(?<!\\\\)\$)?(\{)?(?<!\\\\)\$(\{)?(?P<varname>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:->\$?(?P>varname)|\[[^\]]+\]|::\$?(?P>varname)|\([^\)]*\))*(?(3)\}|)(?(2)\}|)(?(1)\}|)`'; |
||
23 | |||
24 | /** |
||
25 | * List of superglobals as an array of strings. |
||
26 | * |
||
27 | * Used by the ParameterShadowSuperGlobals and ForbiddenClosureUseVariableNames sniffs. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $superglobals = array( |
||
32 | '$GLOBALS', |
||
33 | '$_SERVER', |
||
34 | '$_GET', |
||
35 | '$_POST', |
||
36 | '$_FILES', |
||
37 | '$_COOKIE', |
||
38 | '$_SESSION', |
||
39 | '$_REQUEST', |
||
40 | '$_ENV', |
||
41 | ); |
||
42 | |||
43 | /** |
||
44 | * List of functions using hash algorithm as parameter (always the first parameter). |
||
45 | * |
||
46 | * Used by the new/removed hash algorithm sniffs. |
||
47 | * Key is the function name, value is the 1-based parameter position in the function call. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $hashAlgoFunctions = array( |
||
52 | 'hash_file' => 1, |
||
53 | 'hash_hmac_file' => 1, |
||
54 | 'hash_hmac' => 1, |
||
55 | 'hash_init' => 1, |
||
56 | 'hash_pbkdf2' => 1, |
||
57 | 'hash' => 1, |
||
58 | ); |
||
59 | |||
60 | |||
61 | /** |
||
62 | * List of functions which take an ini directive as parameter (always the first parameter). |
||
63 | * |
||
64 | * Used by the new/removed ini directives sniffs. |
||
65 | * Key is the function name, value is the 1-based parameter position in the function call. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $iniFunctions = array( |
||
70 | 'ini_get' => 1, |
||
71 | 'ini_set' => 1, |
||
72 | ); |
||
73 | |||
74 | |||
75 | /** |
||
76 | * Get the testVersion configuration variable. |
||
77 | * |
||
78 | * The testVersion configuration variable may be in any of the following formats: |
||
79 | * 1) Omitted/empty, in which case no version is specified. This effectively |
||
80 | * disables all the checks for new PHP features provided by this standard. |
||
81 | * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
||
82 | * the code will run on that version of PHP (no deprecated features or newer |
||
83 | * features being used). |
||
84 | * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
||
85 | * on all PHP versions in that range, and that it doesn't use any features that |
||
86 | * were deprecated by the final version in the list, or which were not available |
||
87 | * for the first version in the list. |
||
88 | * We accept ranges where one of the components is missing, e.g. "-5.6" means |
||
89 | * all versions up to PHP 5.6, and "7.0-" means all versions above PHP 7.0. |
||
90 | * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
||
91 | * would be treated as invalid, and ignored. |
||
92 | * |
||
93 | * @return array $arrTestVersions will hold an array containing min/max version |
||
94 | * of PHP that we are checking against (see above). If only a |
||
95 | * single version number is specified, then this is used as |
||
96 | * both the min and max. |
||
97 | * |
||
98 | * @throws PHP_CodeSniffer_Exception If testVersion is invalid. |
||
99 | */ |
||
100 | private function getTestVersion() |
||
101 | { |
||
102 | static $arrTestVersions = array(); |
||
103 | |||
104 | $testVersion = trim(PHP_CodeSniffer::getConfigData('testVersion')); |
||
105 | |||
106 | if (!isset($arrTestVersions[$testVersion]) && !empty($testVersion)) { |
||
107 | |||
108 | $arrTestVersions[$testVersion] = array(null, null); |
||
109 | if (preg_match('/^\d+\.\d+$/', $testVersion)) { |
||
110 | $arrTestVersions[$testVersion] = array($testVersion, $testVersion); |
||
111 | |||
112 | } elseif (preg_match('/^(\d+\.\d+)\s*-\s*(\d+\.\d+)$/', $testVersion, $matches)) { |
||
113 | if (version_compare($matches[1], $matches[2], '>')) { |
||
114 | trigger_error( |
||
115 | "Invalid range in testVersion setting: '" . $testVersion . "'", |
||
116 | E_USER_WARNING |
||
117 | ); |
||
118 | } |
||
119 | else { |
||
120 | $arrTestVersions[$testVersion] = array($matches[1], $matches[2]); |
||
121 | } |
||
122 | |||
123 | } elseif (preg_match('/^\d+\.\d+-$/', $testVersion)) { |
||
124 | $testVersion = substr($testVersion, 0, -1); |
||
125 | // If no upper-limit is set, we set the max version to 99.9. |
||
126 | // This is *probably* safe... :-) |
||
127 | $arrTestVersions[$testVersion] = array($testVersion, '99.9'); |
||
128 | |||
129 | } elseif (preg_match('/^-\d+\.\d+$/', $testVersion)) { |
||
130 | $testVersion = substr($testVersion, 1); |
||
131 | // If no lower-limit is set, we set the min version to 4.0. |
||
132 | // Whilst development focuses on PHP 5 and above, we also accept |
||
133 | // sniffs for PHP 4, so we include that as the minimum. |
||
134 | // (It makes no sense to support PHP 3 as this was effectively a |
||
135 | // different language). |
||
136 | $arrTestVersions[$testVersion] = array('4.0', $testVersion); |
||
137 | |||
138 | } elseif (!$testVersion == '') { |
||
139 | trigger_error( |
||
140 | "Invalid testVersion setting: '" . $testVersion . "'", |
||
141 | E_USER_WARNING |
||
142 | ); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | if (isset($arrTestVersions[$testVersion])) { |
||
147 | return $arrTestVersions[$testVersion]; |
||
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) |
|
167 | { |
||
168 | $testVersion = $this->getTestVersion(); |
||
169 | $testVersion = $testVersion[1]; |
||
170 | |||
171 | if (is_null($testVersion) |
||
172 | || version_compare($testVersion, $phpVersion) >= 0 |
||
173 | ) { |
||
174 | return true; |
||
175 | } else { |
||
176 | return false; |
||
177 | } |
||
178 | }//end supportsAbove() |
||
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) |
|
194 | { |
||
195 | $testVersion = $this->getTestVersion(); |
||
196 | $testVersion = $testVersion[0]; |
||
197 | |||
198 | if (!is_null($testVersion) |
||
199 | && version_compare($testVersion, $phpVersion) <= 0 |
||
200 | ) { |
||
201 | return true; |
||
202 | } else { |
||
203 | return false; |
||
204 | } |
||
205 | }//end supportsBelow() |
||
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()) |
||
225 | { |
||
226 | if ($isError === true) { |
||
227 | $phpcsFile->addError($message, $stackPtr, $code, $data); |
||
228 | } else { |
||
229 | $phpcsFile->addWarning($message, $stackPtr, $code, $data); |
||
230 | } |
||
231 | } |
||
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) |
||
244 | { |
||
245 | return preg_replace('`[^a-z0-9_]`i', '_', strtolower($baseString)); |
||
246 | } |
||
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) |
||
259 | { |
||
260 | return preg_replace('`^([\'"])(.*)\1$`Ds', '$2', $string); |
||
261 | } |
||
262 | |||
263 | |||
264 | /** |
||
265 | * Strip variables from an arbitrary double quoted string. |
||
266 | * |
||
267 | * Intended for use with the content of a T_DOUBLE_QUOTED_STRING. |
||
268 | * |
||
269 | * @param string $string The raw string. |
||
270 | * |
||
271 | * @return string String without variables in it. |
||
272 | */ |
||
273 | public function stripVariables($string) |
||
274 | { |
||
275 | if (strpos($string, '$') === false) { |
||
276 | return $string; |
||
277 | } |
||
278 | |||
279 | return preg_replace(self::REGEX_COMPLEX_VARS, '', $string); |
||
280 | } |
||
281 | |||
282 | |||
283 | /** |
||
284 | * Make all top level array keys in an array lowercase. |
||
285 | * |
||
286 | * @param array $array Initial array. |
||
287 | * |
||
288 | * @return array Same array, but with all lowercase top level keys. |
||
289 | */ |
||
290 | public function arrayKeysToLowercase($array) |
||
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) |
||
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) |
||
433 | |||
434 | |||
435 | /** |
||
436 | * Count the number of parameters a function call has been passed. |
||
437 | * |
||
438 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
439 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
440 | * |
||
441 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
||
442 | * it will return the number of values in the array. |
||
443 | * |
||
444 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
445 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
446 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
447 | * |
||
448 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
449 | * @param int $stackPtr The position of the function call token. |
||
450 | * |
||
451 | * @return int |
||
452 | */ |
||
453 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
461 | |||
462 | |||
463 | /** |
||
464 | * Get information on all parameters passed to a function call. |
||
465 | * |
||
466 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
467 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
468 | * |
||
469 | * Will return an multi-dimentional array with the start token pointer, end token |
||
470 | * pointer and raw parameter value for all parameters. Index will be 1-based. |
||
471 | * If no parameters are found, will return an empty array. |
||
472 | * |
||
473 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
||
474 | * it will tokenize the values / key/value pairs contained in the array call. |
||
475 | * |
||
476 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
477 | * @param int $stackPtr The position of the function call token. |
||
478 | * |
||
479 | * @return array |
||
480 | */ |
||
481 | public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
559 | |||
560 | |||
561 | /** |
||
562 | * Get information on a specific parameter passed to a function call. |
||
563 | * |
||
564 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
565 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
566 | * |
||
567 | * Will return a array with the start token pointer, end token pointer and the raw value |
||
568 | * of the parameter at a specific offset. |
||
569 | * If the specified parameter is not found, will return false. |
||
570 | * |
||
571 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
572 | * @param int $stackPtr The position of the function call token. |
||
573 | * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
||
574 | * |
||
575 | * @return array|false |
||
576 | */ |
||
577 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
||
588 | |||
589 | |||
590 | /** |
||
591 | * Verify whether a token is within a scoped condition. |
||
592 | * |
||
593 | * If the optional $validScopes parameter has been passed, the function |
||
594 | * will check that the token has at least one condition which is of a |
||
595 | * type defined in $validScopes. |
||
596 | * |
||
597 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
598 | * @param int $stackPtr The position of the token. |
||
599 | * @param array|int $validScopes Optional. Array of valid scopes |
||
600 | * or int value of a valid scope. |
||
601 | * Pass the T_.. constant(s) for the |
||
602 | * desired scope to this parameter. |
||
603 | * |
||
604 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
605 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
606 | * valid scope, false otherwise. |
||
607 | */ |
||
608 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
629 | |||
630 | |||
631 | /** |
||
632 | * Verify whether a token is within a class scope. |
||
633 | * |
||
634 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
635 | * @param int $stackPtr The position of the token. |
||
636 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
637 | * scope or also accept interfaces and traits |
||
638 | * as scope. |
||
639 | * |
||
640 | * @return bool True if within class scope, false otherwise. |
||
641 | */ |
||
642 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
656 | |||
657 | |||
658 | /** |
||
659 | * Verify whether a token is within a scoped use statement. |
||
660 | * |
||
661 | * PHPCS cross-version compatibility method. |
||
662 | * |
||
663 | * In PHPCS 1.x no conditions are set for a scoped use statement. |
||
664 | * This method works around that limitation. |
||
665 | * |
||
666 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
667 | * @param int $stackPtr The position of the token. |
||
668 | * |
||
669 | * @return bool True if within use scope, false otherwise. |
||
670 | */ |
||
671 | public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
703 | |||
704 | |||
705 | /** |
||
706 | * Returns the fully qualified class name for a new class instantiation. |
||
707 | * |
||
708 | * Returns an empty string if the class name could not be reliably inferred. |
||
709 | * |
||
710 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
711 | * @param int $stackPtr The position of a T_NEW token. |
||
712 | * |
||
713 | * @return string |
||
714 | */ |
||
715 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
756 | |||
757 | |||
758 | /** |
||
759 | * Returns the fully qualified name of the class that the specified class extends. |
||
760 | * |
||
761 | * Returns an empty string if the class does not extend another class or if |
||
762 | * the class name could not be reliably inferred. |
||
763 | * |
||
764 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
765 | * @param int $stackPtr The position of a T_CLASS token. |
||
766 | * |
||
767 | * @return string |
||
768 | */ |
||
769 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
789 | |||
790 | |||
791 | /** |
||
792 | * Returns the class name for the static usage of a class. |
||
793 | * This can be a call to a method, the use of a property or constant. |
||
794 | * |
||
795 | * Returns an empty string if the class name could not be reliably inferred. |
||
796 | * |
||
797 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
798 | * @param int $stackPtr The position of a T_NEW token. |
||
799 | * |
||
800 | * @return string |
||
801 | */ |
||
802 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
852 | |||
853 | |||
854 | /** |
||
855 | * Get the Fully Qualified name for a class/function/constant etc. |
||
856 | * |
||
857 | * Checks if a class/function/constant name is already fully qualified and |
||
858 | * if not, enrich it with the relevant namespace information. |
||
859 | * |
||
860 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
861 | * @param int $stackPtr The position of the token. |
||
862 | * @param string $name The class / function / constant name. |
||
863 | * |
||
864 | * @return string |
||
865 | */ |
||
866 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
887 | |||
888 | |||
889 | /** |
||
890 | * Is the class/function/constant name namespaced or global ? |
||
891 | * |
||
892 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
893 | * I.e. should always start with a `\`. |
||
894 | * |
||
895 | * @return bool True if namespaced, false if global. |
||
896 | */ |
||
897 | public function isNamespaced($FQName) |
||
905 | |||
906 | |||
907 | /** |
||
908 | * Determine the namespace name an arbitrary token lives in. |
||
909 | * |
||
910 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
911 | * @param int $stackPtr The token position for which to determine the namespace. |
||
912 | * |
||
913 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
914 | */ |
||
915 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
967 | |||
968 | /** |
||
969 | * Get the complete namespace name for a namespace declaration. |
||
970 | * |
||
971 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
972 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
973 | * |
||
974 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
975 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
976 | * |
||
977 | * @return string|false Namespace name or false if not a namespace declaration. |
||
978 | * Namespace name can be an empty string for global namespace declaration. |
||
979 | */ |
||
980 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1020 | |||
1021 | |||
1022 | /** |
||
1023 | * Get the stack pointer for a return type token for a given function. |
||
1024 | * |
||
1025 | * Compatible layer for older PHPCS versions which don't recognize |
||
1026 | * return type hints correctly. |
||
1027 | * |
||
1028 | * Expects to be passed T_RETURN_TYPE, T_FUNCTION or T_CLOSURE token. |
||
1029 | * |
||
1030 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
1031 | * @param int $stackPtr The position of the token. |
||
1032 | * |
||
1033 | * @return int|false Stack pointer to the return type token or false if |
||
1034 | * no return type was found or the passed token was |
||
1035 | * not of the correct type. |
||
1036 | */ |
||
1037 | public function getReturnTypeHintToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1074 | |||
1075 | |||
1076 | /** |
||
1077 | * Check whether a T_VARIABLE token is a class property declaration. |
||
1078 | * |
||
1079 | * Compatibility layer for PHPCS cross-version compatibility |
||
1080 | * as PHPCS 2.4.0 - 2.7.1 does not have good enough support for |
||
1081 | * anonymous classes. Along the same lines, the`getMemberProperties()` |
||
1082 | * method does not support the `var` prefix. |
||
1083 | * |
||
1084 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1085 | * @param int $stackPtr The position in the stack of the |
||
1086 | * T_VARIABLE token to verify. |
||
1087 | * |
||
1088 | * @return bool |
||
1089 | */ |
||
1090 | public function isClassProperty(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1113 | |||
1114 | |||
1115 | /** |
||
1116 | * Check whether a T_CONST token is a class constant declaration. |
||
1117 | * |
||
1118 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1119 | * @param int $stackPtr The position in the stack of the |
||
1120 | * T_CONST token to verify. |
||
1121 | * |
||
1122 | * @return bool |
||
1123 | */ |
||
1124 | public function isClassConstant(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1144 | |||
1145 | |||
1146 | /** |
||
1147 | * Check whether the direct wrapping scope of a token is within a limited set of |
||
1148 | * acceptable tokens. |
||
1149 | * |
||
1150 | * Used to check, for instance, if a T_CONST is a class constant. |
||
1151 | * |
||
1152 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1153 | * @param int $stackPtr The position in the stack of the |
||
1154 | * T_CONST token to verify. |
||
1155 | * @param array $validScopes Array of token types. |
||
1156 | * Keys should be the token types in string |
||
1157 | * format to allow for newer token types. |
||
1158 | * Value is irrelevant. |
||
1159 | * |
||
1160 | * @return bool |
||
1161 | */ |
||
1162 | protected function validDirectScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes) |
||
1186 | |||
1187 | |||
1188 | /** |
||
1189 | * Get an array of just the type hints from a function declaration. |
||
1190 | * |
||
1191 | * Expects to be passed T_FUNCTION or T_CLOSURE token. |
||
1192 | * |
||
1193 | * Strips potential nullable indicator and potential global namespace |
||
1194 | * indicator from the type hints before returning them. |
||
1195 | * |
||
1196 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
1197 | * @param int $stackPtr The position of the token. |
||
1198 | * |
||
1199 | * @return array Array with type hints or an empty array if |
||
1200 | * - the function does not have any parameters |
||
1201 | * - no type hints were found |
||
1202 | * - or the passed token was not of the correct type. |
||
1203 | */ |
||
1204 | public function getTypeHintsFromFunctionDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1237 | |||
1238 | |||
1239 | /** |
||
1240 | * Returns the method parameters for the specified function token. |
||
1241 | * |
||
1242 | * Each parameter is in the following format: |
||
1243 | * |
||
1244 | * <code> |
||
1245 | * 0 => array( |
||
1246 | * 'token' => int, // The position of the var in the token stack. |
||
1247 | * 'name' => '$var', // The variable name. |
||
1248 | * 'content' => string, // The full content of the variable definition. |
||
1249 | * 'pass_by_reference' => boolean, // Is the variable passed by reference? |
||
1250 | * 'variable_length' => boolean, // Is the param of variable length through use of `...` ? |
||
1251 | * 'type_hint' => string, // The type hint for the variable. |
||
1252 | * 'nullable_type' => boolean, // Is the variable using a nullable type? |
||
1253 | * ) |
||
1254 | * </code> |
||
1255 | * |
||
1256 | * Parameters with default values have an additional array index of |
||
1257 | * 'default' with the value of the default as a string. |
||
1258 | * |
||
1259 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
1260 | * class, but with some improvements which have been introduced in |
||
1261 | * PHPCS 2.8.0. |
||
1262 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1117}, |
||
1263 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} and |
||
1264 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1293}. |
||
1265 | * |
||
1266 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
1267 | * that, this method can be removed and calls to it replaced with |
||
1268 | * `$phpcsFile->getMethodParameters($stackPtr)` calls. |
||
1269 | * |
||
1270 | * NOTE: This version does not deal with the new T_NULLABLE token type. |
||
1271 | * This token is included upstream only in 2.8.0+ and as we defer to upstream |
||
1272 | * in that case, no need to deal with it here. |
||
1273 | * |
||
1274 | * Last synced with PHPCS version: PHPCS 2.9.0-alpha at commit f1511adad043edfd6d2e595e77385c32577eb2bc}} |
||
1275 | * |
||
1276 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1277 | * @param int $stackPtr The position in the stack of the |
||
1278 | * function token to acquire the |
||
1279 | * parameters for. |
||
1280 | * |
||
1281 | * @return array|false |
||
1282 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
1283 | * type T_FUNCTION or T_CLOSURE. |
||
1284 | */ |
||
1285 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1442 | |||
1443 | |||
1444 | /** |
||
1445 | * Get the hash algorithm name from the parameter in a hash function call. |
||
1446 | * |
||
1447 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1448 | * @param int $stackPtr The position of the T_STRING function token. |
||
1449 | * |
||
1450 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
1451 | * function call or false if it was not. |
||
1452 | */ |
||
1453 | public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1486 | |||
1487 | }//end class |
||
1488 |
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.