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 | /* The testVersion configuration variable may be in any of the following formats: |
||
60 | * 1) Omitted/empty, in which case no version is specified. This effectively |
||
61 | * disables all the checks provided by this standard. |
||
62 | * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
||
63 | * the code will run on that version of PHP (no deprecated features or newer |
||
64 | * features being used). |
||
65 | * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
||
66 | * on all PHP versions in that range, and that it doesn't use any features that |
||
67 | * were deprecated by the final version in the list, or which were not available |
||
68 | * for the first version in the list. |
||
69 | * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
||
70 | * would be treated as invalid, and ignored. |
||
71 | * This standard doesn't support checking against PHP4, so the minimum version that |
||
72 | * is recognised is "5.0". |
||
73 | */ |
||
74 | |||
75 | private function getTestVersion() |
||
116 | |||
117 | View Code Duplication | public function supportsAbove($phpVersion) |
|
130 | |||
131 | View Code Duplication | public function supportsBelow($phpVersion) |
|
144 | |||
145 | |||
146 | /** |
||
147 | * Add a PHPCS message to the output stack as either a warning or an error. |
||
148 | * |
||
149 | * @param PHP_CodeSniffer_File $phpcsFile The file the message applies to. |
||
150 | * @param string $message The message. |
||
151 | * @param int $stackPtr The position of the token |
||
152 | * the message relates to. |
||
153 | * @param bool $isError Whether to report the message as an |
||
154 | * 'error' or 'warning'. |
||
155 | * Defaults to true (error). |
||
156 | * @param string $code The error code for the message. |
||
157 | * Defaults to 'Found'. |
||
158 | * @param array $data Optional input for the data replacements. |
||
159 | * |
||
160 | * @return void |
||
161 | */ |
||
162 | public function addMessage($phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array()) |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Convert an arbitrary string to an alphanumeric string with underscores. |
||
174 | * |
||
175 | * Pre-empt issues with arbitrary strings being used as error codes in XML and PHP. |
||
176 | * |
||
177 | * @param string $baseString Arbitrary string. |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | public function stringToErrorCode($baseString) |
||
185 | |||
186 | |||
187 | /** |
||
188 | * Strip quotes surrounding an arbitrary string. |
||
189 | * |
||
190 | * Intended for use with the content of a T_CONSTANT_ENCAPSED_STRING / T_DOUBLE_QUOTED_STRING. |
||
191 | * |
||
192 | * @param string $string The raw string. |
||
193 | * |
||
194 | * @return string String without quotes around it. |
||
195 | */ |
||
196 | public function stripQuotes($string) { |
||
199 | |||
200 | |||
201 | /** |
||
202 | * Strip variables from an arbitrary double quoted string. |
||
203 | * |
||
204 | * Intended for use with the content of a T_DOUBLE_QUOTED_STRING. |
||
205 | * |
||
206 | * @param string $string The raw string. |
||
207 | * |
||
208 | * @return string String without variables in it. |
||
209 | */ |
||
210 | public function stripVariables($string) { |
||
217 | |||
218 | |||
219 | /** |
||
220 | * Make all top level array keys in an array lowercase. |
||
221 | * |
||
222 | * @param array $array Initial array. |
||
223 | * |
||
224 | * @return array Same array, but with all lowercase top level keys. |
||
225 | */ |
||
226 | public function arrayKeysToLowercase($array) |
||
232 | |||
233 | |||
234 | /** |
||
235 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
236 | * |
||
237 | * Returns FALSE on error or if there are no implemented interface names. |
||
238 | * |
||
239 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
240 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
241 | * that, this method can be removed and call to it replaced with |
||
242 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
243 | * |
||
244 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
245 | * @param int $stackPtr The position of the class token. |
||
246 | * |
||
247 | * @return array|false |
||
248 | */ |
||
249 | public function findImplementedInterfaceNames(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
296 | |||
297 | |||
298 | /** |
||
299 | * Checks if a function call has parameters. |
||
300 | * |
||
301 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
302 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
303 | * |
||
304 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, it |
||
305 | * will detect whether the array has values or is empty. |
||
306 | * |
||
307 | * @link https://github.com/wimg/PHPCompatibility/issues/120 |
||
308 | * @link https://github.com/wimg/PHPCompatibility/issues/152 |
||
309 | * |
||
310 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
311 | * @param int $stackPtr The position of the function call token. |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
366 | |||
367 | |||
368 | /** |
||
369 | * Count the number of parameters a function call has been passed. |
||
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, |
||
375 | * it will return the number of values in the array. |
||
376 | * |
||
377 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
378 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
379 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
380 | * |
||
381 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
382 | * @param int $stackPtr The position of the function call token. |
||
383 | * |
||
384 | * @return int |
||
385 | */ |
||
386 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
394 | |||
395 | |||
396 | /** |
||
397 | * Get information on all parameters passed to a function call. |
||
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 | * Will return an multi-dimentional array with the start token pointer, end token |
||
403 | * pointer and raw parameter value for all parameters. Index will be 1-based. |
||
404 | * If no parameters are found, will return an empty array. |
||
405 | * |
||
406 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
||
407 | * it will tokenize the values / key/value pairs contained in the array call. |
||
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 array |
||
413 | */ |
||
414 | public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
497 | |||
498 | |||
499 | /** |
||
500 | * Get information on a specific parameter passed to a function call. |
||
501 | * |
||
502 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
503 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
504 | * |
||
505 | * Will return a array with the start token pointer, end token pointer and the raw value |
||
506 | * of the parameter at a specific offset. |
||
507 | * If the specified parameter is not found, will return false. |
||
508 | * |
||
509 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
510 | * @param int $stackPtr The position of the function call token. |
||
511 | * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
||
512 | * |
||
513 | * @return array|false |
||
514 | */ |
||
515 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
||
526 | |||
527 | |||
528 | /** |
||
529 | * Verify whether a token is within a scoped condition. |
||
530 | * |
||
531 | * If the optional $validScopes parameter has been passed, the function |
||
532 | * will check that the token has at least one condition which is of a |
||
533 | * type defined in $validScopes. |
||
534 | * |
||
535 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
536 | * @param int $stackPtr The position of the token. |
||
537 | * @param array|int $validScopes Optional. Array of valid scopes |
||
538 | * or int value of a valid scope. |
||
539 | * Pass the T_.. constant(s) for the |
||
540 | * desired scope to this parameter. |
||
541 | * |
||
542 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
543 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
544 | * valid scope, false otherwise. |
||
545 | */ |
||
546 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
567 | |||
568 | |||
569 | /** |
||
570 | * Verify whether a token is within a class scope. |
||
571 | * |
||
572 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
573 | * @param int $stackPtr The position of the token. |
||
574 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
575 | * scope or also accept interfaces and traits |
||
576 | * as scope. |
||
577 | * |
||
578 | * @return bool True if within class scope, false otherwise. |
||
579 | */ |
||
580 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
594 | |||
595 | |||
596 | /** |
||
597 | * Verify whether a token is within a scoped use statement. |
||
598 | * |
||
599 | * PHPCS cross-version compatibility method. |
||
600 | * |
||
601 | * In PHPCS 1.x no conditions are set for a scoped use statement. |
||
602 | * This method works around that limitation. |
||
603 | * |
||
604 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
605 | * @param int $stackPtr The position of the token. |
||
606 | * |
||
607 | * @return bool True if within use scope, false otherwise. |
||
608 | */ |
||
609 | public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
641 | |||
642 | |||
643 | /** |
||
644 | * Returns the fully qualified class name for a new class instantiation. |
||
645 | * |
||
646 | * Returns an empty string if the class name could not be reliably inferred. |
||
647 | * |
||
648 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
649 | * @param int $stackPtr The position of a T_NEW token. |
||
650 | * |
||
651 | * @return string |
||
652 | */ |
||
653 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
685 | |||
686 | |||
687 | /** |
||
688 | * Returns the fully qualified name of the class that the specified class extends. |
||
689 | * |
||
690 | * Returns an empty string if the class does not extend another class or if |
||
691 | * the class name could not be reliably inferred. |
||
692 | * |
||
693 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
694 | * @param int $stackPtr The position of a T_CLASS token. |
||
695 | * |
||
696 | * @return string |
||
697 | */ |
||
698 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
718 | |||
719 | |||
720 | /** |
||
721 | * Returns the class name for the static usage of a class. |
||
722 | * This can be a call to a method, the use of a property or constant. |
||
723 | * |
||
724 | * Returns an empty string if 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_NEW token. |
||
728 | * |
||
729 | * @return string |
||
730 | */ |
||
731 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
777 | |||
778 | |||
779 | /** |
||
780 | * Get the Fully Qualified name for a class/function/constant etc. |
||
781 | * |
||
782 | * Checks if a class/function/constant name is already fully qualified and |
||
783 | * if not, enrich it with the relevant namespace information. |
||
784 | * |
||
785 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
786 | * @param int $stackPtr The position of the token. |
||
787 | * @param string $name The class / function / constant name. |
||
788 | * |
||
789 | * @return string |
||
790 | */ |
||
791 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
812 | |||
813 | |||
814 | /** |
||
815 | * Is the class/function/constant name namespaced or global ? |
||
816 | * |
||
817 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
818 | * I.e. should always start with a `\` ! |
||
819 | * |
||
820 | * @return bool True if namespaced, false if global. |
||
821 | */ |
||
822 | public function isNamespaced($FQName) { |
||
829 | |||
830 | |||
831 | /** |
||
832 | * Determine the namespace name an arbitrary token lives in. |
||
833 | * |
||
834 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
835 | * @param int $stackPtr The token position for which to determine the namespace. |
||
836 | * |
||
837 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
838 | */ |
||
839 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
889 | |||
890 | /** |
||
891 | * Get the complete namespace name for a namespace declaration. |
||
892 | * |
||
893 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
894 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
895 | * |
||
896 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
897 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
898 | * |
||
899 | * @return string|false Namespace name or false if not a namespace declaration. |
||
900 | * Namespace name can be an empty string for global namespace declaration. |
||
901 | */ |
||
902 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
||
942 | |||
943 | |||
944 | /** |
||
945 | * Get the stack pointer for a return type token for a given function. |
||
946 | * |
||
947 | * Compatible layer for older PHPCS versions which don't recognize |
||
948 | * return type hints correctly. |
||
949 | * |
||
950 | * Expects to be passed T_RETURN_TYPE, T_FUNCTION or T_CLOSURE token. |
||
951 | * |
||
952 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
953 | * @param int $stackPtr The position of the token. |
||
954 | * |
||
955 | * @return int|false Stack pointer to the return type token or false if |
||
956 | * no return type was found or the passed token was |
||
957 | * not of the correct type. |
||
958 | */ |
||
959 | public function getReturnTypeHintToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
996 | |||
997 | |||
998 | /** |
||
999 | * Returns the method parameters for the specified function token. |
||
1000 | * |
||
1001 | * Each parameter is in the following format: |
||
1002 | * |
||
1003 | * <code> |
||
1004 | * 0 => array( |
||
1005 | * 'name' => '$var', // The variable name. |
||
1006 | * 'content' => string, // The full content of the variable definition. |
||
1007 | * 'pass_by_reference' => boolean, // Is the variable passed by reference? |
||
1008 | * 'type_hint' => string, // The type hint for the variable. |
||
1009 | * 'nullable_type' => boolean, // Is the variable using a nullable type? |
||
1010 | * ) |
||
1011 | * </code> |
||
1012 | * |
||
1013 | * Parameters with default values have an additional array index of |
||
1014 | * 'default' with the value of the default as a string. |
||
1015 | * |
||
1016 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
1017 | * class, but with some improvements which will probably be introduced in |
||
1018 | * PHPCS 2.7.2. {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1117}, |
||
1019 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} and |
||
1020 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1293}. |
||
1021 | * |
||
1022 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
1023 | * that, this method can be removed and calls to it replaced with |
||
1024 | * `$phpcsFile->getMethodParameters($stackPtr)` calls. |
||
1025 | * |
||
1026 | * NOTE: This version does not deal with the new T_NULLABLE token type. |
||
1027 | * This token is included upstream only in 2.7.2+ and as we defer to upstream |
||
1028 | * in that case, no need to deal with it here. |
||
1029 | * |
||
1030 | * Last synced with PHPCS version: PHPCS 2.7.2-alpha.}} |
||
1031 | * |
||
1032 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1033 | * @param int $stackPtr The position in the stack of the |
||
1034 | * function token to acquire the |
||
1035 | * parameters for. |
||
1036 | * |
||
1037 | * @return array|false |
||
1038 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
1039 | * type T_FUNCTION or T_CLOSURE. |
||
1040 | */ |
||
1041 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1197 | |||
1198 | |||
1199 | /** |
||
1200 | * Get the hash algorithm name from the parameter in a hash function call. |
||
1201 | * |
||
1202 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1203 | * @param int $stackPtr The position of the T_STRING function token. |
||
1204 | * |
||
1205 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
1206 | * function call or false if it was not. |
||
1207 | */ |
||
1208 | public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1243 | |||
1244 | }//end class |
||
1245 |
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.