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 | * @link https://github.com/wimg/PHPCompatibility/issues/120 |
||
| 305 | * @link https://github.com/wimg/PHPCompatibility/issues/152 |
||
| 306 | * |
||
| 307 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 308 | * @param int $stackPtr The position of the function call token. |
||
| 309 | * |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * Count the number of parameters a function call has been passed. |
||
| 349 | * |
||
| 350 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 351 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 352 | * |
||
| 353 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
| 354 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
| 355 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
| 356 | * |
||
| 357 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 358 | * @param int $stackPtr The position of the function call token. |
||
| 359 | * |
||
| 360 | * @return int |
||
| 361 | */ |
||
| 362 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 370 | |||
| 371 | |||
| 372 | /** |
||
| 373 | * Get information on all parameters passed to a function call. |
||
| 374 | * |
||
| 375 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 376 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 377 | * |
||
| 378 | * Will return an multi-dimentional array with the start token pointer, end token |
||
| 379 | * pointer and raw parameter value for all parameters. Index will be 1-based. |
||
| 380 | * If no parameters are found, will return an empty array. |
||
| 381 | * |
||
| 382 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 383 | * @param int $stackPtr The position of the function call token. |
||
| 384 | * |
||
| 385 | * @return array |
||
| 386 | */ |
||
| 387 | public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 459 | |||
| 460 | |||
| 461 | /** |
||
| 462 | * Get information on a specific parameter passed to a function call. |
||
| 463 | * |
||
| 464 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 465 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 466 | * |
||
| 467 | * Will return a array with the start token pointer, end token pointer and the raw value |
||
| 468 | * of the parameter at a specific offset. |
||
| 469 | * If the specified parameter is not found, will return false. |
||
| 470 | * |
||
| 471 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 472 | * @param int $stackPtr The position of the function call token. |
||
| 473 | * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
||
| 474 | * |
||
| 475 | * @return array|false |
||
| 476 | */ |
||
| 477 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
||
| 488 | |||
| 489 | |||
| 490 | /** |
||
| 491 | * Verify whether a token is within a scoped condition. |
||
| 492 | * |
||
| 493 | * If the optional $validScopes parameter has been passed, the function |
||
| 494 | * will check that the token has at least one condition which is of a |
||
| 495 | * type defined in $validScopes. |
||
| 496 | * |
||
| 497 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 498 | * @param int $stackPtr The position of the token. |
||
| 499 | * @param array|int $validScopes Optional. Array of valid scopes |
||
| 500 | * or int value of a valid scope. |
||
| 501 | * Pass the T_.. constant(s) for the |
||
| 502 | * desired scope to this parameter. |
||
| 503 | * |
||
| 504 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
| 505 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
| 506 | * valid scope, false otherwise. |
||
| 507 | */ |
||
| 508 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
| 546 | |||
| 547 | |||
| 548 | /** |
||
| 549 | * Verify whether a token is within a class scope. |
||
| 550 | * |
||
| 551 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 552 | * @param int $stackPtr The position of the token. |
||
| 553 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
| 554 | * scope or also accept interfaces and traits |
||
| 555 | * as scope. |
||
| 556 | * |
||
| 557 | * @return bool True if within class scope, false otherwise. |
||
| 558 | */ |
||
| 559 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
| 569 | |||
| 570 | |||
| 571 | /** |
||
| 572 | * Verify whether a token is within a scoped use statement. |
||
| 573 | * |
||
| 574 | * PHPCS cross-version compatibility method. |
||
| 575 | * |
||
| 576 | * In PHPCS 1.x no conditions are set for a scoped use statement. |
||
| 577 | * This method works around that limitation. |
||
| 578 | * |
||
| 579 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 580 | * @param int $stackPtr The position of the token. |
||
| 581 | * |
||
| 582 | * @return bool True if within use scope, false otherwise. |
||
| 583 | */ |
||
| 584 | public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 616 | |||
| 617 | |||
| 618 | /** |
||
| 619 | * Returns the fully qualified class name for a new class instantiation. |
||
| 620 | * |
||
| 621 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 622 | * |
||
| 623 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 624 | * @param int $stackPtr The position of a T_NEW token. |
||
| 625 | * |
||
| 626 | * @return string |
||
| 627 | */ |
||
| 628 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 660 | |||
| 661 | |||
| 662 | /** |
||
| 663 | * Returns the fully qualified name of the class that the specified class extends. |
||
| 664 | * |
||
| 665 | * Returns an empty string if the class does not extend another class or if |
||
| 666 | * the class name could not be reliably inferred. |
||
| 667 | * |
||
| 668 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 669 | * @param int $stackPtr The position of a T_CLASS token. |
||
| 670 | * |
||
| 671 | * @return string |
||
| 672 | */ |
||
| 673 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 693 | |||
| 694 | |||
| 695 | /** |
||
| 696 | * Returns the class name for the static usage of a class. |
||
| 697 | * This can be a call to a method, the use of a property or constant. |
||
| 698 | * |
||
| 699 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 700 | * |
||
| 701 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 702 | * @param int $stackPtr The position of a T_NEW token. |
||
| 703 | * |
||
| 704 | * @return string |
||
| 705 | */ |
||
| 706 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 752 | |||
| 753 | |||
| 754 | /** |
||
| 755 | * Get the Fully Qualified name for a class/function/constant etc. |
||
| 756 | * |
||
| 757 | * Checks if a class/function/constant name is already fully qualified and |
||
| 758 | * if not, enrich it with the relevant namespace information. |
||
| 759 | * |
||
| 760 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 761 | * @param int $stackPtr The position of the token. |
||
| 762 | * @param string $name The class / function / constant name. |
||
| 763 | * |
||
| 764 | * @return string |
||
| 765 | */ |
||
| 766 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
| 787 | |||
| 788 | |||
| 789 | /** |
||
| 790 | * Is the class/function/constant name namespaced or global ? |
||
| 791 | * |
||
| 792 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
| 793 | * I.e. should always start with a `\` ! |
||
| 794 | * |
||
| 795 | * @return bool True if namespaced, false if global. |
||
| 796 | */ |
||
| 797 | public function isNamespaced($FQName) { |
||
| 804 | |||
| 805 | |||
| 806 | /** |
||
| 807 | * Determine the namespace name an arbitrary token lives in. |
||
| 808 | * |
||
| 809 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 810 | * @param int $stackPtr The token position for which to determine the namespace. |
||
| 811 | * |
||
| 812 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
| 813 | */ |
||
| 814 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 864 | |||
| 865 | /** |
||
| 866 | * Get the complete namespace name for a namespace declaration. |
||
| 867 | * |
||
| 868 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
| 869 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
| 870 | * |
||
| 871 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 872 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
| 873 | * |
||
| 874 | * @return string|false Namespace name or false if not a namespace declaration. |
||
| 875 | * Namespace name can be an empty string for global namespace declaration. |
||
| 876 | */ |
||
| 877 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
||
| 917 | |||
| 918 | |||
| 919 | /** |
||
| 920 | * Returns the method parameters for the specified T_FUNCTION token. |
||
| 921 | * |
||
| 922 | * Each parameter is in the following format: |
||
| 923 | * |
||
| 924 | * <code> |
||
| 925 | * 0 => array( |
||
| 926 | * 'name' => '$var', // The variable name. |
||
| 927 | * 'pass_by_reference' => false, // Passed by reference. |
||
| 928 | * 'type_hint' => string, // Type hint for array or custom type |
||
| 929 | * 'nullable_type' => bool, // Whether the type given in the type hint is nullable |
||
| 930 | * 'type_hint' => string, // Type hint for array or custom type |
||
| 931 | * 'raw' => string, // Raw content of the tokens for the parameter |
||
| 932 | * ) |
||
| 933 | * </code> |
||
| 934 | * |
||
| 935 | * Parameters with default values have an additional array index of |
||
| 936 | * 'default' with the value of the default as a string. |
||
| 937 | * |
||
| 938 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
| 939 | * class, but with some improvements which will probably be introduced in |
||
| 940 | * PHPCS 2.7.1/2.8. {@see https://github.com/squizlabs/PHP_CodeSniffer/pull/1117} |
||
| 941 | * and {@see https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} |
||
| 942 | * |
||
| 943 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
| 944 | * that, this method can be removed and calls to it replaced with |
||
| 945 | * `$phpcsFile->getMethodParameters($stackPtr)` calls. |
||
| 946 | * |
||
| 947 | * Last synced with PHPCS version: PHPCS 2.7.}} |
||
| 948 | * |
||
| 949 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 950 | * @param int $stackPtr The position in the stack of the |
||
| 951 | * T_FUNCTION token to acquire the |
||
| 952 | * parameters for. |
||
| 953 | * |
||
| 954 | * @return array|false |
||
| 955 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
| 956 | * type T_FUNCTION. |
||
| 957 | */ |
||
| 958 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1112 | |||
| 1113 | |||
| 1114 | /** |
||
| 1115 | * Get the hash algorithm name from the parameter in a hash function call. |
||
| 1116 | * |
||
| 1117 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 1118 | * @param int $stackPtr The position of the T_STRING function token. |
||
| 1119 | * |
||
| 1120 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
| 1121 | * function call or false if it was not. |
||
| 1122 | */ |
||
| 1123 | public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1158 | |||
| 1159 | }//end class |
||
| 1160 |
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.