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 | const REGEX_COMPLEX_VARS = '`(?:(\{)?(?<!\\\\)\$)?(\{)?(?<!\\\\)\$(\{)?(?P<varname>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:->\$?(?P>varname)|\[[^\]]+\]|::\$?(?P>varname)|\([^\)]*\))*(?(3)\}|)(?(2)\}|)(?(1)\}|)`'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * List of functions using hash algorithm as parameter (always the first parameter). |
||
| 29 | * |
||
| 30 | * Used by the new/removed hash algorithm sniffs. |
||
| 31 | * Key is the function name, value is the 1-based parameter position in the function call. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $hashAlgoFunctions = array( |
||
| 36 | 'hash_file' => 1, |
||
| 37 | 'hash_hmac_file' => 1, |
||
| 38 | 'hash_hmac' => 1, |
||
| 39 | 'hash_init' => 1, |
||
| 40 | 'hash_pbkdf2' => 1, |
||
| 41 | 'hash' => 1, |
||
| 42 | ); |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * List of functions which take an ini directive as parameter (always the first parameter). |
||
| 47 | * |
||
| 48 | * Used by the new/removed ini directives sniffs. |
||
| 49 | * Key is the function name, value is the 1-based parameter position in the function call. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $iniFunctions = array( |
||
| 54 | 'ini_get' => 1, |
||
| 55 | 'ini_set' => 1, |
||
| 56 | ); |
||
| 57 | |||
| 58 | |||
| 59 | /* The testVersion configuration variable may be in any of the following formats: |
||
| 60 | * 1) Omitted/empty, in which case no version is specified. This effectively |
||
| 61 | * disables all the checks provided by this standard. |
||
| 62 | * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
||
| 63 | * the code will run on that version of PHP (no deprecated features or newer |
||
| 64 | * features being used). |
||
| 65 | * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
||
| 66 | * on all PHP versions in that range, and that it doesn't use any features that |
||
| 67 | * were deprecated by the final version in the list, or which were not available |
||
| 68 | * for the first version in the list. |
||
| 69 | * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
||
| 70 | * would be treated as invalid, and ignored. |
||
| 71 | * This standard doesn't support checking against PHP4, so the minimum version that |
||
| 72 | * is recognised is "5.0". |
||
| 73 | */ |
||
| 74 | |||
| 75 | private function getTestVersion() |
||
| 116 | |||
| 117 | View Code Duplication | public function supportsAbove($phpVersion) |
|
| 130 | |||
| 131 | View Code Duplication | public function supportsBelow($phpVersion) |
|
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * Add a PHPCS message to the output stack as either a warning or an error. |
||
| 148 | * |
||
| 149 | * @param PHP_CodeSniffer_File $phpcsFile The file the message applies to. |
||
| 150 | * @param string $message The message. |
||
| 151 | * @param int $stackPtr The position of the token |
||
| 152 | * the message relates to. |
||
| 153 | * @param bool $isError Whether to report the message as an |
||
| 154 | * 'error' or 'warning'. |
||
| 155 | * Defaults to true (error). |
||
| 156 | * @param string $code The error code for the message. |
||
| 157 | * Defaults to 'Found'. |
||
| 158 | * @param array $data Optional input for the data replacements. |
||
| 159 | * |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | public function addMessage($phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array()) |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Convert an arbitrary string to an alphanumeric string with underscores. |
||
| 174 | * |
||
| 175 | * Pre-empt issues with arbitrary strings being used as error codes in XML and PHP. |
||
| 176 | * |
||
| 177 | * @param string $baseString Arbitrary string. |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function stringToErrorCode($baseString) |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Strip quotes surrounding an arbitrary string. |
||
| 189 | * |
||
| 190 | * Intended for use with the content of a T_CONSTANT_ENCAPSED_STRING / T_DOUBLE_QUOTED_STRING. |
||
| 191 | * |
||
| 192 | * @param string $string The raw string. |
||
| 193 | * |
||
| 194 | * @return string String without quotes around it. |
||
| 195 | */ |
||
| 196 | public function stripQuotes($string) { |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * Strip variables from an arbitrary double quoted string. |
||
| 203 | * |
||
| 204 | * Intended for use with the content of a T_DOUBLE_QUOTED_STRING. |
||
| 205 | * |
||
| 206 | * @param string $string The raw string. |
||
| 207 | * |
||
| 208 | * @return string String without variables in it. |
||
| 209 | */ |
||
| 210 | public function stripVariables($string) { |
||
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * Make all top level array keys in an array lowercase. |
||
| 221 | * |
||
| 222 | * @param array $array Initial array. |
||
| 223 | * |
||
| 224 | * @return array Same array, but with all lowercase top level keys. |
||
| 225 | */ |
||
| 226 | public function arrayKeysToLowercase($array) |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
| 236 | * |
||
| 237 | * Returns FALSE on error or if there are no implemented interface names. |
||
| 238 | * |
||
| 239 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
| 240 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
| 241 | * that, this method can be removed and call to it replaced with |
||
| 242 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
| 243 | * |
||
| 244 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 245 | * @param int $stackPtr The position of the class token. |
||
| 246 | * |
||
| 247 | * @return array|false |
||
| 248 | */ |
||
| 249 | public function findImplementedInterfaceNames(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * Checks if a function call has parameters. |
||
| 300 | * |
||
| 301 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 302 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 303 | * |
||
| 304 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, it |
||
| 305 | * will detect whether the array has values or is empty. |
||
| 306 | * |
||
| 307 | * @link https://github.com/wimg/PHPCompatibility/issues/120 |
||
| 308 | * @link https://github.com/wimg/PHPCompatibility/issues/152 |
||
| 309 | * |
||
| 310 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 311 | * @param int $stackPtr The position of the function call token. |
||
| 312 | * |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * Count the number of parameters a function call has been passed. |
||
| 370 | * |
||
| 371 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 372 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 373 | * |
||
| 374 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
||
| 375 | * it will return the number of values in the array. |
||
| 376 | * |
||
| 377 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
| 378 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
| 379 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
| 380 | * |
||
| 381 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 382 | * @param int $stackPtr The position of the function call token. |
||
| 383 | * |
||
| 384 | * @return int |
||
| 385 | */ |
||
| 386 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 394 | |||
| 395 | |||
| 396 | /** |
||
| 397 | * Get information on all parameters passed to a function call. |
||
| 398 | * |
||
| 399 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 400 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 401 | * |
||
| 402 | * Will return an multi-dimentional array with the start token pointer, end token |
||
| 403 | * pointer and raw parameter value for all parameters. Index will be 1-based. |
||
| 404 | * If no parameters are found, will return an empty array. |
||
| 405 | * |
||
| 406 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
||
| 407 | * it will tokenize the values / key/value pairs contained in the array call. |
||
| 408 | * |
||
| 409 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 410 | * @param int $stackPtr The position of the function call token. |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 497 | |||
| 498 | |||
| 499 | /** |
||
| 500 | * Get information on a specific parameter passed to a function call. |
||
| 501 | * |
||
| 502 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 503 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 504 | * |
||
| 505 | * Will return a array with the start token pointer, end token pointer and the raw value |
||
| 506 | * of the parameter at a specific offset. |
||
| 507 | * If the specified parameter is not found, will return false. |
||
| 508 | * |
||
| 509 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 510 | * @param int $stackPtr The position of the function call token. |
||
| 511 | * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
||
| 512 | * |
||
| 513 | * @return array|false |
||
| 514 | */ |
||
| 515 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
||
| 526 | |||
| 527 | |||
| 528 | /** |
||
| 529 | * Verify whether a token is within a scoped condition. |
||
| 530 | * |
||
| 531 | * If the optional $validScopes parameter has been passed, the function |
||
| 532 | * will check that the token has at least one condition which is of a |
||
| 533 | * type defined in $validScopes. |
||
| 534 | * |
||
| 535 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 536 | * @param int $stackPtr The position of the token. |
||
| 537 | * @param array|int $validScopes Optional. Array of valid scopes |
||
| 538 | * or int value of a valid scope. |
||
| 539 | * Pass the T_.. constant(s) for the |
||
| 540 | * desired scope to this parameter. |
||
| 541 | * |
||
| 542 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
| 543 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
| 544 | * valid scope, false otherwise. |
||
| 545 | */ |
||
| 546 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
| 567 | |||
| 568 | |||
| 569 | /** |
||
| 570 | * Verify whether a token is within a class scope. |
||
| 571 | * |
||
| 572 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 573 | * @param int $stackPtr The position of the token. |
||
| 574 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
| 575 | * scope or also accept interfaces and traits |
||
| 576 | * as scope. |
||
| 577 | * |
||
| 578 | * @return bool True if within class scope, false otherwise. |
||
| 579 | */ |
||
| 580 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
| 594 | |||
| 595 | |||
| 596 | /** |
||
| 597 | * Verify whether a token is within a scoped use statement. |
||
| 598 | * |
||
| 599 | * PHPCS cross-version compatibility method. |
||
| 600 | * |
||
| 601 | * In PHPCS 1.x no conditions are set for a scoped use statement. |
||
| 602 | * This method works around that limitation. |
||
| 603 | * |
||
| 604 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 605 | * @param int $stackPtr The position of the token. |
||
| 606 | * |
||
| 607 | * @return bool True if within use scope, false otherwise. |
||
| 608 | */ |
||
| 609 | public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 641 | |||
| 642 | |||
| 643 | /** |
||
| 644 | * Returns the fully qualified class name for a new class instantiation. |
||
| 645 | * |
||
| 646 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 647 | * |
||
| 648 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 649 | * @param int $stackPtr The position of a T_NEW token. |
||
| 650 | * |
||
| 651 | * @return string |
||
| 652 | */ |
||
| 653 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 690 | |||
| 691 | |||
| 692 | /** |
||
| 693 | * Returns the fully qualified name of the class that the specified class extends. |
||
| 694 | * |
||
| 695 | * Returns an empty string if the class does not extend another class or if |
||
| 696 | * the class name could not be reliably inferred. |
||
| 697 | * |
||
| 698 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 699 | * @param int $stackPtr The position of a T_CLASS token. |
||
| 700 | * |
||
| 701 | * @return string |
||
| 702 | */ |
||
| 703 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 723 | |||
| 724 | |||
| 725 | /** |
||
| 726 | * Returns the class name for the static usage of a class. |
||
| 727 | * This can be a call to a method, the use of a property or constant. |
||
| 728 | * |
||
| 729 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 730 | * |
||
| 731 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 732 | * @param int $stackPtr The position of a T_NEW token. |
||
| 733 | * |
||
| 734 | * @return string |
||
| 735 | */ |
||
| 736 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 782 | |||
| 783 | |||
| 784 | /** |
||
| 785 | * Get the Fully Qualified name for a class/function/constant etc. |
||
| 786 | * |
||
| 787 | * Checks if a class/function/constant name is already fully qualified and |
||
| 788 | * if not, enrich it with the relevant namespace information. |
||
| 789 | * |
||
| 790 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 791 | * @param int $stackPtr The position of the token. |
||
| 792 | * @param string $name The class / function / constant name. |
||
| 793 | * |
||
| 794 | * @return string |
||
| 795 | */ |
||
| 796 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
| 817 | |||
| 818 | |||
| 819 | /** |
||
| 820 | * Is the class/function/constant name namespaced or global ? |
||
| 821 | * |
||
| 822 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
| 823 | * I.e. should always start with a `\` ! |
||
| 824 | * |
||
| 825 | * @return bool True if namespaced, false if global. |
||
| 826 | */ |
||
| 827 | public function isNamespaced($FQName) { |
||
| 834 | |||
| 835 | |||
| 836 | /** |
||
| 837 | * Determine the namespace name an arbitrary token lives in. |
||
| 838 | * |
||
| 839 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 840 | * @param int $stackPtr The token position for which to determine the namespace. |
||
| 841 | * |
||
| 842 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
| 843 | */ |
||
| 844 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 894 | |||
| 895 | /** |
||
| 896 | * Get the complete namespace name for a namespace declaration. |
||
| 897 | * |
||
| 898 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
| 899 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
| 900 | * |
||
| 901 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 902 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
| 903 | * |
||
| 904 | * @return string|false Namespace name or false if not a namespace declaration. |
||
| 905 | * Namespace name can be an empty string for global namespace declaration. |
||
| 906 | */ |
||
| 907 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
||
| 947 | |||
| 948 | |||
| 949 | /** |
||
| 950 | * Get the stack pointer for a return type token for a given function. |
||
| 951 | * |
||
| 952 | * Compatible layer for older PHPCS versions which don't recognize |
||
| 953 | * return type hints correctly. |
||
| 954 | * |
||
| 955 | * Expects to be passed T_RETURN_TYPE, T_FUNCTION or T_CLOSURE token. |
||
| 956 | * |
||
| 957 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 958 | * @param int $stackPtr The position of the token. |
||
| 959 | * |
||
| 960 | * @return int|false Stack pointer to the return type token or false if |
||
| 961 | * no return type was found or the passed token was |
||
| 962 | * not of the correct type. |
||
| 963 | */ |
||
| 964 | public function getReturnTypeHintToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1001 | |||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Returns the method parameters for the specified function token. |
||
| 1005 | * |
||
| 1006 | * Each parameter is in the following format: |
||
| 1007 | * |
||
| 1008 | * <code> |
||
| 1009 | * 0 => array( |
||
| 1010 | * 'name' => '$var', // The variable name. |
||
| 1011 | * 'content' => string, // The full content of the variable definition. |
||
| 1012 | * 'pass_by_reference' => boolean, // Is the variable passed by reference? |
||
| 1013 | * 'type_hint' => string, // The type hint for the variable. |
||
| 1014 | * 'nullable_type' => boolean, // Is the variable using a nullable type? |
||
| 1015 | * ) |
||
| 1016 | * </code> |
||
| 1017 | * |
||
| 1018 | * Parameters with default values have an additional array index of |
||
| 1019 | * 'default' with the value of the default as a string. |
||
| 1020 | * |
||
| 1021 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
| 1022 | * class, but with some improvements which have been introduced in |
||
| 1023 | * PHPCS 2.8.0. |
||
| 1024 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1117}, |
||
| 1025 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} and |
||
| 1026 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1293}. |
||
| 1027 | * |
||
| 1028 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
| 1029 | * that, this method can be removed and calls to it replaced with |
||
| 1030 | * `$phpcsFile->getMethodParameters($stackPtr)` calls. |
||
| 1031 | * |
||
| 1032 | * NOTE: This version does not deal with the new T_NULLABLE token type. |
||
| 1033 | * This token is included upstream only in 2.7.2+ and as we defer to upstream |
||
| 1034 | * in that case, no need to deal with it here. |
||
| 1035 | * |
||
| 1036 | * Last synced with PHPCS version: PHPCS 2.7.2-alpha.}} |
||
| 1037 | * |
||
| 1038 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 1039 | * @param int $stackPtr The position in the stack of the |
||
| 1040 | * function token to acquire the |
||
| 1041 | * parameters for. |
||
| 1042 | * |
||
| 1043 | * @return array|false |
||
| 1044 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
| 1045 | * type T_FUNCTION or T_CLOSURE. |
||
| 1046 | */ |
||
| 1047 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1203 | |||
| 1204 | |||
| 1205 | /** |
||
| 1206 | * Get the hash algorithm name from the parameter in a hash function call. |
||
| 1207 | * |
||
| 1208 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 1209 | * @param int $stackPtr The position of the T_STRING function token. |
||
| 1210 | * |
||
| 1211 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
| 1212 | * function call or false if it was not. |
||
| 1213 | */ |
||
| 1214 | public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1249 | |||
| 1250 | }//end class |
||
| 1251 |
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.