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 | /* The testVersion configuration variable may be in any of the following formats:  | 
            ||
| 26 | * 1) Omitted/empty, in which case no version is specified. This effectively  | 
            ||
| 27 | * disables all the checks provided by this standard.  | 
            ||
| 28 | * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that  | 
            ||
| 29 | * the code will run on that version of PHP (no deprecated features or newer  | 
            ||
| 30 | * features being used).  | 
            ||
| 31 | * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run  | 
            ||
| 32 | * on all PHP versions in that range, and that it doesn't use any features that  | 
            ||
| 33 | * were deprecated by the final version in the list, or which were not available  | 
            ||
| 34 | * for the first version in the list.  | 
            ||
| 35 | * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2"  | 
            ||
| 36 | * would be treated as invalid, and ignored.  | 
            ||
| 37 | * This standard doesn't support checking against PHP4, so the minimum version that  | 
            ||
| 38 | * is recognised is "5.0".  | 
            ||
| 39 | */  | 
            ||
| 40 | |||
| 41 | private function getTestVersion()  | 
            ||
| 82 | |||
| 83 | View Code Duplication | public function supportsAbove($phpVersion)  | 
            |
| 84 |     { | 
            ||
| 85 | $testVersion = $this->getTestVersion();  | 
            ||
| 86 | $testVersion = $testVersion[1];  | 
            ||
| 87 | |||
| 88 | if (is_null($testVersion)  | 
            ||
| 89 | || version_compare($testVersion, $phpVersion) >= 0  | 
            ||
| 90 |         ) { | 
            ||
| 91 | return true;  | 
            ||
| 92 |         } else { | 
            ||
| 93 | return false;  | 
            ||
| 94 | }  | 
            ||
| 95 | }//end supportsAbove()  | 
            ||
| 96 | |||
| 97 | View Code Duplication | public function supportsBelow($phpVersion)  | 
            |
| 98 |     { | 
            ||
| 99 | $testVersion = $this->getTestVersion();  | 
            ||
| 100 | $testVersion = $testVersion[0];  | 
            ||
| 101 | |||
| 102 | if (!is_null($testVersion)  | 
            ||
| 103 | && version_compare($testVersion, $phpVersion) <= 0  | 
            ||
| 104 |         ) { | 
            ||
| 105 | return true;  | 
            ||
| 106 |         } else { | 
            ||
| 107 | return false;  | 
            ||
| 108 | }  | 
            ||
| 109 | }//end supportsBelow()  | 
            ||
| 110 | |||
| 111 | /**  | 
            ||
| 112 | * Returns the name(s) of the interface(s) that the specified class implements.  | 
            ||
| 113 | *  | 
            ||
| 114 | * Returns FALSE on error or if there are no implemented interface names.  | 
            ||
| 115 | *  | 
            ||
| 116 |      * {@internal Duplicate of same method as introduced in PHPCS 2.7. | 
            ||
| 117 | * Once the minimum supported PHPCS version for this sniff library goes beyond  | 
            ||
| 118 | * that, this method can be removed and call to it replaced with  | 
            ||
| 119 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}}  | 
            ||
| 120 | *  | 
            ||
| 121 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 122 | * @param int $stackPtr The position of the class token.  | 
            ||
| 123 | *  | 
            ||
| 124 | * @return array|false  | 
            ||
| 125 | */  | 
            ||
| 126 | public function findImplementedInterfaceNames($phpcsFile, $stackPtr)  | 
            ||
| 169 | |||
| 170 | |||
| 171 | /**  | 
            ||
| 172 | * Checks if a function call has parameters.  | 
            ||
| 173 | *  | 
            ||
| 174 | * Expects to be passed the T_STRING stack pointer for the function call.  | 
            ||
| 175 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable.  | 
            ||
| 176 | *  | 
            ||
| 177 | * @link https://github.com/wimg/PHPCompatibility/issues/120  | 
            ||
| 178 | * @link https://github.com/wimg/PHPCompatibility/issues/152  | 
            ||
| 179 | *  | 
            ||
| 180 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 181 | * @param int $stackPtr The position of the function call token.  | 
            ||
| 182 | *  | 
            ||
| 183 | * @return bool  | 
            ||
| 184 | */  | 
            ||
| 185 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 218 | |||
| 219 | |||
| 220 | /**  | 
            ||
| 221 | * Count the number of parameters a function call has been passed.  | 
            ||
| 222 | *  | 
            ||
| 223 | * Expects to be passed the T_STRING stack pointer for the function call.  | 
            ||
| 224 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable.  | 
            ||
| 225 | *  | 
            ||
| 226 | * @link https://github.com/wimg/PHPCompatibility/issues/111  | 
            ||
| 227 | * @link https://github.com/wimg/PHPCompatibility/issues/114  | 
            ||
| 228 | * @link https://github.com/wimg/PHPCompatibility/issues/151  | 
            ||
| 229 | *  | 
            ||
| 230 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 231 | * @param int $stackPtr The position of the function call token.  | 
            ||
| 232 | *  | 
            ||
| 233 | * @return int  | 
            ||
| 234 | */  | 
            ||
| 235 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 243 | |||
| 244 | |||
| 245 | /**  | 
            ||
| 246 | * Get information on all parameters passed to a function call.  | 
            ||
| 247 | *  | 
            ||
| 248 | * Expects to be passed the T_STRING stack pointer for the function call.  | 
            ||
| 249 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable.  | 
            ||
| 250 | *  | 
            ||
| 251 | * Will return an multi-dimentional array with the start token pointer, end token  | 
            ||
| 252 | * pointer and raw parameter value for all parameters. Index will be 0-based.  | 
            ||
| 253 | * If no parameters are found, will return an empty array.  | 
            ||
| 254 | *  | 
            ||
| 255 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 256 | * @param int $stackPtr The position of the function call token.  | 
            ||
| 257 | *  | 
            ||
| 258 | * @return array  | 
            ||
| 259 | */  | 
            ||
