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