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 | /**  | 
            ||
| 26 | * List of functions using hash algorithm as parameter (always the first parameter).  | 
            ||
| 27 | *  | 
            ||
| 28 | * Used by the new/removed hash algorithm sniffs.  | 
            ||
| 29 | * Key is the function name, value is the 1-based parameter position in the function call.  | 
            ||
| 30 | *  | 
            ||
| 31 | * @var array  | 
            ||
| 32 | */  | 
            ||
| 33 | protected $hashAlgoFunctions = array(  | 
            ||
| 34 | 'hash_file' => 1,  | 
            ||
| 35 | 'hash_hmac_file' => 1,  | 
            ||
| 36 | 'hash_hmac' => 1,  | 
            ||
| 37 | 'hash_init' => 1,  | 
            ||
| 38 | 'hash_pbkdf2' => 1,  | 
            ||
| 39 | 'hash' => 1,  | 
            ||
| 40 | );  | 
            ||
| 41 | |||
| 42 | |||
| 43 | /**  | 
            ||
| 44 | * List of functions which take an ini directive as parameter (always the first parameter).  | 
            ||
| 45 | *  | 
            ||
| 46 | * Used by the new/removed ini directives sniffs.  | 
            ||
| 47 | * Key is the function name, value is the 1-based parameter position in the function call.  | 
            ||
| 48 | *  | 
            ||
| 49 | * @var array  | 
            ||
| 50 | */  | 
            ||
| 51 | protected $iniFunctions = array(  | 
            ||
| 52 | 'ini_get' => 1,  | 
            ||
| 53 | 'ini_set' => 1,  | 
            ||
| 54 | );  | 
            ||
| 55 | |||
| 56 | |||
| 57 | /* The testVersion configuration variable may be in any of the following formats:  | 
            ||
| 58 | * 1) Omitted/empty, in which case no version is specified. This effectively  | 
            ||
| 59 | * disables all the checks provided by this standard.  | 
            ||
| 60 | * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that  | 
            ||
| 61 | * the code will run on that version of PHP (no deprecated features or newer  | 
            ||
| 62 | * features being used).  | 
            ||
| 63 | * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run  | 
            ||
| 64 | * on all PHP versions in that range, and that it doesn't use any features that  | 
            ||
| 65 | * were deprecated by the final version in the list, or which were not available  | 
            ||
| 66 | * for the first version in the list.  | 
            ||
| 67 | * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2"  | 
            ||
| 68 | * would be treated as invalid, and ignored.  | 
            ||
| 69 | * This standard doesn't support checking against PHP4, so the minimum version that  | 
            ||
| 70 | * is recognised is "5.0".  | 
            ||
| 71 | */  | 
            ||
| 72 | |||
| 73 | private function getTestVersion()  | 
            ||
| 114 | |||
| 115 | View Code Duplication | public function supportsAbove($phpVersion)  | 
            |
| 128 | |||
| 129 | View Code Duplication | public function supportsBelow($phpVersion)  | 
            |
| 142 | |||
| 143 | |||
| 144 | /**  | 
            ||
| 145 | * Add a PHPCS message to the output stack as either a warning or an error.  | 
            ||
| 146 | *  | 
            ||
| 147 | * @param PHP_CodeSniffer_File $phpcsFile The file the message applies to.  | 
            ||
| 148 | * @param string $message The message.  | 
            ||
| 149 | * @param int $stackPtr The position of the token  | 
            ||
| 150 | * the message relates to.  | 
            ||
| 151 | * @param bool $isError Whether to report the message as an  | 
            ||
| 152 | * 'error' or 'warning'.  | 
            ||
| 153 | * Defaults to true (error).  | 
            ||
| 154 | * @param string $code The error code for the message.  | 
            ||
| 155 | * Defaults to 'Found'.  | 
            ||
| 156 | * @param array $data Optional input for the data replacements.  | 
            ||
| 157 | *  | 
            ||
| 158 | * @return void  | 
            ||
| 159 | */  | 
            ||