| 260 | public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 311 | |||
| 312 | |||
| 313 | /**  | 
            ||
| 314 | * Get information on a specific parameter passed to a function call.  | 
            ||
| 315 | *  | 
            ||
| 316 | * Expects to be passed the T_STRING stack pointer for the function call.  | 
            ||
| 317 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable.  | 
            ||
| 318 | *  | 
            ||
| 319 | * Will return a array with the start token pointer, end token pointer and the raw value  | 
            ||
| 320 | * of the parameter at a specific position.  | 
            ||
| 321 | * If the specified parameter is not found, will return false.  | 
            ||
| 322 | *  | 
            ||
| 323 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 324 | * @param int $stackPtr The position of the function call token.  | 
            ||
| 325 | * @param int $paramPosition The 0-based index position of the parameter to retrieve.  | 
            ||
| 326 | *  | 
            ||
| 327 | * @return array|false  | 
            ||
| 328 | */  | 
            ||
| 329 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramPosition)  | 
            ||
| 340 | |||
| 341 | |||
| 342 | /**  | 
            ||
| 343 | * Returns the fully qualified class name for a new class instantiation.  | 
            ||
| 344 | *  | 
            ||
| 345 | * Returns an empty string if the class name could not be reliably inferred.  | 
            ||
| 346 | *  | 
            ||
| 347 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 348 | * @param int $stackPtr The position of a T_NEW token.  | 
            ||
| 349 | *  | 
            ||
| 350 | * @return string  | 
            ||
| 351 | */  | 
            ||
| 352 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 379 | |||
| 380 | |||
| 381 | /**  | 
            ||
| 382 | * Returns the fully qualified name of the class that the specified class extends.  | 
            ||
| 383 | *  | 
            ||
| 384 | * Returns an empty string if the class does not extend another class or if  | 
            ||
| 385 | * the class name could not be reliably inferred.  | 
            ||
| 386 | *  | 
            ||
| 387 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 388 | * @param int $stackPtr The position of a T_CLASS token.  | 
            ||
| 389 | *  | 
            ||
| 390 | * @return string  | 
            ||
| 391 | */  | 
            ||
| 392 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 412 | |||
| 413 | |||
| 414 | /**  | 
            ||
| 415 | * Returns the class name for the static usage of a class.  | 
            ||
| 416 | * This can be a call to a method, the use of a property or constant.  | 
            ||
| 417 | *  | 
            ||
| 418 | * Returns an empty string if the class name could not be reliably inferred.  | 
            ||
| 419 | *  | 
            ||
| 420 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 421 | * @param int $stackPtr The position of a T_NEW token.  | 
            ||
| 422 | *  | 
            ||
| 423 | * @return string  | 
            ||
| 424 | */  | 
            ||
| 425 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 466 | |||
| 467 | |||
| 468 | /**  | 
            ||
| 469 | * Get the Fully Qualified name for a class/function/constant etc.  | 
            ||
| 470 | *  | 
            ||
| 471 | * Checks if a class/function/constant name is already fully qualified and  | 
            ||
| 472 | * if not, enrich it with the relevant namespace information.  | 
            ||
| 473 | *  | 
            ||
| 474 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.  | 
            ||
| 475 | * @param int $stackPtr The position of the token.  | 
            ||
| 476 | * @param string $name The class / function / constant name.  | 
            ||
| 477 | *  | 
            ||
| 478 | * @return string  | 
            ||
| 479 | */  | 
            ||
| 480 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name)  | 
            ||
| 501 | |||
| 502 | |||
| 503 | /**  | 
            ||
| 504 | * Is the class/function/constant name namespaced or global ?  | 
            ||
| 505 | *  | 
            ||
| 506 | * @param string $FQName Fully Qualified name of a class, function etc.  | 
            ||
| 507 | * I.e. should always start with a `\` !  | 
            ||
| 508 | *  | 
            ||
| 509 | * @return bool True if namespaced, false if global.  | 
            ||
| 510 | */  | 
            ||
| 511 |     public function isNamespaced($FQName) { | 
            ||
| 518 | |||
| 519 | |||
| 520 | /**  | 
            ||
| 521 | * Determine the namespace name an arbitrary token lives in.  | 
            ||
| 522 | *  | 
            ||
| 523 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile.  | 
            ||
| 524 | * @param int $stackPtr The token position for which to determine the namespace.  | 
            ||
| 525 | *  | 
            ||
| 526 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies.  | 
            ||
| 527 | */  | 
            ||
| 528 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr)  | 
            ||
| 578 | |||
| 579 | /**  | 
            ||
| 580 | * Get the complete namespace name for a namespace declaration.  | 
            ||
| 581 | *  | 
            ||
| 582 | * For hierarchical namespaces, the name will be composed of several tokens,  | 
            ||
| 583 | * i.e. MyProject\Sub\Level which will be returned together as one string.  | 
            ||
| 584 | *  | 
            ||
| 585 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile.  | 
            ||
| 586 | * @param int|bool $stackPtr The position of a T_NAMESPACE token.  | 
            ||
| 587 | *  | 
            ||
| 588 | * @return string|false Namespace name or false if not a namespace declaration.  | 
            ||
| 589 | * Namespace name can be an empty string for global namespace declaration.  | 
            ||
| 590 | */  | 
            ||
| 591 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr )  | 
            ||
| 631 | |||
| 632 | }//end class  | 
            ||
| 633 | 
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.