annotate vendor/squizlabs/php_codesniffer/src/Sniffs/AbstractVariableSniff.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@4 1 <?php
Chris@4 2 /**
Chris@4 3 * A class to find T_VARIABLE tokens.
Chris@4 4 *
Chris@4 5 * This class can distinguish between normal T_VARIABLE tokens, and those tokens
Chris@4 6 * that represent class members. If a class member is encountered, then the
Chris@4 7 * processMemberVar method is called so the extending class can process it. If
Chris@4 8 * the token is found to be a normal T_VARIABLE token, then processVariable is
Chris@4 9 * called.
Chris@4 10 *
Chris@4 11 * @author Greg Sherwood <gsherwood@squiz.net>
Chris@4 12 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
Chris@4 13 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
Chris@4 14 */
Chris@4 15
Chris@4 16 namespace PHP_CodeSniffer\Sniffs;
Chris@4 17
Chris@4 18 use PHP_CodeSniffer\Files\File;
Chris@4 19 use PHP_CodeSniffer\Util\Tokens;
Chris@4 20
Chris@4 21 abstract class AbstractVariableSniff extends AbstractScopeSniff
Chris@4 22 {
Chris@4 23
Chris@4 24
Chris@4 25 /**
Chris@4 26 * List of PHP Reserved variables.
Chris@4 27 *
Chris@4 28 * Used by various naming convention sniffs.
Chris@4 29 *
Chris@4 30 * @var array
Chris@4 31 */
Chris@4 32 protected $phpReservedVars = [
Chris@4 33 '_SERVER' => true,
Chris@4 34 '_GET' => true,
Chris@4 35 '_POST' => true,
Chris@4 36 '_REQUEST' => true,
Chris@4 37 '_SESSION' => true,
Chris@4 38 '_ENV' => true,
Chris@4 39 '_COOKIE' => true,
Chris@4 40 '_FILES' => true,
Chris@4 41 'GLOBALS' => true,
Chris@4 42 'http_response_header' => true,
Chris@4 43 'HTTP_RAW_POST_DATA' => true,
Chris@4 44 'php_errormsg' => true,
Chris@4 45 ];
Chris@4 46
Chris@4 47
Chris@4 48 /**
Chris@4 49 * Constructs an AbstractVariableTest.
Chris@4 50 */
Chris@4 51 public function __construct()
Chris@4 52 {
Chris@4 53 $scopes = Tokens::$ooScopeTokens;
Chris@4 54
Chris@4 55 $listen = [
Chris@4 56 T_VARIABLE,
Chris@4 57 T_DOUBLE_QUOTED_STRING,
Chris@4 58 T_HEREDOC,
Chris@4 59 ];
Chris@4 60
Chris@4 61 parent::__construct($scopes, $listen, true);
Chris@4 62
Chris@4 63 }//end __construct()
Chris@4 64
Chris@4 65
Chris@4 66 /**
Chris@5 67 * Processes the token in the specified PHP_CodeSniffer\Files\File.
Chris@4 68 *
Chris@4 69 * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
Chris@4 70 * token was found.
Chris@4 71 * @param int $stackPtr The position where the token was found.
Chris@4 72 * @param int $currScope The current scope opener token.
Chris@4 73 *
Chris@4 74 * @return void|int Optionally returns a stack pointer. The sniff will not be
Chris@4 75 * called again on the current file until the returned stack
Chris@4 76 * pointer is reached. Return ($phpcsFile->numTokens + 1) to skip
Chris@4 77 * the rest of the file.
Chris@4 78 */
Chris@4 79 final protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
Chris@4 80 {
Chris@4 81 $tokens = $phpcsFile->getTokens();
Chris@4 82
Chris@4 83 if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING
Chris@4 84 || $tokens[$stackPtr]['code'] === T_HEREDOC
Chris@4 85 ) {
Chris@4 86 // Check to see if this string has a variable in it.
Chris@4 87 $pattern = '|(?<!\\\\)(?:\\\\{2})*\${?[a-zA-Z0-9_]+}?|';
Chris@4 88 if (preg_match($pattern, $tokens[$stackPtr]['content']) !== 0) {
Chris@4 89 return $this->processVariableInString($phpcsFile, $stackPtr);
Chris@4 90 }
Chris@4 91
Chris@4 92 return;
Chris@4 93 }
Chris@4 94
Chris@5 95 // If this token is nested inside a function at a deeper
Chris@4 96 // level than the current OO scope that was found, it's a normal
Chris@4 97 // variable and not a member var.
Chris@4 98 $conditions = array_reverse($tokens[$stackPtr]['conditions'], true);
Chris@4 99 $inFunction = false;
Chris@4 100 foreach ($conditions as $scope => $code) {
Chris@4 101 if (isset(Tokens::$ooScopeTokens[$code]) === true) {
Chris@4 102 break;
Chris@4 103 }
Chris@4 104
Chris@4 105 if ($code === T_FUNCTION || $code === T_CLOSURE) {
Chris@4 106 $inFunction = true;
Chris@4 107 }
Chris@4 108 }
Chris@4 109
Chris@4 110 if ($scope !== $currScope) {
Chris@4 111 // We found a closer scope to this token, so ignore
Chris@4 112 // this particular time through the sniff. We will process
Chris@4 113 // this token when this closer scope is found to avoid
Chris@4 114 // duplicate checks.
Chris@4 115 return;
Chris@4 116 }
Chris@4 117
Chris@4 118 // Just make sure this isn't a variable in a function declaration.
Chris@4 119 if ($inFunction === false && isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
Chris@4 120 foreach ($tokens[$stackPtr]['nested_parenthesis'] as $opener => $closer) {
Chris@4 121 if (isset($tokens[$opener]['parenthesis_owner']) === false) {
Chris@5 122 // Check if this is a USE statement for a closure.
Chris@4 123 $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($opener - 1), null, true);
Chris@4 124 if ($tokens[$prev]['code'] === T_USE) {
Chris@4 125 $inFunction = true;
Chris@4 126 break;
Chris@4 127 }
Chris@4 128
Chris@4 129 continue;
Chris@4 130 }
Chris@4 131
Chris@4 132 $owner = $tokens[$opener]['parenthesis_owner'];
Chris@4 133 if ($tokens[$owner]['code'] === T_FUNCTION
Chris@4 134 || $tokens[$owner]['code'] === T_CLOSURE
Chris@4 135 ) {
Chris@4 136 $inFunction = true;
Chris@4 137 break;
Chris@4 138 }
Chris@4 139 }
Chris@4 140 }//end if
Chris@4 141
Chris@4 142 if ($inFunction === true) {
Chris@4 143 return $this->processVariable($phpcsFile, $stackPtr);
Chris@4 144 } else {
Chris@4 145 return $this->processMemberVar($phpcsFile, $stackPtr);
Chris@4 146 }
Chris@4 147
Chris@4 148 }//end processTokenWithinScope()
Chris@4 149
Chris@4 150
Chris@4 151 /**
Chris@4 152 * Processes the token outside the scope in the file.
Chris@4 153 *
Chris@4 154 * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
Chris@4 155 * token was found.
Chris@4 156 * @param int $stackPtr The position where the token was found.
Chris@4 157 *
Chris@4 158 * @return void|int Optionally returns a stack pointer. The sniff will not be
Chris@4 159 * called again on the current file until the returned stack
Chris@4 160 * pointer is reached. Return ($phpcsFile->numTokens + 1) to skip
Chris@4 161 * the rest of the file.
Chris@4 162 */
Chris@4 163 final protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
Chris@4 164 {
Chris@4 165 $tokens = $phpcsFile->getTokens();
Chris@4 166 // These variables are not member vars.
Chris@4 167 if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
Chris@4 168 return $this->processVariable($phpcsFile, $stackPtr);
Chris@4 169 } else if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING
Chris@4 170 || $tokens[$stackPtr]['code'] === T_HEREDOC
Chris@4 171 ) {
Chris@4 172 // Check to see if this string has a variable in it.
Chris@4 173 $pattern = '|(?<!\\\\)(?:\\\\{2})*\${?[a-zA-Z0-9_]+}?|';
Chris@4 174 if (preg_match($pattern, $tokens[$stackPtr]['content']) !== 0) {
Chris@4 175 return $this->processVariableInString($phpcsFile, $stackPtr);
Chris@4 176 }
Chris@4 177 }
Chris@4 178
Chris@4 179 }//end processTokenOutsideScope()
Chris@4 180
Chris@4 181
Chris@4 182 /**
Chris@4 183 * Called to process class member vars.
Chris@4 184 *
Chris@4 185 * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
Chris@4 186 * token was found.
Chris@4 187 * @param int $stackPtr The position where the token was found.
Chris@4 188 *
Chris@4 189 * @return void|int Optionally returns a stack pointer. The sniff will not be
Chris@4 190 * called again on the current file until the returned stack
Chris@4 191 * pointer is reached. Return ($phpcsFile->numTokens + 1) to skip
Chris@4 192 * the rest of the file.
Chris@4 193 */
Chris@4 194 abstract protected function processMemberVar(File $phpcsFile, $stackPtr);
Chris@4 195
Chris@4 196
Chris@4 197 /**
Chris@4 198 * Called to process normal member vars.
Chris@4 199 *
Chris@4 200 * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
Chris@4 201 * token was found.
Chris@4 202 * @param int $stackPtr The position where the token was found.
Chris@4 203 *
Chris@4 204 * @return void|int Optionally returns a stack pointer. The sniff will not be
Chris@4 205 * called again on the current file until the returned stack
Chris@4 206 * pointer is reached. Return ($phpcsFile->numTokens + 1) to skip
Chris@4 207 * the rest of the file.
Chris@4 208 */
Chris@4 209 abstract protected function processVariable(File $phpcsFile, $stackPtr);
Chris@4 210
Chris@4 211
Chris@4 212 /**
Chris@4 213 * Called to process variables found in double quoted strings or heredocs.
Chris@4 214 *
Chris@4 215 * Note that there may be more than one variable in the string, which will
Chris@4 216 * result only in one call for the string or one call per line for heredocs.
Chris@4 217 *
Chris@4 218 * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
Chris@4 219 * token was found.
Chris@4 220 * @param int $stackPtr The position where the double quoted
Chris@4 221 * string was found.
Chris@4 222 *
Chris@4 223 * @return void|int Optionally returns a stack pointer. The sniff will not be
Chris@4 224 * called again on the current file until the returned stack
Chris@4 225 * pointer is reached. Return ($phpcsFile->numTokens + 1) to skip
Chris@4 226 * the rest of the file.
Chris@4 227 */
Chris@4 228 abstract protected function processVariableInString(File $phpcsFile, $stackPtr);
Chris@4 229
Chris@4 230
Chris@4 231 }//end class