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 | /* The testVersion configuration variable may be in any of the following formats: |
||
| 44 | * 1) Omitted/empty, in which case no version is specified. This effectively |
||
| 45 | * disables all the checks provided by this standard. |
||
| 46 | * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
||
| 47 | * the code will run on that version of PHP (no deprecated features or newer |
||
| 48 | * features being used). |
||
| 49 | * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
||
| 50 | * on all PHP versions in that range, and that it doesn't use any features that |
||
| 51 | * were deprecated by the final version in the list, or which were not available |
||
| 52 | * for the first version in the list. |
||
| 53 | * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
||
| 54 | * would be treated as invalid, and ignored. |
||
| 55 | * This standard doesn't support checking against PHP4, so the minimum version that |
||
| 56 | * is recognised is "5.0". |
||
| 57 | */ |
||
| 58 | |||
| 59 | private function getTestVersion() |
||
| 100 | |||
| 101 | View Code Duplication | public function supportsAbove($phpVersion) |
|
| 114 | |||
| 115 | View Code Duplication | public function supportsBelow($phpVersion) |
|
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Strip quotes surrounding an arbitrary string. |
||
| 132 | * |
||
| 133 | * Intended for use with the content of a T_CONSTANT_ENCAPSED_STRING. |
||
| 134 | * |
||
| 135 | * @param string $string The raw string. |
||
| 136 | * |
||
| 137 | * @return string String without quotes around it. |
||
| 138 | */ |
||
| 139 | public function stripQuotes($string) { |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
| 146 | * |
||
| 147 | * Returns FALSE on error or if there are no implemented interface names. |
||
| 148 | * |
||
| 149 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
| 150 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
| 151 | * that, this method can be removed and call to it replaced with |
||
| 152 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
| 153 | * |
||
| 154 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 155 | * @param int $stackPtr The position of the class token. |
||
| 156 | * |
||
| 157 | * @return array|false |
||
| 158 | */ |
||
| 159 | public function findImplementedInterfaceNames(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * Checks if a function call has parameters. |
||
| 210 | * |
||
| 211 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 212 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 213 | * |
||
| 214 | * @link https://github.com/wimg/PHPCompatibility/issues/120 |
||
| 215 | * @link https://github.com/wimg/PHPCompatibility/issues/152 |
||
| 216 | * |
||
| 217 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 218 | * @param int $stackPtr The position of the function call token. |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * Count the number of parameters a function call has been passed. |
||
| 259 | * |
||
| 260 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 261 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 262 | * |
||
| 263 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
| 264 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
| 265 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
| 266 | * |
||
| 267 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 268 | * @param int $stackPtr The position of the function call token. |
||
| 269 | * |
||
| 270 | * @return int |
||
| 271 | */ |
||
| 272 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Get information on all parameters passed to a function call. |
||
| 284 | * |
||
| 285 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 286 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 287 | * |
||
| 288 | * Will return an multi-dimentional array with the start token pointer, end token |
||
| 289 | * pointer and raw parameter value for all parameters. Index will be 1-based. |
||
| 290 | * If no parameters are found, will return an empty array. |
||
| 291 | * |
||
| 292 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 293 | * @param int $stackPtr The position of the function call token. |
||
| 294 | * |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * Get information on a specific parameter passed to a function call. |
||
| 373 | * |
||
| 374 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 375 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 376 | * |
||
| 377 | * Will return a array with the start token pointer, end token pointer and the raw value |
||
| 378 | * of the parameter at a specific offset. |
||
| 379 | * If the specified parameter is not found, will return false. |
||
| 380 | * |
||
| 381 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 382 | * @param int $stackPtr The position of the function call token. |
||
| 383 | * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
||
| 384 | * |
||
| 385 | * @return array|false |
||
| 386 | */ |
||
| 387 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
||
| 398 | |||
| 399 | |||
| 400 | /** |
||
| 401 | * Verify whether a token is within a scoped condition. |
||
| 402 | * |
||
| 403 | * If the optional $validScopes parameter has been passed, the function |
||
| 404 | * will check that the token has at least one condition which is of a |
||
| 405 | * type defined in $validScopes. |
||
| 406 | * |
||
| 407 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 408 | * @param int $stackPtr The position of the token. |
||
| 409 | * @param array|int $validScopes Optional. Array of valid scopes |
||
| 410 | * or int value of a valid scope. |
||
| 411 | * |
||
| 412 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
| 413 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
| 414 | * valid scope, false otherwise. |
||
| 415 | */ |
||
| 416 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * Verify whether a token is within a class scope. |
||
| 458 | * |
||
| 459 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 460 | * @param int $stackPtr The position of the token. |
||
| 461 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
| 462 | * scope or also accept interfaces and traits |
||
| 463 | * as scope. |
||
| 464 | * |
||
| 465 | * @return bool True if within class scope, false otherwise. |
||
| 466 | */ |
||
| 467 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
| 477 | |||
| 478 | |||
| 479 | /** |
||
| 480 | * Returns the fully qualified class name for a new class instantiation. |
||
| 481 | * |
||
| 482 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 483 | * |
||
| 484 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 485 | * @param int $stackPtr The position of a T_NEW token. |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 521 | |||
| 522 | |||
| 523 | /** |
||
| 524 | * Returns the fully qualified name of the class that the specified class extends. |
||
| 525 | * |
||
| 526 | * Returns an empty string if the class does not extend another class or if |
||
| 527 | * the class name could not be reliably inferred. |
||
| 528 | * |
||
| 529 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 530 | * @param int $stackPtr The position of a T_CLASS token. |
||
| 531 | * |
||
| 532 | * @return string |
||
| 533 | */ |
||
| 534 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 554 | |||
| 555 | |||
| 556 | /** |
||
| 557 | * Returns the class name for the static usage of a class. |
||
| 558 | * This can be a call to a method, the use of a property or constant. |
||
| 559 | * |
||
| 560 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 561 | * |
||
| 562 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 563 | * @param int $stackPtr The position of a T_NEW token. |
||
| 564 | * |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 613 | |||
| 614 | |||
| 615 | /** |
||
| 616 | * Get the Fully Qualified name for a class/function/constant etc. |
||
| 617 | * |
||
| 618 | * Checks if a class/function/constant name is already fully qualified and |
||
| 619 | * if not, enrich it with the relevant namespace information. |
||
| 620 | * |
||
| 621 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 622 | * @param int $stackPtr The position of the token. |
||
| 623 | * @param string $name The class / function / constant name. |
||
| 624 | * |
||
| 625 | * @return string |
||
| 626 | */ |
||
| 627 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
| 648 | |||
| 649 | |||
| 650 | /** |
||
| 651 | * Is the class/function/constant name namespaced or global ? |
||
| 652 | * |
||
| 653 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
| 654 | * I.e. should always start with a `\` ! |
||
| 655 | * |
||
| 656 | * @return bool True if namespaced, false if global. |
||
| 657 | */ |
||
| 658 | public function isNamespaced($FQName) { |
||
| 665 | |||
| 666 | |||
| 667 | /** |
||
| 668 | * Determine the namespace name an arbitrary token lives in. |
||
| 669 | * |
||
| 670 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 671 | * @param int $stackPtr The token position for which to determine the namespace. |
||
| 672 | * |
||
| 673 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
| 674 | */ |
||
| 675 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Get the complete namespace name for a namespace declaration. |
||
| 728 | * |
||
| 729 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
| 730 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
| 731 | * |
||
| 732 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 733 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
| 734 | * |
||
| 735 | * @return string|false Namespace name or false if not a namespace declaration. |
||
| 736 | * Namespace name can be an empty string for global namespace declaration. |
||
| 737 | */ |
||
| 738 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
||
| 778 | |||
| 779 | |||
| 780 | /** |
||
| 781 | * Returns the method parameters for the specified T_FUNCTION token. |
||
| 782 | * |
||
| 783 | * Each parameter is in the following format: |
||
| 784 | * |
||
| 785 | * <code> |
||
| 786 | * 0 => array( |
||
| 787 | * 'name' => '$var', // The variable name. |
||
| 788 | * 'pass_by_reference' => false, // Passed by reference. |
||
| 789 | * 'type_hint' => string, // Type hint for array or custom type |
||
| 790 | * ) |
||
| 791 | * </code> |
||
| 792 | * |
||
| 793 | * Parameters with default values have an additional array index of |
||
| 794 | * 'default' with the value of the default as a string. |
||
| 795 | * |
||
| 796 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
| 797 | * class, but with some improvements which were only introduced in PHPCS 2.7. |
||
| 798 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
| 799 | * that, this method can be removed and calls to it replaced with |
||
| 800 | * `$phpcsFile->getMethodParameters($stackPtr)` calls.}} |
||
| 801 | * |
||
| 802 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 803 | * @param int $stackPtr The position in the stack of the T_FUNCTION token |
||
| 804 | * to acquire the parameters for. |
||
| 805 | * |
||
| 806 | * @return array|false |
||
| 807 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
| 808 | * type T_FUNCTION. |
||
| 809 | */ |
||
| 810 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 955 | |||
| 956 | |||
| 957 | /** |
||
| 958 | * Get the hash algorithm name from the parameter in a hash function call. |
||
| 959 | * |
||
| 960 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 961 | * @param int $stackPtr The position of the T_STRING function token. |
||
| 962 | * |
||
| 963 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
| 964 | * function call or false if it was not. |
||
| 965 | */ |
||
| 966 | public function maybeGetHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1001 | |||
| 1002 | }//end class |
||
| 1003 |
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.