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 | * 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 1-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) |
||
| 332 | |||
| 333 | |||
| 334 | /** |
||
| 335 | * Get information on a specific parameter passed to a function call. |
||
| 336 | * |
||
| 337 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 338 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 339 | * |
||
| 340 | * Will return a array with the start token pointer, end token pointer and the raw value |
||
| 341 | * of the parameter at a specific offset. |
||
| 342 | * If the specified parameter is not found, will return false. |
||
| 343 | * |
||
| 344 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 345 | * @param int $stackPtr The position of the function call token. |
||
| 346 | * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
||
| 347 | * |
||
| 348 | * @return array|false |
||
| 349 | */ |
||
| 350 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * Verify whether a token is within a scoped condition. |
||
| 365 | * |
||
| 366 | * If the optional $validScopes parameter has been passed, the function |
||
| 367 | * will check that the token has at least one condition which is of a |
||
| 368 | * type defined in $validScopes. |
||
| 369 | * |
||
| 370 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 371 | * @param int $stackPtr The position of the token. |
||
| 372 | * @param array|int $validScopes Optional. Array of valid scopes |
||
| 373 | * or int value of a valid scope. |
||
| 374 | * |
||
| 375 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
| 376 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
| 377 | * valid scope, false otherwise. |
||
| 378 | */ |
||
| 379 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * Verify whether a token is within a class scope. |
||
| 421 | * |
||
| 422 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 423 | * @param int $stackPtr The position of the token. |
||
| 424 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
| 425 | * scope or also accept interfaces and traits |
||
| 426 | * as scope. |
||
| 427 | * |
||
| 428 | * @return bool True if within class scope, false otherwise. |
||
| 429 | */ |
||
| 430 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * Returns the fully qualified class name for a new class instantiation. |
||
| 444 | * |
||
| 445 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 446 | * |
||
| 447 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 448 | * @param int $stackPtr The position of a T_NEW token. |
||
| 449 | * |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 484 | |||
| 485 | |||
| 486 | /** |
||
| 487 | * Returns the fully qualified name of the class that the specified class extends. |
||
| 488 | * |
||
| 489 | * Returns an empty string if the class does not extend another class or if |
||
| 490 | * the class name could not be reliably inferred. |
||
| 491 | * |
||
| 492 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 493 | * @param int $stackPtr The position of a T_CLASS token. |
||
| 494 | * |
||
| 495 | * @return string |
||
| 496 | */ |
||
| 497 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 517 | |||
| 518 | |||
| 519 | /** |
||
| 520 | * Returns the class name for the static usage of a class. |
||
| 521 | * This can be a call to a method, the use of a property or constant. |
||
| 522 | * |
||
| 523 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 524 | * |
||
| 525 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 526 | * @param int $stackPtr The position of a T_NEW token. |
||
| 527 | * |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 576 | |||
| 577 | |||
| 578 | /** |
||
| 579 | * Get the Fully Qualified name for a class/function/constant etc. |
||
| 580 | * |
||
| 581 | * Checks if a class/function/constant name is already fully qualified and |
||
| 582 | * if not, enrich it with the relevant namespace information. |
||
| 583 | * |
||
| 584 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 585 | * @param int $stackPtr The position of the token. |
||
| 586 | * @param string $name The class / function / constant name. |
||
| 587 | * |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
| 611 | |||
| 612 | |||
| 613 | /** |
||
| 614 | * Is the class/function/constant name namespaced or global ? |
||
| 615 | * |
||
| 616 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
| 617 | * I.e. should always start with a `\` ! |
||
| 618 | * |
||
| 619 | * @return bool True if namespaced, false if global. |
||
| 620 | */ |
||
| 621 | public function isNamespaced($FQName) { |
||
| 628 | |||
| 629 | |||
| 630 | /** |
||
| 631 | * Determine the namespace name an arbitrary token lives in. |
||
| 632 | * |
||
| 633 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 634 | * @param int $stackPtr The token position for which to determine the namespace. |
||
| 635 | * |
||
| 636 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
| 637 | */ |
||
| 638 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 639 | { |
||
| 640 | $tokens = $phpcsFile->getTokens(); |
||
| 641 | |||
| 642 | // Check for the existence of the token. |
||
| 643 | if (isset($tokens[$stackPtr]) === false) { |
||
| 644 | return ''; |
||
| 645 | } |
||
| 646 | |||
| 647 | // Check for scoped namespace {}. |
||
| 648 | if (empty($tokens[$stackPtr]['conditions']) === false) { |
||
| 649 | foreach ($tokens[$stackPtr]['conditions'] as $pointer => $type) { |
||
| 650 | if ($type === T_NAMESPACE) { |
||
| 651 | $namespace = $this->getDeclaredNamespaceName($phpcsFile, $pointer); |
||
| 652 | if ($namespace !== false) { |
||
| 653 | return $namespace; |
||
| 654 | } |
||
| 655 | break; // Nested namespaces is not possible. |
||
| 656 | } |
||
| 657 | } |
||
| 658 | } |
||
| 659 | |||
| 660 | /* |
||
| 661 | * Not in a scoped namespace, so let's see if we can find a non-scoped namespace instead. |
||
| 662 | * Keeping in mind that: |
||
| 663 | * - there can be multiple non-scoped namespaces in a file (bad practice, but it happens). |
||
| 664 | * - the namespace keyword can also be used as part of a function/method call and such. |
||
| 665 | * - that a non-named namespace resolves to the global namespace. |
||
| 666 | */ |
||
| 667 | $previousNSToken = $stackPtr; |
||
| 668 | $namespace = false; |
||
| 669 | do { |
||
| 670 | $previousNSToken = $phpcsFile->findPrevious(T_NAMESPACE, $previousNSToken -1); |
||
| 671 | |||
| 672 | // Stop if we encounter a scoped namespace declaration as we already know we're not in one. |
||
| 673 | if (empty($tokens[$previousNSToken]['scope_condition']) === false && $tokens[$previousNSToken]['scope_condition'] = $previousNSToken) { |
||
| 674 | break; |
||
| 675 | } |
||
| 676 | $namespace = $this->getDeclaredNamespaceName($phpcsFile, $previousNSToken); |
||
| 677 | |||
| 678 | } while ($namespace === false && $previousNSToken !== false); |
||
| 679 | |||
| 680 | // If we still haven't got a namespace, return an empty string. |
||
| 681 | if ($namespace === false) { |
||
| 682 | return ''; |
||
| 683 | } |
||
| 684 | else { |
||
| 685 | return $namespace; |
||
| 686 | } |
||
| 687 | } |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Get the complete namespace name for a namespace declaration. |
||
| 691 | * |
||
| 692 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
| 693 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
| 694 | * |
||
| 695 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 696 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
| 697 | * |
||
| 698 | * @return string|false Namespace name or false if not a namespace declaration. |
||
| 699 | * Namespace name can be an empty string for global namespace declaration. |
||
| 700 | */ |
||
| 701 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
||
| 741 | |||
| 742 | |||
| 743 | /** |
||
| 744 | * Returns the method parameters for the specified T_FUNCTION token. |
||
| 745 | * |
||
| 746 | * Each parameter is in the following format: |
||
| 747 | * |
||
| 748 | * <code> |
||
| 749 | * 0 => array( |
||
| 750 | * 'name' => '$var', // The variable name. |
||
| 751 | * 'pass_by_reference' => false, // Passed by reference. |
||
| 752 | * 'type_hint' => string, // Type hint for array or custom type |
||
| 753 | * ) |
||
| 754 | * </code> |
||
| 755 | * |
||
| 756 | * Parameters with default values have an additional array index of |
||
| 757 | * 'default' with the value of the default as a string. |
||
| 758 | * |
||
| 759 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
| 760 | * class, but with some improvements which were only introduced in PHPCS 2.7. |
||
| 761 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
| 762 | * that, this method can be removed and calls to it replaced with |
||
| 763 | * `$phpcsFile->getMethodParameters($stackPtr)` calls.}} |
||
| 764 | * |
||
| 765 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 766 | * @param int $stackPtr The position in the stack of the T_FUNCTION token |
||
| 767 | * to acquire the parameters for. |
||
| 768 | * |
||
| 769 | * @return array |
||
| 770 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
| 771 | * type T_FUNCTION. |
||
| 772 | */ |
||
| 773 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 918 | |||
| 919 | }//end class |
||
| 920 |
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.