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() |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * var $arrTestVersions will hold an array containing min/max version of PHP |
||
| 45 | * that we are checking against (see above). If only a single version |
||
| 46 | * number is specified, then this is used as both the min and max. |
||
| 47 | */ |
||
| 48 | static $arrTestVersions = array(); |
||
| 49 | |||
| 50 | $testVersion = trim(PHP_CodeSniffer::getConfigData('testVersion')); |
||
| 51 | |||
| 52 | if (!isset($arrTestVersions[$testVersion]) && !empty($testVersion)) { |
||
| 53 | |||
| 54 | $arrTestVersions[$testVersion] = array(null, null); |
||
| 55 | if (preg_match('/^\d+\.\d+$/', $testVersion)) { |
||
| 56 | $arrTestVersions[$testVersion] = array($testVersion, $testVersion); |
||
| 57 | } |
||
| 58 | elseif (preg_match('/^(\d+\.\d+)\s*-\s*(\d+\.\d+)$/', $testVersion, |
||
| 59 | $matches)) |
||
| 60 | { |
||
| 61 | if (version_compare($matches[1], $matches[2], '>')) { |
||
| 62 | trigger_error("Invalid range in testVersion setting: '" |
||
| 63 | . $testVersion . "'", E_USER_WARNING); |
||
| 64 | } |
||
| 65 | else { |
||
| 66 | $arrTestVersions[$testVersion] = array($matches[1], $matches[2]); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | elseif (!$testVersion == '') { |
||
| 70 | trigger_error("Invalid testVersion setting: '" . $testVersion |
||
| 71 | . "'", E_USER_WARNING); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | if (isset($arrTestVersions[$testVersion])) { |
||
| 76 | return $arrTestVersions[$testVersion]; |
||
| 77 | } |
||
| 78 | else { |
||
| 79 | return array(null, null); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | View Code Duplication | public function supportsAbove($phpVersion) |
|
| 96 | |||
| 97 | View Code Duplication | public function supportsBelow($phpVersion) |
|
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Strip quotes surrounding an arbitrary string. |
||
| 114 | * |
||
| 115 | * Intended for use with the content of a T_CONSTANT_ENCAPSED_STRING. |
||
| 116 | * |
||
| 117 | * @param string $string The raw string. |
||
| 118 | * |
||
| 119 | * @return string String without quotes around it. |
||
| 120 | */ |
||
| 121 | public function stripQuotes($string) { |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
| 128 | * |
||
| 129 | * Returns FALSE on error or if there are no implemented interface names. |
||
| 130 | * |
||
| 131 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
| 132 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
| 133 | * that, this method can be removed and call to it replaced with |
||
| 134 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
| 135 | * |
||
| 136 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 137 | * @param int $stackPtr The position of the class token. |
||
| 138 | * |
||
| 139 | * @return array|false |
||
| 140 | */ |
||
| 141 | public function findImplementedInterfaceNames($phpcsFile, $stackPtr) |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * Checks if a function call has parameters. |
||
| 188 | * |
||
| 189 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 190 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 191 | * |
||
| 192 | * @link https://github.com/wimg/PHPCompatibility/issues/120 |
||
| 193 | * @link https://github.com/wimg/PHPCompatibility/issues/152 |
||
| 194 | * |
||
| 195 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 196 | * @param int $stackPtr The position of the function call token. |
||
| 197 | * |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * Count the number of parameters a function call has been passed. |
||
| 237 | * |
||
| 238 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 239 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 240 | * |
||
| 241 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
| 242 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
| 243 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
| 244 | * |
||
| 245 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 246 | * @param int $stackPtr The position of the function call token. |
||
| 247 | * |
||
| 248 | * @return int |
||
| 249 | */ |
||
| 250 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * Get information on all parameters passed to a function call. |
||
| 262 | * |
||
| 263 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 264 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 265 | * |
||
| 266 | * Will return an multi-dimentional array with the start token pointer, end token |
||
| 267 | * pointer and raw parameter value for all parameters. Index will be 1-based. |
||
| 268 | * If no parameters are found, will return an empty array. |
||
| 269 | * |
||
| 270 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 271 | * @param int $stackPtr The position of the function call token. |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 347 | |||
| 348 | |||
| 349 | /** |
||
| 350 | * Get information on a specific parameter passed to a function call. |
||
| 351 | * |
||
| 352 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 353 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 354 | * |
||
| 355 | * Will return a array with the start token pointer, end token pointer and the raw value |
||
| 356 | * of the parameter at a specific offset. |
||
| 357 | * If the specified parameter is not found, will return false. |
||
| 358 | * |
||
| 359 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 360 | * @param int $stackPtr The position of the function call token. |
||
| 361 | * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
||
| 362 | * |
||
| 363 | * @return array|false |
||
| 364 | */ |
||
| 365 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * Verify whether a token is within a scoped condition. |
||
| 380 | * |
||
| 381 | * If the optional $validScopes parameter has been passed, the function |
||
| 382 | * will check that the token has at least one condition which is of a |
||
| 383 | * type defined in $validScopes. |
||
| 384 | * |
||
| 385 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 386 | * @param int $stackPtr The position of the token. |
||
| 387 | * @param array|int $validScopes Optional. Array of valid scopes |
||
| 388 | * or int value of a valid scope. |
||
| 389 | * |
||
| 390 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
| 391 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
| 392 | * valid scope, false otherwise. |
||
| 393 | */ |
||
| 394 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * Verify whether a token is within a class scope. |
||
| 436 | * |
||
| 437 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 438 | * @param int $stackPtr The position of the token. |
||
| 439 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
| 440 | * scope or also accept interfaces and traits |
||
| 441 | * as scope. |
||
| 442 | * |
||
| 443 | * @return bool True if within class scope, false otherwise. |
||
| 444 | */ |
||
| 445 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
| 455 | |||
| 456 | |||
| 457 | /** |
||
| 458 | * Returns the fully qualified class name for a new class instantiation. |
||
| 459 | * |
||
| 460 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 461 | * |
||
| 462 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 463 | * @param int $stackPtr The position of a T_NEW token. |
||
| 464 | * |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * Returns the fully qualified name of the class that the specified class extends. |
||
| 503 | * |
||
| 504 | * Returns an empty string if the class does not extend another class or if |
||
| 505 | * the class name could not be reliably inferred. |
||
| 506 | * |
||
| 507 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 508 | * @param int $stackPtr The position of a T_CLASS token. |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 532 | |||
| 533 | |||
| 534 | /** |
||
| 535 | * Returns the class name for the static usage of a class. |
||
| 536 | * This can be a call to a method, the use of a property or constant. |
||
| 537 | * |
||
| 538 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 539 | * |
||
| 540 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 541 | * @param int $stackPtr The position of a T_NEW token. |
||
| 542 | * |
||
| 543 | * @return string |
||
| 544 | */ |
||
| 545 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 591 | |||
| 592 | |||
| 593 | /** |
||
| 594 | * Get the Fully Qualified name for a class/function/constant etc. |
||
| 595 | * |
||
| 596 | * Checks if a class/function/constant name is already fully qualified and |
||
| 597 | * if not, enrich it with the relevant namespace information. |
||
| 598 | * |
||
| 599 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 600 | * @param int $stackPtr The position of the token. |
||
| 601 | * @param string $name The class / function / constant name. |
||
| 602 | * |
||
| 603 | * @return string |
||
| 604 | */ |
||
| 605 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
| 626 | |||
| 627 | |||
| 628 | /** |
||
| 629 | * Is the class/function/constant name namespaced or global ? |
||
| 630 | * |
||
| 631 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
| 632 | * I.e. should always start with a `\` ! |
||
| 633 | * |
||
| 634 | * @return bool True if namespaced, false if global. |
||
| 635 | */ |
||
| 636 | public function isNamespaced($FQName) { |
||
| 643 | |||
| 644 | |||
| 645 | /** |
||
| 646 | * Determine the namespace name an arbitrary token lives in. |
||
| 647 | * |
||
| 648 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 649 | * @param int $stackPtr The token position for which to determine the namespace. |
||
| 650 | * |
||
| 651 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
| 652 | */ |
||
| 653 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Get the complete namespace name for a namespace declaration. |
||
| 706 | * |
||
| 707 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
| 708 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
| 709 | * |
||
| 710 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 711 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
| 712 | * |
||
| 713 | * @return string|false Namespace name or false if not a namespace declaration. |
||
| 714 | * Namespace name can be an empty string for global namespace declaration. |
||
| 715 | */ |
||
| 716 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
||
| 756 | |||
| 757 | |||
| 758 | /** |
||
| 759 | * Returns the method parameters for the specified T_FUNCTION token. |
||
| 760 | * |
||
| 761 | * Each parameter is in the following format: |
||
| 762 | * |
||
| 763 | * <code> |
||
| 764 | * 0 => array( |
||
| 765 | * 'name' => '$var', // The variable name. |
||
| 766 | * 'pass_by_reference' => false, // Passed by reference. |
||
| 767 | * 'type_hint' => string, // Type hint for array or custom type |
||
| 768 | * ) |
||
| 769 | * </code> |
||
| 770 | * |
||
| 771 | * Parameters with default values have an additional array index of |
||
| 772 | * 'default' with the value of the default as a string. |
||
| 773 | * |
||
| 774 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
| 775 | * class, but with some improvements which were only introduced in PHPCS 2.7. |
||
| 776 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
| 777 | * that, this method can be removed and calls to it replaced with |
||
| 778 | * `$phpcsFile->getMethodParameters($stackPtr)` calls.}} |
||
| 779 | * |
||
| 780 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 781 | * @param int $stackPtr The position in the stack of the T_FUNCTION token |
||
| 782 | * to acquire the parameters for. |
||
| 783 | * |
||
| 784 | * @return array |
||
| 785 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
| 786 | * type T_FUNCTION. |
||
| 787 | */ |
||
| 788 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 933 | |||
| 934 | }//end class |
||
| 935 |
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.