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 |
||
| 19 | abstract class PHPCompatibility_Sniff implements PHP_CodeSniffer_Sniff |
||
|
|
|||
| 20 | { |
||
| 21 | |||
| 22 | const REGEX_COMPLEX_VARS = '`(?:(\{)?(?<!\\\\)\$)?(\{)?(?<!\\\\)\$(\{)?(?P<varname>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:->\$?(?P>varname)|\[[^\]]+\]|::\$?(?P>varname)|\([^\)]*\))*(?(3)\}|)(?(2)\}|)(?(1)\}|)`'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * List of superglobals as an array of strings. |
||
| 26 | * |
||
| 27 | * Used by the ParameterShadowSuperGlobals and ForbiddenClosureUseVariableNames sniffs. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $superglobals = array( |
||
| 32 | '$GLOBALS', |
||
| 33 | '$_SERVER', |
||
| 34 | '$_GET', |
||
| 35 | '$_POST', |
||
| 36 | '$_FILES', |
||
| 37 | '$_COOKIE', |
||
| 38 | '$_SESSION', |
||
| 39 | '$_REQUEST', |
||
| 40 | '$_ENV', |
||
| 41 | ); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * List of functions using hash algorithm as parameter (always the first parameter). |
||
| 45 | * |
||
| 46 | * Used by the new/removed hash algorithm 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 $hashAlgoFunctions = array( |
||
| 52 | 'hash_file' => 1, |
||
| 53 | 'hash_hmac_file' => 1, |
||
| 54 | 'hash_hmac' => 1, |
||
| 55 | 'hash_init' => 1, |
||
| 56 | 'hash_pbkdf2' => 1, |
||
| 57 | 'hash' => 1, |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * List of functions which take an ini directive as parameter (always the first parameter). |
||
| 62 | * |
||
| 63 | * Used by the new/removed ini directives sniffs. |
||
| 64 | * Key is the function name, value is the 1-based parameter position in the function call. |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $iniFunctions = array( |
||
| 69 | 'ini_get' => 1, |
||
| 70 | 'ini_set' => 1, |
||
| 71 | ); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Internal cache of the results of the `getFunctionCallParameters()` method. |
||
| 75 | * |
||
| 76 | * The results are stored in a multi-dimensional array by filename and $stackPtr of |
||
| 77 | * the function call/array token. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | private $getFunctionCallParametersCache = array(); |
||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Get the testVersion configuration variable. |
||
| 86 | * |
||
| 87 | * The testVersion configuration variable may be in any of the following formats: |
||
| 88 | * 1) Omitted/empty, in which case no version is specified. This effectively |
||
| 89 | * disables all the checks for new PHP features provided by this standard. |
||
| 90 | * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
||
| 91 | * the code will run on that version of PHP (no deprecated features or newer |
||
| 92 | * features being used). |
||
| 93 | * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
||
| 94 | * on all PHP versions in that range, and that it doesn't use any features that |
||
| 95 | * were deprecated by the final version in the list, or which were not available |
||
| 96 | * for the first version in the list. |
||
| 97 | * We accept ranges where one of the components is missing, e.g. "-5.6" means |
||
| 98 | * all versions up to PHP 5.6, and "7.0-" means all versions above PHP 7.0. |
||
| 99 | * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
||
| 100 | * would be treated as invalid, and ignored. |
||
| 101 | * |
||
| 102 | * @return array $arrTestVersions will hold an array containing min/max version |
||
| 103 | * of PHP that we are checking against (see above). If only a |
||
| 104 | * single version number is specified, then this is used as |
||
| 105 | * both the min and max. |
||
| 106 | * |
||
| 107 | * @throws PHP_CodeSniffer_Exception If testVersion is invalid. |
||
| 108 | */ |
||
| 109 | private function getTestVersion() |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Check whether a specific PHP version is equal to or higher than the maximum |
||
| 163 | * supported PHP version as provided by the user in `testVersion`. |
||
| 164 | * |
||
| 165 | * Should be used when sniffing for *old* PHP features (deprecated/removed). |
||
| 166 | * |
||
| 167 | * @param string $phpVersion A PHP version number in 'major.minor' format. |
||
| 168 | * |
||
| 169 | * @return bool True if testVersion has not been provided or if the PHP version |
||
| 170 | * is equal to or higher than the highest supported PHP version |
||
| 171 | * in testVersion. False otherwise. |
||
| 172 | */ |
||
| 173 | View Code Duplication | public function supportsAbove($phpVersion) |
|
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * Check whether a specific PHP version is equal to or lower than the minimum |
||
| 190 | * supported PHP version as provided by the user in `testVersion`. |
||
| 191 | * |
||
| 192 | * Should be used when sniffing for *new* PHP features. |
||
| 193 | * |
||
| 194 | * @param string $phpVersion A PHP version number in 'major.minor' format. |
||
| 195 | * |
||
| 196 | * @return bool True if the PHP version is equal to or lower than the lowest |
||
| 197 | * supported PHP version in testVersion. |
||
| 198 | * False otherwise or if no testVersion is provided. |
||
| 199 | */ |
||
| 200 | View Code Duplication | public function supportsBelow($phpVersion) |
|
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * Add a PHPCS message to the output stack as either a warning or an error. |
||
| 217 | * |
||
| 218 | * @param PHP_CodeSniffer_File $phpcsFile The file the message applies to. |
||
| 219 | * @param string $message The message. |
||
| 220 | * @param int $stackPtr The position of the token |
||
| 221 | * the message relates to. |
||
| 222 | * @param bool $isError Whether to report the message as an |
||
| 223 | * 'error' or 'warning'. |
||
| 224 | * Defaults to true (error). |
||
| 225 | * @param string $code The error code for the message. |
||
| 226 | * Defaults to 'Found'. |
||
| 227 | * @param array $data Optional input for the data replacements. |
||
| 228 | * |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | public function addMessage($phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array()) |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * Convert an arbitrary string to an alphanumeric string with underscores. |
||
| 243 | * |
||
| 244 | * Pre-empt issues with arbitrary strings being used as error codes in XML and PHP. |
||
| 245 | * |
||
| 246 | * @param string $baseString Arbitrary string. |
||
| 247 | * |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function stringToErrorCode($baseString) |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Strip quotes surrounding an arbitrary string. |
||
| 258 | * |
||
| 259 | * Intended for use with the content of a T_CONSTANT_ENCAPSED_STRING / T_DOUBLE_QUOTED_STRING. |
||
| 260 | * |
||
| 261 | * @param string $string The raw string. |
||
| 262 | * |
||
| 263 | * @return string String without quotes around it. |
||
| 264 | */ |
||
| 265 | public function stripQuotes($string) |
||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * Strip variables from an arbitrary double quoted string. |
||
| 273 | * |
||
| 274 | * Intended for use with the content of a T_DOUBLE_QUOTED_STRING. |
||
| 275 | * |
||
| 276 | * @param string $string The raw string. |
||
| 277 | * |
||
| 278 | * @return string String without variables in it. |
||
| 279 | */ |
||
| 280 | public function stripVariables($string) |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Make all top level array keys in an array lowercase. |
||
| 292 | * |
||
| 293 | * @param array $array Initial array. |
||
| 294 | * |
||
| 295 | * @return array Same array, but with all lowercase top level keys. |
||
| 296 | */ |
||
| 297 | public function arrayKeysToLowercase($array) |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
| 307 | * |
||
| 308 | * Returns FALSE on error or if there are no implemented interface names. |
||
| 309 | * |
||
| 310 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
| 311 | * This method also includes an improvement we use which was only introduced |
||
| 312 | * in PHPCS 2.8.0, so only defer to upstream for higher versions. |
||
| 313 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
| 314 | * that, this method can be removed and calls to it replaced with |
||
| 315 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
| 316 | * |
||
| 317 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 318 | * @param int $stackPtr The position of the class token. |
||
| 319 | * |
||
| 320 | * @return array|false |
||
| 321 | */ |
||
| 322 | public function findImplementedInterfaceNames(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Checks if a function call has parameters. |
||
| 375 | * |
||
| 376 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 377 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 378 | * |
||
| 379 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, it |
||
| 380 | * will detect whether the array has values or is empty. |
||
| 381 | * |
||
| 382 | * @link https://github.com/wimg/PHPCompatibility/issues/120 |
||
| 383 | * @link https://github.com/wimg/PHPCompatibility/issues/152 |
||
| 384 | * |
||
| 385 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 386 | * @param int $stackPtr The position of the function call token. |
||
| 387 | * |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * Count the number of parameters a function call has been passed. |
||
| 444 | * |
||
| 445 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 446 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 447 | * |
||
| 448 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
||
| 449 | * it will return the number of values in the array. |
||
| 450 | * |
||
| 451 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
| 452 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
| 453 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
| 454 | * |
||
| 455 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 456 | * @param int $stackPtr The position of the function call token. |
||
| 457 | * |
||
| 458 | * @return int |
||
| 459 | */ |
||
| 460 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 468 | |||
| 469 | |||
| 470 | /** |
||
| 471 | * Get information on all parameters passed to a function call. |
||
| 472 | * |
||
| 473 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 474 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 475 | * |
||
| 476 | * Will return an multi-dimentional array with the start token pointer, end token |
||
| 477 | * pointer and raw parameter value for all parameters. Index will be 1-based. |
||
| 478 | * If no parameters are found, will return an empty array. |
||
| 479 | * |
||
| 480 | * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, |
||
| 481 | * it will tokenize the values / key/value pairs contained in the array call. |
||
| 482 | * |
||
| 483 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 484 | * @param int $stackPtr The position of the function call token. |
||
| 485 | * |
||
| 486 | * @return array |
||
| 487 | */ |
||
| 488 | public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 577 | |||
| 578 | |||
| 579 | /** |
||
| 580 | * Get information on a specific parameter passed to a function call. |
||
| 581 | * |
||
| 582 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 583 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 584 | * |
||
| 585 | * Will return a array with the start token pointer, end token pointer and the raw value |
||
| 586 | * of the parameter at a specific offset. |
||
| 587 | * If the specified parameter is not found, will return false. |
||
| 588 | * |
||
| 589 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 590 | * @param int $stackPtr The position of the function call token. |
||
| 591 | * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
||
| 592 | * |
||
| 593 | * @return array|false |
||
| 594 | */ |
||
| 595 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
||
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * Verify whether a token is within a scoped condition. |
||
| 609 | * |
||
| 610 | * If the optional $validScopes parameter has been passed, the function |
||
| 611 | * will check that the token has at least one condition which is of a |
||
| 612 | * type defined in $validScopes. |
||
| 613 | * |
||
| 614 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 615 | * @param int $stackPtr The position of the token. |
||
| 616 | * @param array|int $validScopes Optional. Array of valid scopes |
||
| 617 | * or int value of a valid scope. |
||
| 618 | * Pass the T_.. constant(s) for the |
||
| 619 | * desired scope to this parameter. |
||
| 620 | * |
||
| 621 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
| 622 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
| 623 | * valid scope, false otherwise. |
||
| 624 | */ |
||
| 625 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
| 646 | |||
| 647 | |||
| 648 | /** |
||
| 649 | * Verify whether a token is within a class scope. |
||
| 650 | * |
||
| 651 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 652 | * @param int $stackPtr The position of the token. |
||
| 653 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
| 654 | * scope or also accept interfaces and traits |
||
| 655 | * as scope. |
||
| 656 | * |
||
| 657 | * @return bool True if within class scope, false otherwise. |
||
| 658 | */ |
||
| 659 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
| 673 | |||
| 674 | |||
| 675 | /** |
||
| 676 | * Verify whether a token is within a scoped use statement. |
||
| 677 | * |
||
| 678 | * PHPCS cross-version compatibility method. |
||
| 679 | * |
||
| 680 | * In PHPCS 1.x no conditions are set for a scoped use statement. |
||
| 681 | * This method works around that limitation. |
||
| 682 | * |
||
| 683 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 684 | * @param int $stackPtr The position of the token. |
||
| 685 | * |
||
| 686 | * @return bool True if within use scope, false otherwise. |
||
| 687 | */ |
||
| 688 | public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 720 | |||
| 721 | |||
| 722 | /** |
||
| 723 | * Returns the fully qualified class name for a new class instantiation. |
||
| 724 | * |
||
| 725 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 726 | * |
||
| 727 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 728 | * @param int $stackPtr The position of a T_NEW token. |
||
| 729 | * |
||
| 730 | * @return string |
||
| 731 | */ |
||
| 732 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 773 | |||
| 774 | |||
| 775 | /** |
||
| 776 | * Returns the fully qualified name of the class that the specified class extends. |
||
| 777 | * |
||
| 778 | * Returns an empty string if the class does not extend another class or if |
||
| 779 | * the class name could not be reliably inferred. |
||
| 780 | * |
||
| 781 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 782 | * @param int $stackPtr The position of a T_CLASS token. |
||
| 783 | * |
||
| 784 | * @return string |
||
| 785 | */ |
||
| 786 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 806 | |||
| 807 | |||
| 808 | /** |
||
| 809 | * Returns the class name for the static usage of a class. |
||
| 810 | * This can be a call to a method, the use of a property or constant. |
||
| 811 | * |
||
| 812 | * Returns an empty string if the class name could not be reliably inferred. |
||
| 813 | * |
||
| 814 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 815 | * @param int $stackPtr The position of a T_NEW token. |
||
| 816 | * |
||
| 817 | * @return string |
||
| 818 | */ |
||
| 819 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 870 | |||
| 871 | |||
| 872 | /** |
||
| 873 | * Get the Fully Qualified name for a class/function/constant etc. |
||
| 874 | * |
||
| 875 | * Checks if a class/function/constant name is already fully qualified and |
||
| 876 | * if not, enrich it with the relevant namespace information. |
||
| 877 | * |
||
| 878 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 879 | * @param int $stackPtr The position of the token. |
||
| 880 | * @param string $name The class / function / constant name. |
||
| 881 | * |
||
| 882 | * @return string |
||
| 883 | */ |
||
| 884 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
| 904 | |||
| 905 | |||
| 906 | /** |
||
| 907 | * Is the class/function/constant name namespaced or global ? |
||
| 908 | * |
||
| 909 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
| 910 | * I.e. should always start with a `\`. |
||
| 911 | * |
||
| 912 | * @return bool True if namespaced, false if global. |
||
| 913 | */ |
||
| 914 | public function isNamespaced($FQName) |
||
| 922 | |||
| 923 | |||
| 924 | /** |
||
| 925 | * Determine the namespace name an arbitrary token lives in. |
||
| 926 | * |
||
| 927 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 928 | * @param int $stackPtr The token position for which to determine the namespace. |
||
| 929 | * |
||
| 930 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
| 931 | */ |
||
| 932 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 983 | |||
| 984 | /** |
||
| 985 | * Get the complete namespace name for a namespace declaration. |
||
| 986 | * |
||
| 987 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
| 988 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
| 989 | * |
||
| 990 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 991 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
| 992 | * |
||
| 993 | * @return string|false Namespace name or false if not a namespace declaration. |
||
| 994 | * Namespace name can be an empty string for global namespace declaration. |
||
| 995 | */ |
||
| 996 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1036 | |||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * Get the stack pointer for a return type token for a given function. |
||
| 1040 | * |
||
| 1041 | * Compatible layer for older PHPCS versions which don't recognize |
||
| 1042 | * return type hints correctly. |
||
| 1043 | * |
||
| 1044 | * Expects to be passed T_RETURN_TYPE, T_FUNCTION or T_CLOSURE token. |
||
| 1045 | * |
||
| 1046 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 1047 | * @param int $stackPtr The position of the token. |
||
| 1048 | * |
||
| 1049 | * @return int|false Stack pointer to the return type token or false if |
||
| 1050 | * no return type was found or the passed token was |
||
| 1051 | * not of the correct type. |
||
| 1052 | */ |
||
| 1053 | public function getReturnTypeHintToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1090 | |||
| 1091 | |||
| 1092 | /** |
||
| 1093 | * Check whether a T_VARIABLE token is a class property declaration. |
||
| 1094 | * |
||
| 1095 | * Compatibility layer for PHPCS cross-version compatibility |
||
| 1096 | * as PHPCS 2.4.0 - 2.7.1 does not have good enough support for |
||
| 1097 | * anonymous classes. Along the same lines, the`getMemberProperties()` |
||
| 1098 | * method does not support the `var` prefix. |
||
| 1099 | * |
||
| 1100 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 1101 | * @param int $stackPtr The position in the stack of the |
||
| 1102 | * T_VARIABLE token to verify. |
||
| 1103 | * |
||
| 1104 | * @return bool |
||
| 1105 | */ |
||
| 1106 | public function isClassProperty(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1129 | |||
| 1130 | |||
| 1131 | /** |
||
| 1132 | * Check whether a T_CONST token is a class constant declaration. |
||
| 1133 | * |
||
| 1134 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 1135 | * @param int $stackPtr The position in the stack of the |
||
| 1136 | * T_CONST token to verify. |
||
| 1137 | * |
||
| 1138 | * @return bool |
||
| 1139 | */ |
||
| 1140 | public function isClassConstant(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1160 | |||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Check whether the direct wrapping scope of a token is within a limited set of |
||
| 1164 | * acceptable tokens. |
||
| 1165 | * |
||
| 1166 | * Used to check, for instance, if a T_CONST is a class constant. |
||
| 1167 | * |
||
| 1168 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 1169 | * @param int $stackPtr The position in the stack of the |
||
| 1170 | * T_CONST token to verify. |
||
| 1171 | * @param array $validScopes Array of token types. |
||
| 1172 | * Keys should be the token types in string |
||
| 1173 | * format to allow for newer token types. |
||
| 1174 | * Value is irrelevant. |
||
| 1175 | * |
||
| 1176 | * @return bool |
||
| 1177 | */ |
||
| 1178 | protected function validDirectScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes) |
||
| 1202 | |||
| 1203 | |||
| 1204 | /** |
||
| 1205 | * Get an array of just the type hints from a function declaration. |
||
| 1206 | * |
||
| 1207 | * Expects to be passed T_FUNCTION or T_CLOSURE token. |
||
| 1208 | * |
||
| 1209 | * Strips potential nullable indicator and potential global namespace |
||
| 1210 | * indicator from the type hints before returning them. |
||
| 1211 | * |
||
| 1212 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 1213 | * @param int $stackPtr The position of the token. |
||
| 1214 | * |
||
| 1215 | * @return array Array with type hints or an empty array if |
||
| 1216 | * - the function does not have any parameters |
||
| 1217 | * - no type hints were found |
||
| 1218 | * - or the passed token was not of the correct type. |
||
| 1219 | */ |
||
| 1220 | public function getTypeHintsFromFunctionDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1253 | |||
| 1254 | |||
| 1255 | /** |
||
| 1256 | * Returns the method parameters for the specified function token. |
||
| 1257 | * |
||
| 1258 | * Each parameter is in the following format: |
||
| 1259 | * |
||
| 1260 | * <code> |
||
| 1261 | * 0 => array( |
||
| 1262 | * 'token' => int, // The position of the var in the token stack. |
||
| 1263 | * 'name' => '$var', // The variable name. |
||
| 1264 | * 'content' => string, // The full content of the variable definition. |
||
| 1265 | * 'pass_by_reference' => boolean, // Is the variable passed by reference? |
||
| 1266 | * 'variable_length' => boolean, // Is the param of variable length through use of `...` ? |
||
| 1267 | * 'type_hint' => string, // The type hint for the variable. |
||
| 1268 | * 'nullable_type' => boolean, // Is the variable using a nullable type? |
||
| 1269 | * ) |
||
| 1270 | * </code> |
||
| 1271 | * |
||
| 1272 | * Parameters with default values have an additional array index of |
||
| 1273 | * 'default' with the value of the default as a string. |
||
| 1274 | * |
||
| 1275 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
| 1276 | * class, but with some improvements which have been introduced in |
||
| 1277 | * PHPCS 2.8.0. |
||
| 1278 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1117}, |
||
| 1279 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} and |
||
| 1280 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1293}. |
||
| 1281 | * |
||
| 1282 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
| 1283 | * that, this method can be removed and calls to it replaced with |
||
| 1284 | * `$phpcsFile->getMethodParameters($stackPtr)` calls. |
||
| 1285 | * |
||
| 1286 | * NOTE: This version does not deal with the new T_NULLABLE token type. |
||
| 1287 | * This token is included upstream only in 2.8.0+ and as we defer to upstream |
||
| 1288 | * in that case, no need to deal with it here. |
||
| 1289 | * |
||
| 1290 | * Last synced with PHPCS version: PHPCS 2.9.0-alpha at commit f1511adad043edfd6d2e595e77385c32577eb2bc}} |
||
| 1291 | * |
||
| 1292 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 1293 | * @param int $stackPtr The position in the stack of the |
||
| 1294 | * function token to acquire the |
||
| 1295 | * parameters for. |
||
| 1296 | * |
||
| 1297 | * @return array|false |
||
| 1298 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
| 1299 | * type T_FUNCTION or T_CLOSURE. |
||
| 1300 | */ |
||
| 1301 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1458 | |||
| 1459 | |||
| 1460 | /** |
||
| 1461 | * Returns the name of the class that the specified class extends. |
||
| 1462 | * |
||
| 1463 | * Returns FALSE on error or if there is no extended class name. |
||
| 1464 | * |
||
| 1465 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
| 1466 | * class, but with some improvements which have been introduced in |
||
| 1467 | * PHPCS 2.8.0. |
||
| 1468 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/commit/0011d448119d4c568e3ac1f825ae78815bf2cc34}. |
||
| 1469 | * |
||
| 1470 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
| 1471 | * that, this method can be removed and calls to it replaced with |
||
| 1472 | * `$phpcsFile->findExtendedClassName($stackPtr)` calls. |
||
| 1473 | * |
||
| 1474 | * Last synced with PHPCS version: PHPCS 2.9.0 at commit b940fb7dca8c2a37f0514161b495363e5b36d879}} |
||
| 1475 | * |
||
| 1476 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 1477 | * @param int $stackPtr The position in the stack of the |
||
| 1478 | * class token. |
||
| 1479 | * |
||
| 1480 | * @return string|false |
||
| 1481 | */ |
||
| 1482 | public function findExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1528 | |||
| 1529 | |||
| 1530 | /** |
||
| 1531 | * Get the hash algorithm name from the parameter in a hash function call. |
||
| 1532 | * |
||
| 1533 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
| 1534 | * @param int $stackPtr The position of the T_STRING function token. |
||
| 1535 | * |
||
| 1536 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
| 1537 | * function call or false if it was not. |
||
| 1538 | */ |
||
| 1539 | public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 1572 | |||
| 1573 | }//end class |
||
| 1574 |
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.