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 | /** |
||
| 44 | * List of functions which take an ini directive as parameter (always the first parameter). |
||
| 45 | * |
||
| 46 | * Used by the new/removed ini directives sniffs. |
||
| 47 | * Key is the function name, value is the 1-based parameter position in the function call. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $iniFunctions = array( |
||
| 52 | 'ini_get' => 1, |
||
| 53 | 'ini_set' => 1, |
||
| 54 | ); |
||
| 55 | |||
| 56 | |||
| 57 | /* The testVersion configuration variable may be in any of the following formats: |
||
| 58 | * 1) Omitted/empty, in which case no version is specified. This effectively |
||
| 59 | * disables all the checks provided by this standard. |
||
| 60 | * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
||
| 61 | * the code will run on that version of PHP (no deprecated features or newer |
||
| 62 | * features being used). |
||
| 63 | * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
||
| 64 | * on all PHP versions in that range, and that it doesn't use any features that |
||
| 65 | * were deprecated by the final version in the list, or which were not available |
||
| 66 | * for the first version in the list. |
||
| 67 | * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
||
| 68 | * would be treated as invalid, and ignored. |
||
| 69 | * This standard doesn't support checking against PHP4, so the minimum version that |
||
| 70 | * is recognised is "5.0". |
||
| 71 | */ |
||
| 72 | |||
| 73 | private function getTestVersion() |
||
| 114 | |||
| 115 | View Code Duplication | public function supportsAbove($phpVersion) |
|
| 128 | |||
| 129 | View Code Duplication | public function supportsBelow($phpVersion) |
|
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Add a PHPCS message to the output stack as either a warning or an error. |
||
| 146 | * |
||
| 147 | * @param PHP_CodeSniffer_File $phpcsFile The file the message applies to. |
||
| 148 | * @param string $message The message. |
||
| 149 | * @param int $stackPtr The position of the token |
||
| 150 | * the message relates to. |
||
| 151 | * @param bool $isError Whether to report the message as an |
||
| 152 | * 'error' or 'warning'. |
||
| 153 | * Defaults to true (error). |
||
| 154 | * @param string $code The error code for the message. |
||
| 155 | * Defaults to 'Found'. |
||
| 156 | * @param array $data Optional input for the data replacements. |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | public function addMessage($phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array()) |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Convert an arbitrary string to an alphanumeric string with underscores. |
||
| 172 | * |
||
| 173 | * Pre-empt issues with arbitrary strings being used as error codes in XML and PHP. |
||
| 174 | * |
||
| 175 | * @param string $baseString Arbitrary string. |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function stringToErrorCode($baseString) |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * Strip quotes surrounding an arbitrary string. |
||
| 187 | * |
||
| 188 | * Intended for use with the content of a T_CONSTANT_ENCAPSED_STRING. |
||
| 189 | * |
||
| 190 | * @param string $string The raw string. |
||
| 191 | * |
||
| 192 | * @return string String without quotes around it. |
||
| 193 | */ |
||
| 194 | public function stripQuotes($string) { |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * Make all top level array keys in an array lowercase. |
||
| 201 | * |
||
| 202 | * @param array $array Initial array. |
||
| 203 | * |
||
| 204 | * @return array Same array, but with all lowercase top level keys. |
||
| 205 | */ |
||
| 206 | public function arrayKeysToLowercase($array) |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
| 216 | * |
||
| 217 | * Returns FALSE on error or if there are no implemented interface names. |
||
| 218 | * |
||
| 219 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
| 220 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
| 221 | * that, this method can be removed and call to it replaced with |
||
| 222 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
| 223 | * |
||
| 224 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 225 | * @param int $stackPtr The position of the class token. |
||
| 226 | * |
||
| 227 | * @return array|false |
||
| 228 | */ |
||
| 229 | public function findImplementedInterfaceNames(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * Checks if a function call has parameters. |
||
| 280 | * |
||
| 281 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 282 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 283 | * |
||
| 284 | * @link https://github.com/wimg/PHPCompatibility/issues/120 |
||
| 285 | * @link https://github.com/wimg/PHPCompatibility/issues/152 |
||
| 286 | * |
||
| 287 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 288 | * @param int $stackPtr The position of the function call token. |
||
| 289 | * |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * Count the number of parameters a function call has been passed. |
||
| 329 | * |
||
| 330 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 331 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 332 | * |
||
| 333 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
| 334 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
| 335 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
| 336 | * |
||
| 337 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 338 | * @param int $stackPtr The position of the function call token. |
||
| 339 | * |
||
| 340 | * @return int |
||
| 341 | */ |
||
| 342 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * Get information on all parameters passed to a function call. |
||
| 354 | * |
||
| 355 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 356 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 357 | * |
||
| 358 | * Will return an multi-dimentional array with the start token pointer, end token |
||
| 359 | * pointer and raw parameter value for all parameters. Index will be 1-based. |
||
| 360 | * If no parameters are found, will return an empty array. |
||
| 361 | * |
||
| 362 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 363 | * @param int $stackPtr The position of the function call token. |
||
| 364 | * |
||
| 365 | * @return array |
||
| 366 | */ |
||
| 367 | public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 439 | |||
| 440 | |||
| 441 | /** |
||
| 442 | * Get information on a specific parameter passed to a function call. |
||
| 443 | * |
||
| 444 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 445 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 446 | * |
||
| 447 | * Will return a array with the start token pointer, end token pointer and the raw value |
||
| 448 | * of the parameter at a specific offset. |
||
| 449 | * If the specified parameter is not found, will return false. |
||
| 450 | * |
||
| 451 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 452 | * @param int $stackPtr The position of the function call token. |
||
| 453 | * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
||
| 454 | * |
||
| 455 | * @return array|false |
||
| 456 | */ |
||
| 457 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
||
| 468 | |||
| 469 | |||
| 470 | /** |
||
| 471 | * Verify whether a token is within a scoped condition. |
||
| 472 | * |
||
| 473 | * If the optional $validScopes parameter has been passed, the function |
||
| 474 | * will check that the token has at least one condition which is of a |
||
| 475 | * type defined in $validScopes. |
||
| 476 | * |
||
| 477 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 478 | * @param int $stackPtr The position of the token. |
||
| 479 | * @param array|int $validScopes Optional. Array of valid scopes |
||
| 480 | * or int value of a valid scope. |
||
| 481 | * Pass the T_.. constant(s) for the |
||
| 482 | * desired scope to this parameter. |
||
| 483 | * |
||
| 484 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
| 485 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
| 486 | * valid scope, false otherwise. |
||
| 487 | */ |
||
| 488 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
| 526 | |||
| 527 | |||
| 528 | /** |
||
| 529 | * Verify whether a token is within a class scope. |
||
| 530 | * |
||
| 531 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 532 | * @param int $stackPtr The position of the token. |
||
| 533 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
| 534 | * scope or also accept interfaces and traits |
||
| 535 | * as scope. |
||
| 536 | * |
||
| 537 | * @return bool True if within class scope, false otherwise. |
||
| 538 | */ |
||
| 539 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
| 549 | |||
| 550 | |||
| 551 | /** |
||
| 552 | * Verify whether a token is within a scoped use statement. |
||
| 553 | * |
||
| 554 | * PHPCS cross-version compatibility method. |
||
| 555 | * |
||
| 556 | * In PHPCS 1.x no conditions are set for a scoped use statement. |
||
| 557 | * This method works around that limitation. |
||
| 558 | * |
||
| 559 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 560 | * @param int $stackPtr The position of the token. |
||
| 561 | * |
||
| 562 | * @return bool True if within use scope, false otherwise. |
||
| 563 | */ |
||
| 564 | public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 596 | |||
| 597 | |||
| 598 | /** |
||
| 599 | * Returns the fully qualified class name for a new class instantiation. |
||
| 600 | * |
||
| 601 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 602 | * |
||
| 603 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 604 | * @param int $stackPtr The position of a T_NEW token. |
||
| 605 | * |
||
| 606 | * @return string |
||
| 607 | */ |
||
| 608 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 640 | |||
| 641 | |||
| 642 | /** |
||
| 643 | * Returns the fully qualified name of the class that the specified class extends. |
||
| 644 | * |
||
| 645 | * Returns an empty string if the class does not extend another class or if |
||
| 646 | * 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_CLASS token. |
||
| 650 | * |
||
| 651 | * @return string |
||
| 652 | */ |
||
| 653 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 673 | |||
| 674 | |||
| 675 | /** |
||
| 676 | * Returns the class name for the static usage of a class. |
||
| 677 | * This can be a call to a method, the use of a property or constant. |
||
| 678 | * |
||
| 679 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 680 | * |
||
| 681 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 682 | * @param int $stackPtr The position of a T_NEW token. |
||
| 683 | * |
||
| 684 | * @return string |
||
| 685 | */ |
||
| 686 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 732 | |||
| 733 | |||
| 734 | /** |
||
| 735 | * Get the Fully Qualified name for a class/function/constant etc. |
||
| 736 | * |
||
| 737 | * Checks if a class/function/constant name is already fully qualified and |
||
| 738 | * if not, enrich it with the relevant namespace information. |
||
| 739 | * |
||
| 740 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 741 | * @param int $stackPtr The position of the token. |
||
| 742 | * @param string $name The class / function / constant name. |
||
| 743 | * |
||
| 744 | * @return string |
||
| 745 | */ |
||
| 746 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
| 767 | |||
| 768 | |||
| 769 | /** |
||
| 770 | * Is the class/function/constant name namespaced or global ? |
||
| 771 | * |
||
| 772 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
| 773 | * I.e. should always start with a `\` ! |
||
| 774 | * |
||
| 775 | * @return bool True if namespaced, false if global. |
||
| 776 | */ |
||
| 777 | public function isNamespaced($FQName) { |
||
| 784 | |||
| 785 | |||
| 786 | /** |
||
| 787 | * Determine the namespace name an arbitrary token lives in. |
||
| 788 | * |
||
| 789 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 790 | * @param int $stackPtr The token position for which to determine the namespace. |
||
| 791 | * |
||
| 792 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
| 793 | */ |
||
| 794 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 844 | |||
| 845 | /** |
||
| 846 | * Get the complete namespace name for a namespace declaration. |
||
| 847 | * |
||
| 848 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
| 849 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
| 850 | * |
||
| 851 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 852 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
| 853 | * |
||
| 854 | * @return string|false Namespace name or false if not a namespace declaration. |
||
| 855 | * Namespace name can be an empty string for global namespace declaration. |
||
| 856 | */ |
||
| 857 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
||
| 897 | |||
| 898 | |||
| 899 | /** |
||
| 900 | * Returns the method parameters for the specified T_FUNCTION token. |
||
| 901 | * |
||
| 902 | * Each parameter is in the following format: |
||
| 903 | * |
||
| 904 | * <code> |
||
| 905 | * 0 => array( |
||
| 906 | * 'name' => '$var', // The variable name. |
||
| 907 | * 'pass_by_reference' => false, // Passed by reference. |
||
| 908 | * 'type_hint' => string, // Type hint for array or custom type |
||
| 909 | * 'nullable_type' => bool, // Whether the type given in the type hint is nullable |
||
| 910 | * 'type_hint' => string, // Type hint for array or custom type |
||
| 911 | * 'raw' => string, // Raw content of the tokens for the parameter |
||
| 912 | * ) |
||
| 913 | * </code> |
||
| 914 | * |
||
| 915 | * Parameters with default values have an additional array index of |
||
| 916 | * 'default' with the value of the default as a string. |
||
| 917 | * |
||
| 918 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
| 919 | * class, but with some improvements which will probably be introduced in |
||
| 920 | * PHPCS 2.7.1/2.8. {@see https://github.com/squizlabs/PHP_CodeSniffer/pull/1117} |
||
| 921 | * and {@see https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} |
||
| 922 | * |
||
| 923 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
| 924 | * that, this method can be removed and calls to it replaced with |
||
| 925 | * `$phpcsFile->getMethodParameters($stackPtr)` calls. |
||
| 926 | * |
||
| 927 | * Last synced with PHPCS version: PHPCS 2.7.}} |
||
| 928 | * |
||
| 929 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 930 | * @param int $stackPtr The position in the stack of the |
||
| 931 | * T_FUNCTION token to acquire the |
||
| 932 | * parameters for. |
||
| 933 | * |
||
| 934 | * @return array|false |
||
| 935 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
| 936 | * type T_FUNCTION. |
||
| 937 | */ |
||
| 938 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 939 | { |
||
| 940 | $tokens = $phpcsFile->getTokens(); |
||
| 941 | |||
| 942 | // Check for the existence of the token. |
||
| 943 | if (isset($tokens[$stackPtr]) === false) { |
||
| 944 | return false; |
||
| 945 | } |
||
| 946 | |||
| 947 | if ($tokens[$stackPtr]['code'] !== T_FUNCTION) { |
||
| 948 | throw new PHP_CodeSniffer_Exception('$stackPtr must be of type T_FUNCTION'); |
||
| 949 | } |
||
| 950 | |||
| 951 | $opener = $tokens[$stackPtr]['parenthesis_opener']; |
||
| 952 | $closer = $tokens[$stackPtr]['parenthesis_closer']; |
||
| 953 | |||
| 954 | $vars = array(); |
||
| 955 | $currVar = null; |
||
| 956 | $paramStart = ($opener + 1); |
||
| 957 | $defaultStart = null; |
||
| 958 | $paramCount = 0; |
||
| 959 | $passByReference = false; |
||
| 960 | $variableLength = false; |
||
| 961 | $typeHint = ''; |
||
| 962 | $nullableType = false; |
||
| 963 | |||
| 964 | for ($i = $paramStart; $i <= $closer; $i++) { |
||
| 965 | // Check to see if this token has a parenthesis or bracket opener. If it does |
||
| 966 | // it's likely to be an array which might have arguments in it. This |
||
| 967 | // could cause problems in our parsing below, so lets just skip to the |
||
| 968 | // end of it. |
||
| 969 | View Code Duplication | if (isset($tokens[$i]['parenthesis_opener']) === true) { |
|
|
1 ignored issue
–
show
|
|||
| 970 | // Don't do this if it's the close parenthesis for the method. |
||
| 971 | if ($i !== $tokens[$i]['parenthesis_closer']) { |
||
| 972 | $i = ($tokens[$i]['parenthesis_closer'] + 1); |
||
| 973 | } |
||
| 974 | } |
||
| 975 | |||
| 976 | View Code Duplication | if (isset($tokens[$i]['bracket_opener']) === true) { |
|
|
1 ignored issue
–
show
|
|||
| 977 | // Don't do this if it's the close parenthesis for the method. |
||
| 978 | if ($i !== $tokens[$i]['bracket_closer']) { |
||
| 979 | $i = ($tokens[$i]['bracket_closer'] + 1); |
||
| 980 | } |
||
| 981 | } |
||
| 982 | |||
| 983 | switch ($tokens[$i]['code']) { |
||
| 984 | case T_BITWISE_AND: |
||
| 985 | $passByReference = true; |
||
| 986 | break; |
||
| 987 | case T_VARIABLE: |
||
| 988 | $currVar = $i; |
||
| 989 | break; |
||
| 990 | case T_ELLIPSIS: |
||
| 991 | $variableLength = true; |
||
| 992 | break; |
||
| 993 | case T_ARRAY_HINT: |
||
| 994 | case T_CALLABLE: |
||
| 995 | $typeHint .= $tokens[$i]['content']; |
||
| 996 | break; |
||
| 997 | case T_SELF: |
||
| 998 | case T_PARENT: |
||
| 999 | case T_STATIC: |
||
| 1000 | // Self is valid, the others invalid, but were probably intended as type hints. |
||
| 1001 | if ($defaultStart === null) { |
||
| 1002 | $typeHint .= $tokens[$i]['content']; |
||
| 1003 | } |
||
| 1004 | break; |
||
| 1005 | case T_STRING: |
||
| 1006 | // This is a string, so it may be a type hint, but it could |
||
| 1007 | // also be a constant used as a default value. |
||
| 1008 | $prevComma = false; |
||
| 1009 | View Code Duplication | for ($t = $i; $t >= $opener; $t--) { |
|
|
1 ignored issue
–
show
|
|||
| 1010 | if ($tokens[$t]['code'] === T_COMMA) { |
||
| 1011 | $prevComma = $t; |
||
| 1012 | break; |
||
| 1013 | } |
||
| 1014 | } |
||
| 1015 | |||
| 1016 | if ($prevComma !== false) { |
||
| 1017 | $nextEquals = false; |
||
| 1018 | View Code Duplication | for ($t = $prevComma; $t < $i; $t++) { |
|
|
1 ignored issue
–
show
|
|||
| 1019 | if ($tokens[$t]['code'] === T_EQUAL) { |
||
| 1020 | $nextEquals = $t; |
||
| 1021 | break; |
||
| 1022 | } |
||
| 1023 | } |
||
| 1024 | |||
| 1025 | if ($nextEquals !== false) { |
||
| 1026 | break; |
||
| 1027 | } |
||
| 1028 | } |
||
| 1029 | |||
| 1030 | if ($defaultStart === null) { |
||
| 1031 | $typeHint .= $tokens[$i]['content']; |
||
| 1032 | } |
||
| 1033 | break; |
||
| 1034 | case T_NS_SEPARATOR: |
||
| 1035 | // Part of a type hint or default value. |
||
| 1036 | if ($defaultStart === null) { |
||
| 1037 | $typeHint .= $tokens[$i]['content']; |
||
| 1038 | } |
||
| 1039 | break; |
||
| 1040 | case T_INLINE_THEN: |
||
| 1041 | if ($defaultStart === null) { |
||
| 1042 | $nullableType = true; |
||
| 1043 | $typeHint .= $tokens[$i]['content']; |
||
| 1044 | } |
||
| 1045 | break; |
||
| 1046 | case T_CLOSE_PARENTHESIS: |
||
| 1047 | case T_COMMA: |
||
| 1048 | // If it's null, then there must be no parameters for this |
||
| 1049 | // method. |
||
| 1050 | if ($currVar === null) { |
||
| 1051 | continue; |
||
| 1052 | } |
||
| 1053 | |||
| 1054 | $vars[$paramCount] = array(); |
||
| 1055 | $vars[$paramCount]['name'] = $tokens[$currVar]['content']; |
||
| 1056 | |||
| 1057 | if ($defaultStart !== null) { |
||
| 1058 | $vars[$paramCount]['default'] |
||
| 1059 | = $phpcsFile->getTokensAsString( |
||
| 1060 | $defaultStart, |
||
| 1061 | ($i - $defaultStart) |
||
| 1062 | ); |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | $rawContent = trim($phpcsFile->getTokensAsString($paramStart, ($i - $paramStart))); |
||
| 1066 | |||
| 1067 | $vars[$paramCount]['pass_by_reference'] = $passByReference; |
||
| 1068 | $vars[$paramCount]['variable_length'] = $variableLength; |
||
| 1069 | $vars[$paramCount]['type_hint'] = $typeHint; |
||
| 1070 | $vars[$paramCount]['nullable_type'] = $nullableType; |
||
| 1071 | $vars[$paramCount]['raw'] = $rawContent; |
||
| 1072 | |||
| 1073 | // Reset the vars, as we are about to process the next parameter. |
||
| 1074 | $defaultStart = null; |
||
| 1075 | $paramStart = ($i + 1); |
||
| 1076 | $passByReference = false; |
||
| 1077 | $variableLength = false; |
||
| 1078 | $typeHint = ''; |
||
| 1079 | $nullableType = false; |
||
| 1080 | |||
| 1081 | $paramCount++; |
||
| 1082 | break; |
||
| 1083 | case T_EQUAL: |
||
| 1084 | $defaultStart = ($i + 1); |
||
| 1085 | break; |
||
| 1086 | }//end switch |
||
| 1087 | }//end for |
||
| 1088 | |||
| 1089 | return $vars; |
||
| 1090 | |||
| 1091 | }//end getMethodParameters() |
||
| 1092 | |||
| 1093 | |||
| 1094 | /** |
||
| 1095 | * Get the hash algorithm name from the parameter in a hash function call. |
||
| 1096 | * |
||
| 1097 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 1098 | * @param int $stackPtr The position of the T_STRING function token. |
||
| 1099 | * |
||
| 1100 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
| 1101 | * function call or false if it was not. |
||
| 1102 | */ |
||
| 1103 | public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1138 | |||
| 1139 | }//end class |
||
| 1140 |
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.