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) |
||
| 584 | |||
| 585 | |||
| 586 | /** |
||
| 587 | * Verify whether a token is within a class scope. |
||
| 588 | * |
||
| 589 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 590 | * @param int $stackPtr The position of the token. |
||
| 591 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
| 592 | * scope or also accept interfaces and traits |
||
| 593 | * as scope. |
||
| 594 | * |
||
| 595 | * @return bool True if within class scope, false otherwise. |
||
| 596 | */ |
||
| 597 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
| 607 | |||
| 608 | |||
| 609 | /** |
||
| 610 | * Verify whether a token is within a scoped use statement. |
||
| 611 | * |
||
| 612 | * PHPCS cross-version compatibility method. |
||
| 613 | * |
||
| 614 | * In PHPCS 1.x no conditions are set for a scoped use statement. |
||
| 615 | * This method works around that limitation. |
||
| 616 | * |
||
| 617 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 618 | * @param int $stackPtr The position of the token. |
||
| 619 | * |
||
| 620 | * @return bool True if within use scope, false otherwise. |
||
| 621 | */ |
||
| 622 | public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 654 | |||
| 655 | |||
| 656 | /** |
||
| 657 | * Returns the fully qualified class name for a new class instantiation. |
||
| 658 | * |
||
| 659 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 660 | * |
||
| 661 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 662 | * @param int $stackPtr The position of a T_NEW token. |
||
| 663 | * |
||
| 664 | * @return string |
||
| 665 | */ |
||
| 666 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 698 | |||
| 699 | |||
| 700 | /** |
||
| 701 | * Returns the fully qualified name of the class that the specified class extends. |
||
| 702 | * |
||
| 703 | * Returns an empty string if the class does not extend another class or if |
||
| 704 | * the class name could not be reliably inferred. |
||
| 705 | * |
||
| 706 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 707 | * @param int $stackPtr The position of a T_CLASS token. |
||
| 708 | * |
||
| 709 | * @return string |
||
| 710 | */ |
||
| 711 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 731 | |||
| 732 | |||
| 733 | /** |
||
| 734 | * Returns the class name for the static usage of a class. |
||
| 735 | * This can be a call to a method, the use of a property or constant. |
||
| 736 | * |
||
| 737 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 738 | * |
||
| 739 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 740 | * @param int $stackPtr The position of a T_NEW token. |
||
| 741 | * |
||
| 742 | * @return string |
||
| 743 | */ |
||
| 744 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 790 | |||
| 791 | |||
| 792 | /** |
||
| 793 | * Get the Fully Qualified name for a class/function/constant etc. |
||
| 794 | * |
||
| 795 | * Checks if a class/function/constant name is already fully qualified and |
||
| 796 | * if not, enrich it with the relevant namespace information. |
||
| 797 | * |
||
| 798 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 799 | * @param int $stackPtr The position of the token. |
||
| 800 | * @param string $name The class / function / constant name. |
||
| 801 | * |
||
| 802 | * @return string |
||
| 803 | */ |
||
| 804 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
| 825 | |||
| 826 | |||
| 827 | /** |
||
| 828 | * Is the class/function/constant name namespaced or global ? |
||
| 829 | * |
||
| 830 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
| 831 | * I.e. should always start with a `\` ! |
||
| 832 | * |
||
| 833 | * @return bool True if namespaced, false if global. |
||
| 834 | */ |
||
| 835 | public function isNamespaced($FQName) { |
||
| 842 | |||
| 843 | |||
| 844 | /** |
||
| 845 | * Determine the namespace name an arbitrary token lives in. |
||
| 846 | * |
||
| 847 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 848 | * @param int $stackPtr The token position for which to determine the namespace. |
||
| 849 | * |
||
| 850 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
| 851 | */ |
||
| 852 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Get the complete namespace name for a namespace declaration. |
||
| 905 | * |
||
| 906 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
| 907 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
| 908 | * |
||
| 909 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 910 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
| 911 | * |
||
| 912 | * @return string|false Namespace name or false if not a namespace declaration. |
||
| 913 | * Namespace name can be an empty string for global namespace declaration. |
||
| 914 | */ |
||
| 915 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
||
| 955 | |||
| 956 | |||
| 957 | /** |
||
| 958 | * Returns the method parameters for the specified T_FUNCTION token. |
||
| 959 | * |
||
| 960 | * Each parameter is in the following format: |
||
| 961 | * |
||
| 962 | * <code> |
||
| 963 | * 0 => array( |
||
| 964 | * 'name' => '$var', // The variable name. |
||
| 965 | * 'pass_by_reference' => false, // Passed by reference. |
||
| 966 | * 'type_hint' => string, // Type hint for array or custom type |
||
| 967 | * 'nullable_type' => bool, // Whether the type given in the type hint is nullable |
||
| 968 | * 'type_hint' => string, // Type hint for array or custom type |
||
| 969 | * 'raw' => string, // Raw content of the tokens for the parameter |
||
| 970 | * ) |
||
| 971 | * </code> |
||
| 972 | * |
||
| 973 | * Parameters with default values have an additional array index of |
||
| 974 | * 'default' with the value of the default as a string. |
||
| 975 | * |
||
| 976 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
| 977 | * class, but with some improvements which will probably be introduced in |
||
| 978 | * PHPCS 2.7.1/2.8. {@see https://github.com/squizlabs/PHP_CodeSniffer/pull/1117} |
||
| 979 | * and {@see https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} |
||
| 980 | * |
||
| 981 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
| 982 | * that, this method can be removed and calls to it replaced with |
||
| 983 | * `$phpcsFile->getMethodParameters($stackPtr)` calls. |
||
| 984 | * |
||
| 985 | * Last synced with PHPCS version: PHPCS 2.7.}} |
||
| 986 | * |
||
| 987 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 988 | * @param int $stackPtr The position in the stack of the |
||
| 989 | * T_FUNCTION token to acquire the |
||
| 990 | * parameters for. |
||
| 991 | * |
||
| 992 | * @return array|false |
||
| 993 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
| 994 | * type T_FUNCTION. |
||
| 995 | */ |
||
| 996 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1150 | |||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * Get the hash algorithm name from the parameter in a hash function call. |
||
| 1154 | * |
||
| 1155 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 1156 | * @param int $stackPtr The position of the T_STRING function token. |
||
| 1157 | * |
||
| 1158 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
| 1159 | * function call or false if it was not. |
||
| 1160 | */ |
||
| 1161 | public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1196 | |||
| 1197 | }//end class |
||
| 1198 |
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.