| 160 | public function addMessage($phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array())  | 
            ||
| 168 | |||
| 169 | |||
| 170 | /**  | 
            ||
| 171 | * Convert an arbitrary string to an alphanumeric string with underscores.  | 
            ||
| 172 | *  | 
            ||
| 173 | * Pre-empt issues with arbitrary strings being used as error codes in XML and PHP.  | 
            ||
| 174 | *  | 
            ||
| 175 | * @param string $baseString Arbitrary string.  | 
            ||
| 176 | *  | 
            ||
| 177 | * @return string  | 
            ||
| 178 | */  | 
            ||
| 179 | public function stringToErrorCode($baseString)  | 
            ||
| 183 | |||
| 184 | |||
| 185 | /**  | 
            ||
| 186 | * Strip quotes surrounding an arbitrary string.  | 
            ||
| 187 | *  | 
            ||
| 188 | * Intended for use with the content of a T_CONSTANT_ENCAPSED_STRING.  | 
            ||
| 189 | *  | 
            ||
| 190 | * @param string $string The raw string.  | 
            ||
| 191 | *  | 
            ||
| 192 | * @return string String without quotes around it.  | 
            ||
| 193 | */  | 
            ||
| 194 |     public function stripQuotes($string) { | 
            ||
| 197 | |||
| 198 | |||
| 199 | /**  | 
            ||
| 200 | * Make all top level array keys in an array lowercase.  | 
            ||
| 201 | *  | 
            ||
| 202 | * @param array $array Initial array.  | 
            ||
| 203 | *  | 
            ||
| 204 | * @return array Same array, but with all lowercase top level keys.  | 
            ||
| 205 | */  | 
            ||
| 206 | public function arrayKeysToLowercase($array)  | 
            ||
| 212 | |||
| 213 | |||
| 214 | /**  | 
            ||
| 215 | * Returns the name(s) of the interface(s) that the specified class implements.  | 
            ||
| 216 | *  | 
            ||
| 217 | * Returns FALSE on error or if there are no implemented interface names.  | 
            ||
| 218 | *  | 
            ||
| 219 |      * {@internal Duplicate of same method as introduced in PHPCS 2.7. | 
            ||
| 220 | * Once the minimum supported PHPCS version for this sniff library goes beyond  | 
            ||
| 221 | * that, this method can be removed and call to it replaced with  | 
            ||
| 222 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}}  | 
            ||
| 223 | *  | 
            ||
| 224 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 225 | * @param int $stackPtr The position of the class token.  | 
            ||
| 226 | *  | 
            ||
| 227 | * @return array|false  | 
            ||
| 228 | */  | 
            ||
| 229 | public function findImplementedInterfaceNames(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 276 | |||
| 277 | |||
| 278 | /**  | 
            ||
| 279 | * Checks if a function call has parameters.  | 
            ||
| 280 | *  | 
            ||
| 281 | * Expects to be passed the T_STRING stack pointer for the function call.  | 
            ||
| 282 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable.  | 
            ||
| 283 | *  | 
            ||
| 284 | * @link https://github.com/wimg/PHPCompatibility/issues/120  | 
            ||
| 285 | * @link https://github.com/wimg/PHPCompatibility/issues/152  | 
            ||
| 286 | *  | 
            ||
| 287 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 288 | * @param int $stackPtr The position of the function call token.  | 
            ||
| 289 | *  | 
            ||
| 290 | * @return bool  | 
            ||
| 291 | */  | 
            ||
| 292 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 325 | |||
| 326 | |||
| 327 | /**  | 
            ||
| 328 | * Count the number of parameters a function call has been passed.  | 
            ||
| 329 | *  | 
            ||
| 330 | * Expects to be passed the T_STRING stack pointer for the function call.  | 
            ||
| 331 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable.  | 
            ||
| 332 | *  | 
            ||
| 333 | * @link https://github.com/wimg/PHPCompatibility/issues/111  | 
            ||
| 334 | * @link https://github.com/wimg/PHPCompatibility/issues/114  | 
            ||
| 335 | * @link https://github.com/wimg/PHPCompatibility/issues/151  | 
            ||
| 336 | *  | 
            ||
| 337 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 338 | * @param int $stackPtr The position of the function call token.  | 
            ||
| 339 | *  | 
            ||
| 340 | * @return int  | 
            ||
| 341 | */  | 
            ||
| 342 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 350 | |||
| 351 | |||
| 352 | /**  | 
            ||
| 353 | * Get information on all parameters passed to a function call.  | 
            ||
| 354 | *  | 
            ||
| 355 | * Expects to be passed the T_STRING stack pointer for the function call.  | 
            ||
| 356 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable.  | 
            ||
| 357 | *  | 
            ||
| 358 | * Will return an multi-dimentional array with the start token pointer, end token  | 
            ||
| 359 | * pointer and raw parameter value for all parameters. Index will be 1-based.  | 
            ||
| 360 | * If no parameters are found, will return an empty array.  | 
            ||
| 361 | *  | 
            ||
| 362 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 363 | * @param int $stackPtr The position of the function call token.  | 
            ||
| 364 | *  | 
            ||
| 365 | * @return array  | 
            ||
| 366 | */  | 
            ||
| 367 | public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 439 | |||
| 440 | |||
| 441 | /**  | 
            ||
| 442 | * Get information on a specific parameter passed to a function call.  | 
            ||
| 443 | *  | 
            ||
| 444 | * Expects to be passed the T_STRING stack pointer for the function call.  | 
            ||
| 445 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable.  | 
            ||
| 446 | *  | 
            ||
| 447 | * Will return a array with the start token pointer, end token pointer and the raw value  | 
            ||
| 448 | * of the parameter at a specific offset.  | 
            ||
| 449 | * If the specified parameter is not found, will return false.  | 
            ||
| 450 | *  | 
            ||
| 451 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 452 | * @param int $stackPtr The position of the function call token.  | 
            ||
| 453 | * @param int $paramOffset The 1-based index position of the parameter to retrieve.  | 
            ||
| 454 | *  | 
            ||
| 455 | * @return array|false  | 
            ||
| 456 | */  | 
            ||
| 457 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset)  | 
            ||
| 468 | |||
| 469 | |||
| 470 | /**  | 
            ||
| 471 | * Verify whether a token is within a scoped condition.  | 
            ||
| 472 | *  | 
            ||
| 473 | * If the optional $validScopes parameter has been passed, the function  | 
            ||
| 474 | * will check that the token has at least one condition which is of a  | 
            ||
| 475 | * type defined in $validScopes.  | 
            ||
| 476 | *  | 
            ||
| 477 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 478 | * @param int $stackPtr The position of the token.  | 
            ||
| 479 | * @param array|int $validScopes Optional. Array of valid scopes  | 
            ||
| 480 | * or int value of a valid scope.  | 
            ||
| 481 | * Pass the T_.. constant(s) for the  | 
            ||
| 482 | * desired scope to this parameter.  | 
            ||
| 483 | *  | 
            ||
| 484 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise.  | 
            ||
| 485 | * If the $scopeTypes are set: True if *one* of the conditions is a  | 
            ||
| 486 | * valid scope, false otherwise.  | 
            ||
| 487 | */  | 
            ||
| 488 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null)  | 
            ||
