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 functions using hash algorithm as parameter (always the first parameter). |
||
29 | * |
||
30 | * Used by the new/removed hash algorithm sniffs. |
||
31 | * Key is the function name, value is the 1-based parameter position in the function call. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $hashAlgoFunctions = array( |
||
36 | 'hash_file' => 1, |
||
37 | 'hash_hmac_file' => 1, |
||
38 | 'hash_hmac' => 1, |
||
39 | 'hash_init' => 1, |
||
40 | 'hash_pbkdf2' => 1, |
||
41 | 'hash' => 1, |
||
42 | ); |
||
43 | |||
44 | |||
45 | /** |
||
46 | * List of functions which take an ini directive as parameter (always the first parameter). |
||
47 | * |
||
48 | * Used by the new/removed ini directives sniffs. |
||
49 | * Key is the function name, value is the 1-based parameter position in the function call. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $iniFunctions = array( |
||
54 | 'ini_get' => 1, |
||
55 | 'ini_set' => 1, |
||
56 | ); |
||
57 | |||
58 | |||
59 | /** |
||
60 | * Get the testVersion configuration variable. |
||
61 | * |
||
62 | * The testVersion configuration variable may be in any of the following formats: |
||
63 | * 1) Omitted/empty, in which case no version is specified. This effectively |
||
64 | * disables all the checks for new PHP features provided by this standard. |
||
65 | * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
||
66 | * the code will run on that version of PHP (no deprecated features or newer |
||
67 | * features being used). |
||
68 | * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
||
69 | * on all PHP versions in that range, and that it doesn't use any features that |
||
70 | * were deprecated by the final version in the list, or which were not available |
||
71 | * for the first version in the list. |
||
72 | * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
||
73 | * would be treated as invalid, and ignored. |
||
74 | * |
||
75 | * @return array $arrTestVersions will hold an array containing min/max version |
||
76 | * of PHP that we are checking against (see above). If only a |
||
77 | * single version number is specified, then this is used as |
||
78 | * both the min and max. |
||
79 | * |
||
80 | * @throws PHP_CodeSniffer_Exception If testVersion is invalid. |
||
81 | */ |
||
82 | private function getTestVersion() |
||
118 | |||
119 | |||
120 | /** |
||
121 | * Check whether a specific PHP version is equal to or higher than the maximum |
||
122 | * supported PHP version as provided by the user in `testVersion`. |
||
123 | * |
||
124 | * Should be used when sniffing for *old* PHP features (deprecated/removed). |
||
125 | * |
||
126 | * @param string $phpVersion A PHP version number in 'major.minor' format. |
||
127 | * |
||
128 | * @return bool True if testVersion has not been provided or if the PHP version |
||
129 | * is equal to or higher than the highest supported PHP version |
||
130 | * in testVersion. False otherwise. |
||
131 | */ |
||
132 | View Code Duplication | public function supportsAbove($phpVersion) |
|
145 | |||
146 | |||
147 | /** |
||
148 | * Check whether a specific PHP version is equal to or lower than the minimum |
||
149 | * supported PHP version as provided by the user in `testVersion`. |
||
150 | * |
||
151 | * Should be used when sniffing for *new* PHP features. |
||
152 | * |
||
153 | * @param string $phpVersion A PHP version number in 'major.minor' format. |
||
154 | * |
||
155 | * @return bool True if the PHP version is equal to or lower than the lowest |
||
156 | * supported PHP version in testVersion. |
||
157 | * False otherwise or if no testVersion is provided. |
||
158 | */ |
||
159 | View Code Duplication | public function supportsBelow($phpVersion) |
|
172 | |||
173 | |||
174 | /** |
||
175 | * Add a PHPCS message to the output stack as either a warning or an error. |
||
176 | * |
||
177 | * @param PHP_CodeSniffer_File $phpcsFile The file the message applies to. |
||
178 | * @param string $message The message. |
||
179 | * @param int $stackPtr The position of the token |
||
180 | * the message relates to. |
||
181 | * @param bool $isError Whether to report the message as an |
||
182 | * 'error' or 'warning'. |
||
183 | * Defaults to true (error). |
||
184 | * @param string $code The error code for the message. |
||
185 | * Defaults to 'Found'. |
||
186 | * @param array $data Optional input for the data replacements. |
||
187 | * |
||
188 | * @return void |
||
189 | */ |
||
190 | public function addMessage($phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array()) |
||
198 | |||
199 | |||
200 | /** |
||
201 | * Convert an arbitrary string to an alphanumeric string with underscores. |
||
202 | * |
||
203 | * Pre-empt issues with arbitrary strings being used as error codes in XML and PHP. |
||
204 | * |
||
205 | * @param string $baseString Arbitrary string. |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public function stringToErrorCode($baseString) |
||
213 | |||
214 | |||
215 | /** |
||
216 | * Strip quotes surrounding an arbitrary string. |
||
217 | * |
||
218 | * Intended for use with the content of a T_CONSTANT_ENCAPSED_STRING / T_DOUBLE_QUOTED_STRING. |
||
219 | * |
||
220 | * @param string $string The raw string. |
||
221 | * |
||
222 | * @return string String without quotes around it. |
||
223 | */ |
||
224 | public function stripQuotes($string) { |
||
227 | |||
228 | |||
229 | /** |
||
230 | * Strip variables from an arbitrary double quoted string. |
||
231 | * |
||
232 | * Intended for use with the content of a T_DOUBLE_QUOTED_STRING. |
||
233 | * |
||
234 | * @param string $string The raw string. |
||
235 | * |
||
236 | * @return string String without variables in it. |
||
237 | */ |
||
238 | public function stripVariables($string) { |
||
245 | |||
246 | |||
247 | /** |
||
248 | * Make all top level array keys in an array lowercase. |
||
249 | * |
||
250 | * @param array $array Initial array. |
||
251 | * |
||
252 | * @return array Same array, but with all lowercase top level keys. |
||
253 | */ |
||
254 | public function arrayKeysToLowercase($array) |
||
260 | |||
261 | |||
262 | /** |
||
263 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
264 | * |
||
265 | * Returns FALSE on error or if there are no implemented interface names. |
||
266 | * |
||
267 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
268 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
269 | * that, this method can be removed and call to it replaced with |
||
270 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
271 | * |
||
272 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
273 | * @param int $stackPtr The position of the class token. |
||
274 | * |
||
275 | * @return array|false |
||
276 | */ |
||
277 | public function findImplementedInterfaceNames(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
324 | |||
325 | |||
326 | /** |
||
327 | * Checks if a function call has parameters. |
||
328 | * |
||
329 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
330 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
331 | * |
||
332 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, it |
||
333 | * will detect whether the array has values or is empty. |
||
334 | * |
||
335 | * @link https://github.com/wimg/PHPCompatibility/issues/120 |
||
336 | * @link https://github.com/wimg/PHPCompatibility/issues/152 |
||
337 | * |
||
338 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
339 | * @param int $stackPtr The position of the function call token. |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
394 | |||
395 | |||
396 | /** |
||
397 | * Count the number of parameters a function call has been passed. |
||
398 | * |
||
399 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
400 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
401 | * |
||
402 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
||
403 | * it will return the number of values in the array. |
||
404 | * |
||
405 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
406 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
407 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
408 | * |
||
409 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
410 | * @param int $stackPtr The position of the function call token. |
||
411 | * |
||
412 | * @return int |
||
413 | */ |
||
414 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
422 | |||
423 | |||
424 | /** |
||
425 | * Get information on all parameters passed to a function call. |
||
426 | * |
||
427 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
428 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
429 | * |
||
430 | * Will return an multi-dimentional array with the start token pointer, end token |
||
431 | * pointer and raw parameter value for all parameters. Index will be 1-based. |
||
432 | * If no parameters are found, will return an empty array. |
||
433 | * |
||
434 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
||
435 | * it will tokenize the values / key/value pairs contained in the array call. |
||
436 | * |
||
437 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
438 | * @param int $stackPtr The position of the function call token. |
||
439 | * |
||
440 | * @return array |
||
441 | */ |
||
442 | public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
525 | |||
526 | |||
527 | /** |
||
528 | * Get information on a specific parameter passed to a function call. |
||
529 | * |
||
530 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
531 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
532 | * |
||
533 | * Will return a array with the start token pointer, end token pointer and the raw value |
||
534 | * of the parameter at a specific offset. |
||
535 | * If the specified parameter is not found, will return false. |
||
536 | * |
||
537 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
538 | * @param int $stackPtr The position of the function call token. |
||
539 | * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
||
540 | * |
||
541 | * @return array|false |
||
542 | */ |
||
543 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
||
554 | |||
555 | |||
556 | /** |
||
557 | * Verify whether a token is within a scoped condition. |
||
558 | * |
||
559 | * If the optional $validScopes parameter has been passed, the function |
||
560 | * will check that the token has at least one condition which is of a |
||
561 | * type defined in $validScopes. |
||
562 | * |
||
563 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
564 | * @param int $stackPtr The position of the token. |
||
565 | * @param array|int $validScopes Optional. Array of valid scopes |
||
566 | * or int value of a valid scope. |
||
567 | * Pass the T_.. constant(s) for the |
||
568 | * desired scope to this parameter. |
||
569 | * |
||
570 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
571 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
572 | * valid scope, false otherwise. |
||
573 | */ |
||
574 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
595 | |||
596 | |||
597 | /** |
||
598 | * Verify whether a token is within a class scope. |
||
599 | * |
||
600 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
601 | * @param int $stackPtr The position of the token. |
||
602 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
603 | * scope or also accept interfaces and traits |
||
604 | * as scope. |
||
605 | * |
||
606 | * @return bool True if within class scope, false otherwise. |
||
607 | */ |
||
608 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
622 | |||
623 | |||
624 | /** |
||
625 | * Verify whether a token is within a scoped use statement. |
||
626 | * |
||
627 | * PHPCS cross-version compatibility method. |
||
628 | * |
||
629 | * In PHPCS 1.x no conditions are set for a scoped use statement. |
||
630 | * This method works around that limitation. |
||
631 | * |
||
632 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
633 | * @param int $stackPtr The position of the token. |
||
634 | * |
||
635 | * @return bool True if within use scope, false otherwise. |
||
636 | */ |
||
637 | public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
669 | |||
670 | |||
671 | /** |
||
672 | * Returns the fully qualified class name for a new class instantiation. |
||
673 | * |
||
674 | * Returns an empty string if the class name could not be reliably inferred. |
||
675 | * |
||
676 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
677 | * @param int $stackPtr The position of a T_NEW token. |
||
678 | * |
||
679 | * @return string |
||
680 | */ |
||
681 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
718 | |||
719 | |||
720 | /** |
||
721 | * Returns the fully qualified name of the class that the specified class extends. |
||
722 | * |
||
723 | * Returns an empty string if the class does not extend another class or if |
||
724 | * the class name could not be reliably inferred. |
||
725 | * |
||
726 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
727 | * @param int $stackPtr The position of a T_CLASS token. |
||
728 | * |
||
729 | * @return string |
||
730 | */ |
||
731 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
751 | |||
752 | |||
753 | /** |
||
754 | * Returns the class name for the static usage of a class. |
||
755 | * This can be a call to a method, the use of a property or constant. |
||
756 | * |
||
757 | * Returns an empty string if the class name could not be reliably inferred. |
||
758 | * |
||
759 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
760 | * @param int $stackPtr The position of a T_NEW token. |
||
761 | * |
||
762 | * @return string |
||
763 | */ |
||
764 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
810 | |||
811 | |||
812 | /** |
||
813 | * Get the Fully Qualified name for a class/function/constant etc. |
||
814 | * |
||
815 | * Checks if a class/function/constant name is already fully qualified and |
||
816 | * if not, enrich it with the relevant namespace information. |
||
817 | * |
||
818 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
819 | * @param int $stackPtr The position of the token. |
||
820 | * @param string $name The class / function / constant name. |
||
821 | * |
||
822 | * @return string |
||
823 | */ |
||
824 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
845 | |||
846 | |||
847 | /** |
||
848 | * Is the class/function/constant name namespaced or global ? |
||
849 | * |
||
850 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
851 | * I.e. should always start with a `\` ! |
||
852 | * |
||
853 | * @return bool True if namespaced, false if global. |
||
854 | */ |
||
855 | public function isNamespaced($FQName) { |
||
862 | |||
863 | |||
864 | /** |
||
865 | * Determine the namespace name an arbitrary token lives in. |
||
866 | * |
||
867 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
868 | * @param int $stackPtr The token position for which to determine the namespace. |
||
869 | * |
||
870 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
871 | */ |
||
872 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
922 | |||
923 | /** |
||
924 | * Get the complete namespace name for a namespace declaration. |
||
925 | * |
||
926 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
927 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
928 | * |
||
929 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
930 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
931 | * |
||
932 | * @return string|false Namespace name or false if not a namespace declaration. |
||
933 | * Namespace name can be an empty string for global namespace declaration. |
||
934 | */ |
||
935 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
||
975 | |||
976 | |||
977 | /** |
||
978 | * Get the stack pointer for a return type token for a given function. |
||
979 | * |
||
980 | * Compatible layer for older PHPCS versions which don't recognize |
||
981 | * return type hints correctly. |
||
982 | * |
||
983 | * Expects to be passed T_RETURN_TYPE, T_FUNCTION or T_CLOSURE token. |
||
984 | * |
||
985 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
986 | * @param int $stackPtr The position of the token. |
||
987 | * |
||
988 | * @return int|false Stack pointer to the return type token or false if |
||
989 | * no return type was found or the passed token was |
||
990 | * not of the correct type. |
||
991 | */ |
||
992 | public function getReturnTypeHintToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1029 | |||
1030 | |||
1031 | /** |
||
1032 | * Returns the method parameters for the specified function token. |
||
1033 | * |
||
1034 | * Each parameter is in the following format: |
||
1035 | * |
||
1036 | * <code> |
||
1037 | * 0 => array( |
||
1038 | * 'name' => '$var', // The variable name. |
||
1039 | * 'content' => string, // The full content of the variable definition. |
||
1040 | * 'pass_by_reference' => boolean, // Is the variable passed by reference? |
||
1041 | * 'type_hint' => string, // The type hint for the variable. |
||
1042 | * 'nullable_type' => boolean, // Is the variable using a nullable type? |
||
1043 | * ) |
||
1044 | * </code> |
||
1045 | * |
||
1046 | * Parameters with default values have an additional array index of |
||
1047 | * 'default' with the value of the default as a string. |
||
1048 | * |
||
1049 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
1050 | * class, but with some improvements which have been introduced in |
||
1051 | * PHPCS 2.8.0. |
||
1052 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1117}, |
||
1053 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} and |
||
1054 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1293}. |
||
1055 | * |
||
1056 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
1057 | * that, this method can be removed and calls to it replaced with |
||
1058 | * `$phpcsFile->getMethodParameters($stackPtr)` calls. |
||
1059 | * |
||
1060 | * NOTE: This version does not deal with the new T_NULLABLE token type. |
||
1061 | * This token is included upstream only in 2.7.2+ and as we defer to upstream |
||
1062 | * in that case, no need to deal with it here. |
||
1063 | * |
||
1064 | * Last synced with PHPCS version: PHPCS 2.7.2-alpha.}} |
||
1065 | * |
||
1066 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1067 | * @param int $stackPtr The position in the stack of the |
||
1068 | * function token to acquire the |
||
1069 | * parameters for. |
||
1070 | * |
||
1071 | * @return array|false |
||
1072 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
1073 | * type T_FUNCTION or T_CLOSURE. |
||
1074 | */ |
||
1075 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1231 | |||
1232 | |||
1233 | /** |
||
1234 | * Get the hash algorithm name from the parameter in a hash function call. |
||
1235 | * |
||
1236 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1237 | * @param int $stackPtr The position of the T_STRING function token. |
||
1238 | * |
||
1239 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
1240 | * function call or false if it was not. |
||
1241 | */ |
||
1242 | public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1277 | |||
1278 | }//end class |
||
1279 |
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.