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 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 Sniff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | abstract class Sniff implements \PHP_CodeSniffer_Sniff |
||
24 | { |
||
25 | |||
26 | const REGEX_COMPLEX_VARS = '`(?:(\{)?(?<!\\\\)\$)?(\{)?(?<!\\\\)\$(\{)?(?P<varname>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:->\$?(?P>varname)|\[[^\]]+\]|::\$?(?P>varname)|\([^\)]*\))*(?(3)\}|)(?(2)\}|)(?(1)\}|)`'; |
||
27 | |||
28 | /** |
||
29 | * List of superglobals as an array of strings. |
||
30 | * |
||
31 | * Used by the ParameterShadowSuperGlobals and ForbiddenClosureUseVariableNames sniffs. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $superglobals = array( |
||
36 | '$GLOBALS', |
||
37 | '$_SERVER', |
||
38 | '$_GET', |
||
39 | '$_POST', |
||
40 | '$_FILES', |
||
41 | '$_COOKIE', |
||
42 | '$_SESSION', |
||
43 | '$_REQUEST', |
||
44 | '$_ENV', |
||
45 | ); |
||
46 | |||
47 | /** |
||
48 | * List of functions using hash algorithm as parameter (always the first parameter). |
||
49 | * |
||
50 | * Used by the new/removed hash algorithm sniffs. |
||
51 | * Key is the function name, value is the 1-based parameter position in the function call. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $hashAlgoFunctions = array( |
||
56 | 'hash_file' => 1, |
||
57 | 'hash_hmac_file' => 1, |
||
58 | 'hash_hmac' => 1, |
||
59 | 'hash_init' => 1, |
||
60 | 'hash_pbkdf2' => 1, |
||
61 | 'hash' => 1, |
||
62 | ); |
||
63 | |||
64 | |||
65 | /** |
||
66 | * List of functions which take an ini directive as parameter (always the first parameter). |
||
67 | * |
||
68 | * Used by the new/removed ini directives sniffs. |
||
69 | * Key is the function name, value is the 1-based parameter position in the function call. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $iniFunctions = array( |
||
74 | 'ini_get' => 1, |
||
75 | 'ini_set' => 1, |
||
76 | ); |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Get the testVersion configuration variable. |
||
81 | * |
||
82 | * The testVersion configuration variable may be in any of the following formats: |
||
83 | * 1) Omitted/empty, in which case no version is specified. This effectively |
||
84 | * disables all the checks for new PHP features provided by this standard. |
||
85 | * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
||
86 | * the code will run on that version of PHP (no deprecated features or newer |
||
87 | * features being used). |
||
88 | * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
||
89 | * on all PHP versions in that range, and that it doesn't use any features that |
||
90 | * were deprecated by the final version in the list, or which were not available |
||
91 | * for the first version in the list. |
||
92 | * We accept ranges where one of the components is missing, e.g. "-5.6" means |
||
93 | * all versions up to PHP 5.6, and "7.0-" means all versions above PHP 7.0. |
||
94 | * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
||
95 | * would be treated as invalid, and ignored. |
||
96 | * |
||
97 | * @return array $arrTestVersions will hold an array containing min/max version |
||
98 | * of PHP that we are checking against (see above). If only a |
||
99 | * single version number is specified, then this is used as |
||
100 | * both the min and max. |
||
101 | * |
||
102 | * @throws \PHP_CodeSniffer_Exception If testVersion is invalid. |
||
103 | */ |
||
104 | 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(\PHP_CodeSniffer_File $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) |
||
264 | |||
265 | |||
266 | /** |
||
267 | * Strip variables from an arbitrary double quoted string. |
||
268 | * |
||
269 | * Intended for use with the content of a T_DOUBLE_QUOTED_STRING. |
||
270 | * |
||
271 | * @param string $string The raw string. |
||
272 | * |
||
273 | * @return string String without variables in it. |
||
274 | */ |
||
275 | public function stripVariables($string) |
||
283 | |||
284 | |||
285 | /** |
||
286 | * Make all top level array keys in an array lowercase. |
||
287 | * |
||
288 | * @param array $array Initial array. |
||
289 | * |
||
290 | * @return array Same array, but with all lowercase top level keys. |
||
291 | */ |
||
292 | public function arrayKeysToLowercase($array) |
||
298 | |||
299 | |||
300 | /** |
||
301 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
302 | * |
||
303 | * Returns FALSE on error or if there are no implemented interface names. |
||
304 | * |
||
305 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
306 | * This method also includes an improvement we use which was only introduced |
||
307 | * in PHPCS 2.8.0, so only defer to upstream for higher versions. |
||
308 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
309 | * that, this method can be removed and calls to it replaced with |
||
310 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
311 | * |
||
312 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
313 | * @param int $stackPtr The position of the class token. |
||
314 | * |
||
315 | * @return array|false |
||
316 | */ |
||
317 | public function findImplementedInterfaceNames(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
366 | |||
367 | |||
368 | /** |
||
369 | * Checks if a function call has parameters. |
||
370 | * |
||
371 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
372 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
373 | * |
||
374 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, it |
||
375 | * will detect whether the array has values or is empty. |
||
376 | * |
||
377 | * @link https://github.com/wimg/PHPCompatibility/issues/120 |
||
378 | * @link https://github.com/wimg/PHPCompatibility/issues/152 |
||
379 | * |
||
380 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
381 | * @param int $stackPtr The position of the function call token. |
||
382 | * |
||
383 | * @return bool |
||
384 | */ |
||
385 | public function doesFunctionCallHaveParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
435 | |||
436 | |||
437 | /** |
||
438 | * Count the number of parameters a function call has been passed. |
||
439 | * |
||
440 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
441 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
442 | * |
||
443 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
||
444 | * it will return the number of values in the array. |
||
445 | * |
||
446 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
447 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
448 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
449 | * |
||
450 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
451 | * @param int $stackPtr The position of the function call token. |
||
452 | * |
||
453 | * @return int |
||
454 | */ |
||
455 | public function getFunctionCallParameterCount(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
463 | |||
464 | |||
465 | /** |
||
466 | * Get information on all parameters passed to a function call. |
||
467 | * |
||
468 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
469 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
470 | * |
||
471 | * Will return an multi-dimentional array with the start token pointer, end token |
||
472 | * pointer and raw parameter value for all parameters. Index will be 1-based. |
||
473 | * If no parameters are found, will return an empty array. |
||
474 | * |
||
475 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
||
476 | * it will tokenize the values / key/value pairs contained in the array call. |
||
477 | * |
||
478 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
479 | * @param int $stackPtr The position of the function call token. |
||
480 | * |
||
481 | * @return array |
||
482 | */ |
||
483 | public function getFunctionCallParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
561 | |||
562 | |||
563 | /** |
||
564 | * Get information on a specific parameter passed to a function call. |
||
565 | * |
||
566 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
567 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
568 | * |
||
569 | * Will return a array with the start token pointer, end token pointer and the raw value |
||
570 | * of the parameter at a specific offset. |
||
571 | * If the specified parameter is not found, will return false. |
||
572 | * |
||
573 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
574 | * @param int $stackPtr The position of the function call token. |
||
575 | * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
||
576 | * |
||
577 | * @return array|false |
||
578 | */ |
||
579 | public function getFunctionCallParameter(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
||
589 | |||
590 | |||
591 | /** |
||
592 | * Verify whether a token is within a scoped condition. |
||
593 | * |
||
594 | * If the optional $validScopes parameter has been passed, the function |
||
595 | * will check that the token has at least one condition which is of a |
||
596 | * type defined in $validScopes. |
||
597 | * |
||
598 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
599 | * @param int $stackPtr The position of the token. |
||
600 | * @param array|int $validScopes Optional. Array of valid scopes |
||
601 | * or int value of a valid scope. |
||
602 | * Pass the T_.. constant(s) for the |
||
603 | * desired scope to this parameter. |
||
604 | * |
||
605 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
606 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
607 | * valid scope, false otherwise. |
||
608 | */ |
||
609 | public function tokenHasScope(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
630 | |||
631 | |||
632 | /** |
||
633 | * Verify whether a token is within a class scope. |
||
634 | * |
||
635 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
636 | * @param int $stackPtr The position of the token. |
||
637 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
638 | * scope or also accept interfaces and traits |
||
639 | * as scope. |
||
640 | * |
||
641 | * @return bool True if within class scope, false otherwise. |
||
642 | */ |
||
643 | public function inClassScope(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
657 | |||
658 | |||
659 | /** |
||
660 | * Verify whether a token is within a scoped use statement. |
||
661 | * |
||
662 | * PHPCS cross-version compatibility method. |
||
663 | * |
||
664 | * In PHPCS 1.x no conditions are set for a scoped use statement. |
||
665 | * This method works around that limitation. |
||
666 | * |
||
667 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
668 | * @param int $stackPtr The position of the token. |
||
669 | * |
||
670 | * @return bool True if within use scope, false otherwise. |
||
671 | */ |
||
672 | public function inUseScope(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
704 | |||
705 | |||
706 | /** |
||
707 | * Returns the fully qualified class name for a new class instantiation. |
||
708 | * |
||
709 | * Returns an empty string if the class name could not be reliably inferred. |
||
710 | * |
||
711 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
712 | * @param int $stackPtr The position of a T_NEW token. |
||
713 | * |
||
714 | * @return string |
||
715 | */ |
||
716 | public function getFQClassNameFromNewToken(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
757 | |||
758 | |||
759 | /** |
||
760 | * Returns the fully qualified name of the class that the specified class extends. |
||
761 | * |
||
762 | * Returns an empty string if the class does not extend another class or if |
||
763 | * the class name could not be reliably inferred. |
||
764 | * |
||
765 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
766 | * @param int $stackPtr The position of a T_CLASS token. |
||
767 | * |
||
768 | * @return string |
||
769 | */ |
||
770 | public function getFQExtendedClassName(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
790 | |||
791 | |||
792 | /** |
||
793 | * Returns the class name for the static usage of a class. |
||
794 | * This can be a call to a method, the use of a property or constant. |
||
795 | * |
||
796 | * Returns an empty string if the class name could not be reliably inferred. |
||
797 | * |
||
798 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
799 | * @param int $stackPtr The position of a T_NEW token. |
||
800 | * |
||
801 | * @return string |
||
802 | */ |
||
803 | public function getFQClassNameFromDoubleColonToken(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
854 | |||
855 | |||
856 | /** |
||
857 | * Get the Fully Qualified name for a class/function/constant etc. |
||
858 | * |
||
859 | * Checks if a class/function/constant name is already fully qualified and |
||
860 | * if not, enrich it with the relevant namespace information. |
||
861 | * |
||
862 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
863 | * @param int $stackPtr The position of the token. |
||
864 | * @param string $name The class / function / constant name. |
||
865 | * |
||
866 | * @return string |
||
867 | */ |
||
868 | public function getFQName(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
888 | |||
889 | |||
890 | /** |
||
891 | * Is the class/function/constant name namespaced or global ? |
||
892 | * |
||
893 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
894 | * I.e. should always start with a `\`. |
||
895 | * |
||
896 | * @return bool True if namespaced, false if global. |
||
897 | */ |
||
898 | public function isNamespaced($FQName) |
||
906 | |||
907 | |||
908 | /** |
||
909 | * Determine the namespace name an arbitrary token lives in. |
||
910 | * |
||
911 | * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
912 | * @param int $stackPtr The token position for which to determine the namespace. |
||
913 | * |
||
914 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
915 | */ |
||
916 | 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 | * Returns the name of the class that the specified class extends. |
||
1446 | * |
||
1447 | * Returns FALSE on error or if there is no extended class name. |
||
1448 | * |
||
1449 | * {@internal Duplicate of same method as contained in the `\PHP_CodeSniffer_File` |
||
1450 | * class, but with some improvements which have been introduced in |
||
1451 | * PHPCS 2.8.0. |
||
1452 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/commit/0011d448119d4c568e3ac1f825ae78815bf2cc34}. |
||
1453 | * |
||
1454 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
1455 | * that, this method can be removed and calls to it replaced with |
||
1456 | * `$phpcsFile->findExtendedClassName($stackPtr)` calls. |
||
1457 | * |
||
1458 | * Last synced with PHPCS version: PHPCS 2.9.0 at commit b940fb7dca8c2a37f0514161b495363e5b36d879}} |
||
1459 | * |
||
1460 | * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1461 | * @param int $stackPtr The position in the stack of the |
||
1462 | * class token. |
||
1463 | * |
||
1464 | * @return string|false |
||
1465 | */ |
||
1466 | public function findExtendedClassName(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1512 | |||
1513 | |||
1514 | /** |
||
1515 | * Get the hash algorithm name from the parameter in a hash function call. |
||
1516 | * |
||
1517 | * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1518 | * @param int $stackPtr The position of the T_STRING function token. |
||
1519 | * |
||
1520 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
1521 | * function call or false if it was not. |
||
1522 | */ |
||
1523 | public function getHashAlgorithmParameter(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1556 | |||
1557 | }//end class |
||
1558 |
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.