| 526 | |||
| 527 | |||
| 528 | /**  | 
            ||
| 529 | * Verify whether a token is within a class scope.  | 
            ||
| 530 | *  | 
            ||
| 531 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 532 | * @param int $stackPtr The position of the token.  | 
            ||
| 533 | * @param bool $strict Whether to strictly check for the T_CLASS  | 
            ||
| 534 | * scope or also accept interfaces and traits  | 
            ||
| 535 | * as scope.  | 
            ||
| 536 | *  | 
            ||
| 537 | * @return bool True if within class scope, false otherwise.  | 
            ||
| 538 | */  | 
            ||
| 539 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true)  | 
            ||
| 549 | |||
| 550 | |||
| 551 | /**  | 
            ||
| 552 | * Verify whether a token is within a scoped use statement.  | 
            ||
| 553 | *  | 
            ||
| 554 | * PHPCS cross-version compatibility method.  | 
            ||
| 555 | *  | 
            ||
| 556 | * In PHPCS 1.x no conditions are set for a scoped use statement.  | 
            ||
| 557 | * This method works around that limitation.  | 
            ||
| 558 | *  | 
            ||
| 559 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 560 | * @param int $stackPtr The position of the token.  | 
            ||
| 561 | *  | 
            ||
| 562 | * @return bool True if within use scope, false otherwise.  | 
            ||
| 563 | */  | 
            ||
