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 | * Pass the T_.. constant(s) for the |
||
| 412 | * desired scope to this parameter. |
||
| 413 | * |
||
| 414 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
| 415 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
| 416 | * valid scope, false otherwise. |
||
| 417 | */ |
||
| 418 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
| 456 | |||
| 457 | |||
| 458 | /** |
||
| 459 | * Verify whether a token is within a class scope. |
||
| 460 | * |
||
| 461 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 462 | * @param int $stackPtr The position of the token. |
||
| 463 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
| 464 | * scope or also accept interfaces and traits |
||
| 465 | * as scope. |
||
| 466 | * |
||
| 467 | * @return bool True if within class scope, false otherwise. |
||
| 468 | */ |
||
| 469 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
| 479 | |||
| 480 | |||
| 481 | /** |
||
| 482 | * Verify whether a token is within a scoped use statement. |
||
| 483 | * |
||
| 484 | * PHPCS cross-version compatibility method. |
||
| 485 | * |
||
| 486 | * In PHPCS 1.x no conditions are set for a scoped use statement. |
||
| 487 | * This method works around that limitation. |
||
| 488 | * |
||
| 489 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 490 | * @param int $stackPtr The position of the token. |
||
| 491 | * |
||
| 492 | * @return bool True if within use scope, false otherwise. |
||
| 493 | */ |
||
| 494 | public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 526 | |||
| 527 | |||
| 528 | /** |
||
| 529 | * Returns the fully qualified class name for a new class instantiation. |
||
| 530 | * |
||
| 531 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 532 | * |
||
| 533 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 534 | * @param int $stackPtr The position of a T_NEW token. |
||
| 535 | * |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 570 | |||
| 571 | |||
| 572 | /** |
||
| 573 | * Returns the fully qualified name of the class that the specified class extends. |
||
| 574 | * |
||
| 575 | * Returns an empty string if the class does not extend another class or if |
||
| 576 | * the class name could not be reliably inferred. |
||
| 577 | * |
||
| 578 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 579 | * @param int $stackPtr The position of a T_CLASS token. |
||
| 580 | * |
||
| 581 | * @return string |
||
| 582 | */ |
||
| 583 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 603 | |||
| 604 | |||
| 605 | /** |
||
| 606 | * Returns the class name for the static usage of a class. |
||
| 607 | * This can be a call to a method, the use of a property or constant. |
||
| 608 | * |
||
| 609 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 610 | * |
||
| 611 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 612 | * @param int $stackPtr The position of a T_NEW token. |
||
| 613 | * |
||
| 614 | * @return string |
||
| 615 | */ |
||
| 616 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 662 | |||
| 663 | |||
| 664 | /** |
||
| 665 | * Get the Fully Qualified name for a class/function/constant etc. |
||
| 666 | * |
||
| 667 | * Checks if a class/function/constant name is already fully qualified and |
||
| 668 | * if not, enrich it with the relevant namespace information. |
||
| 669 | * |
||
| 670 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 671 | * @param int $stackPtr The position of the token. |
||
| 672 | * @param string $name The class / function / constant name. |
||
| 673 | * |
||
| 674 | * @return string |
||
| 675 | */ |
||
| 676 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
| 697 | |||
| 698 | |||
| 699 | /** |
||
| 700 | * Is the class/function/constant name namespaced or global ? |
||
| 701 | * |
||
| 702 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
| 703 | * I.e. should always start with a `\` ! |
||
| 704 | * |
||
| 705 | * @return bool True if namespaced, false if global. |
||
| 706 | */ |
||
| 707 | public function isNamespaced($FQName) { |
||
| 714 | |||
| 715 | |||
| 716 | /** |
||
| 717 | * Determine the namespace name an arbitrary token lives in. |
||
| 718 | * |
||
| 719 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 720 | * @param int $stackPtr The token position for which to determine the namespace. |
||
| 721 | * |
||
| 722 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
| 723 | */ |
||
| 724 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Get the complete namespace name for a namespace declaration. |
||
| 777 | * |
||
| 778 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
| 779 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
| 780 | * |
||
| 781 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 782 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
| 783 | * |
||
| 784 | * @return string|false Namespace name or false if not a namespace declaration. |
||
| 785 | * Namespace name can be an empty string for global namespace declaration. |
||
| 786 | */ |
||
| 787 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
||
| 827 | |||
| 828 | |||
| 829 | /** |
||
| 830 | * Returns the method parameters for the specified T_FUNCTION token. |
||
| 831 | * |
||
| 832 | * Each parameter is in the following format: |
||
| 833 | * |
||
| 834 | * <code> |
||
| 835 | * 0 => array( |
||
| 836 | * 'name' => '$var', // The variable name. |
||
| 837 | * 'pass_by_reference' => false, // Passed by reference. |
||
| 838 | * 'type_hint' => string, // Type hint for array or custom type |
||
| 839 | * 'nullable_type' => bool, // Whether the type given in the type hint is nullable |
||
| 840 | * 'type_hint' => string, // Type hint for array or custom type |
||
| 841 | * 'raw' => string, // Raw content of the tokens for the parameter |
||
| 842 | * ) |
||
| 843 | * </code> |
||
| 844 | * |
||
| 845 | * Parameters with default values have an additional array index of |
||
| 846 | * 'default' with the value of the default as a string. |
||
| 847 | * |
||
| 848 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
| 849 | * class, but with some improvements which will probably be introduced in |
||
| 850 | * PHPCS 2.7.1/2.8. {@see https://github.com/squizlabs/PHP_CodeSniffer/pull/1117} |
||
| 851 | * and {@see https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} |
||
| 852 | * |
||
| 853 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
| 854 | * that, this method can be removed and calls to it replaced with |
||
| 855 | * `$phpcsFile->getMethodParameters($stackPtr)` calls. |
||
| 856 | * |
||
| 857 | * Last synced with PHPCS version: PHPCS 2.7.}} |
||
| 858 | * |
||
| 859 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 860 | * @param int $stackPtr The position in the stack of the |
||
| 861 | * T_FUNCTION token to acquire the |
||
| 862 | * parameters for. |
||
| 863 | * |
||
| 864 | * @return array|false |
||
| 865 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
| 866 | * type T_FUNCTION. |
||
| 867 | */ |
||
| 868 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1022 | |||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Get the hash algorithm name from the parameter in a hash function call. |
||
| 1026 | * |
||
| 1027 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 1028 | * @param int $stackPtr The position of the T_STRING function token. |
||
| 1029 | * |
||
| 1030 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
| 1031 | * function call or false if it was not. |
||
| 1032 | */ |
||
| 1033 | public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1068 | |||
| 1069 | }//end class |
||
| 1070 |
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.