| 564 | public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 596 | |||
| 597 | |||
| 598 | /**  | 
            ||
| 599 | * Returns the fully qualified class name for a new class instantiation.  | 
            ||
| 600 | *  | 
            ||
| 601 | * Returns an empty string if the class name could not be reliably inferred.  | 
            ||
| 602 | *  | 
            ||
| 603 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 604 | * @param int $stackPtr The position of a T_NEW token.  | 
            ||
| 605 | *  | 
            ||
| 606 | * @return string  | 
            ||
| 607 | */  | 
            ||
| 608 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 640 | |||
| 641 | |||
| 642 | /**  | 
            ||
| 643 | * Returns the fully qualified name of the class that the specified class extends.  | 
            ||
| 644 | *  | 
            ||
| 645 | * Returns an empty string if the class does not extend another class or if  | 
            ||
| 646 | * 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_CLASS token.  | 
            ||
| 650 | *  | 
            ||
| 651 | * @return string  | 
            ||
| 652 | */  | 
            ||
| 653 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 673 | |||
| 674 | |||
| 675 | /**  | 
            ||
| 676 | * Returns the class name for the static usage of a class.  | 
            ||
| 677 | * This can be a call to a method, the use of a property or constant.  | 
            ||
| 678 | *  | 
            ||
| 679 | * Returns an empty string if the class name could not be reliably inferred.  | 
            ||
| 680 | *  | 
            ||
| 681 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 682 | * @param int $stackPtr The position of a T_NEW token.  | 
            ||
| 683 | *  | 
            ||
| 684 | * @return string  | 
            ||
| 685 | */  | 
            ||
| 686 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 732 | |||
| 733 | |||
| 734 | /**  | 
            ||
| 735 | * Get the Fully Qualified name for a class/function/constant etc.  | 
            ||
| 736 | *  | 
            ||
| 737 | * Checks if a class/function/constant name is already fully qualified and  | 
            ||
| 738 | * if not, enrich it with the relevant namespace information.  | 
            ||
| 739 | *  | 
            ||
| 740 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 741 | * @param int $stackPtr The position of the token.  | 
            ||
| 742 | * @param string $name The class / function / constant name.  | 
            ||
| 743 | *  | 
            ||
| 744 | * @return string  | 
            ||
| 745 | */  | 
            ||
| 746 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name)  | 
            ||
| 767 | |||
| 768 | |||
| 769 | /**  | 
            ||
| 770 | * Is the class/function/constant name namespaced or global ?  | 
            ||
| 771 | *  | 
            ||
| 772 | * @param string $FQName Fully Qualified name of a class, function etc.  | 
            ||
| 773 | * I.e. should always start with a `\` !  | 
            ||
| 774 | *  | 
            ||
| 775 | * @return bool True if namespaced, false if global.  | 
            ||
| 776 | */  | 
            ||
| 777 |     public function isNamespaced($FQName) { | 
            ||
| 784 | |||
| 785 | |||
| 786 | /**  | 
            ||
| 787 | * Determine the namespace name an arbitrary token lives in.  | 
            ||
| 788 | *  | 
            ||
| 789 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile.  | 
            ||
| 790 | * @param int $stackPtr The token position for which to determine the namespace.  | 
            ||
| 791 | *  | 
            ||
| 792 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies.  | 
            ||
| 793 | */  | 
            ||
| 794 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 844 | |||
| 845 | /**  | 
            ||
| 846 | * Get the complete namespace name for a namespace declaration.  | 
            ||
| 847 | *  | 
            ||
| 848 | * For hierarchical namespaces, the name will be composed of several tokens,  | 
            ||
| 849 | * i.e. MyProject\Sub\Level which will be returned together as one string.  | 
            ||
| 850 | *  | 
            ||
| 851 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile.  | 
            ||
| 852 | * @param int|bool $stackPtr The position of a T_NAMESPACE token.  | 
            ||
| 853 | *  | 
            ||
| 854 | * @return string|false Namespace name or false if not a namespace declaration.  | 
            ||
| 855 | * Namespace name can be an empty string for global namespace declaration.  | 
            ||
| 856 | */  | 
            ||
| 857 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr )  | 
            ||
| 897 | |||
| 898 | |||
| 899 | /**  | 
            ||
| 900 | * Returns the method parameters for the specified T_FUNCTION token.  | 
            ||
| 901 | *  | 
            ||
| 902 | * Each parameter is in the following format:  | 
            ||
| 903 | *  | 
            ||
| 904 | * <code>  | 
            ||
| 905 | * 0 => array(  | 
            ||
| 906 | * 'name' => '$var', // The variable name.  | 
            ||
| 907 | * 'pass_by_reference' => false, // Passed by reference.  | 
            ||
| 908 | * 'type_hint' => string, // Type hint for array or custom type  | 
            ||
| 909 | * 'nullable_type' => bool, // Whether the type given in the type hint is nullable  | 
            ||
| 910 | * 'type_hint' => string, // Type hint for array or custom type  | 
            ||
| 911 | * 'raw' => string, // Raw content of the tokens for the parameter  | 
            ||
| 912 | * )  | 
            ||
| 913 | * </code>  | 
            ||
| 914 | *  | 
            ||
| 915 | * Parameters with default values have an additional array index of  | 
            ||
| 916 | * 'default' with the value of the default as a string.  | 
            ||
| 917 | *  | 
            ||
| 918 |      * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` | 
            ||
| 919 | * class, but with some improvements which will probably be introduced in  | 
            ||
| 920 |      * PHPCS 2.7.1/2.8. {@see https://github.com/squizlabs/PHP_CodeSniffer/pull/1117} | 
            ||
| 921 |      * and {@see https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} | 
            ||
| 922 | *  | 
            ||
| 923 | * Once the minimum supported PHPCS version for this sniff library goes beyond  | 
            ||
| 924 | * that, this method can be removed and calls to it replaced with  | 
            ||
| 925 | * `$phpcsFile->getMethodParameters($stackPtr)` calls.  | 
            ||
| 926 | *  | 
            ||
| 927 | * Last synced with PHPCS version: PHPCS 2.7.}}  | 
            ||
| 928 | *  | 
            ||
| 929 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile.  | 
            ||
| 930 | * @param int $stackPtr The position in the stack of the  | 
            ||
| 931 | * T_FUNCTION token to acquire the  | 
            ||
| 932 | * parameters for.  | 
            ||
| 933 | *  | 
            ||
| 934 | * @return array|false  | 
            ||
| 935 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of  | 
            ||
| 936 | * type T_FUNCTION.  | 
            ||
| 937 | */  | 
            ||
| 938 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 1092 | |||
| 1093 | |||
| 1094 | /**  | 
            ||
| 1095 | * Get the hash algorithm name from the parameter in a hash function call.  | 
            ||
| 1096 | *  | 
            ||
| 1097 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile.  | 
            ||
| 1098 | * @param int $stackPtr The position of the T_STRING function token.  | 
            ||
| 1099 | *  | 
            ||
| 1100 | * @return string|false The algorithm name without quotes if this was a relevant hash  | 
            ||
| 1101 | * function call or false if it was not.  | 
            ||
| 1102 | */  | 
            ||
| 1103 | public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 1138 | |||
| 1139 | }//end class  | 
            ||
| 1140 | 
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.