Conditions | 16 |
Paths | 31 |
Total Lines | 82 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
176 | * the stack passed in $tokens. |
||
177 | * |
||
178 | * @return void |
||
179 | */ |
||
180 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
181 | { |
||
182 | $tokens = $phpcsFile->getTokens(); |
||
183 | $tokenType = $tokens[$stackPtr]['type']; |
||
184 | |||
185 | // Allow for dealing with multi-token keywords, like "yield from". |
||
186 | $end = $stackPtr; |
||
187 | |||
188 | // Translate T_STRING token if necessary. |
||
189 | if ($tokens[$stackPtr]['type'] === 'T_STRING') { |
||
190 | $content = $tokens[$stackPtr]['content']; |
||
191 | if (isset($this->translateContentToToken[$content]) === false) { |
||
192 | // Not one of the tokens we're looking for. |
||
193 | return; |
||
194 | } |
||
195 | |||
196 | $tokenType = $this->translateContentToToken[$content]; |
||
197 | } |
||
198 | |||
199 | /* |
||
200 | * Special case: distinguish between `yield` and `yield from`. |
||
201 | * |
||
202 | * PHPCS currently (at least up to v 3.0.1) does not backfill for the |
||
203 | * `yield` nor the `yield from` keywords. |
||
204 | * See: https://github.com/squizlabs/PHP_CodeSniffer/issues/1524 |
||
205 | * |
||
206 | * In PHP < 5.5, both `yield` as well as `from` are tokenized as T_STRING. |
||
207 | * In PHP 5.5 - 5.6, `yield` is tokenized as T_YIELD and `from` as T_STRING, |
||
208 | * but the `T_YIELD_FROM` token *is* defined in PHP. |
||
209 | * In PHP 7.0+ both are tokenized as their respective token, however, |
||
210 | * a multi-line "yield from" is tokenized as two tokens. |
||
211 | */ |
||
212 | if ($tokenType === 'T_YIELD') { |
||
213 | $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true); |
||
214 | if ($tokens[$nextToken]['code'] === T_STRING |
||
215 | && $tokens[$nextToken]['content'] === 'from' |
||
216 | ) { |
||
217 | $tokenType = 'T_YIELD_FROM'; |
||
218 | $end = $nextToken; |
||
219 | } |
||
220 | unset($nextToken); |
||
221 | } |
||
222 | |||
223 | if ($tokenType === 'T_YIELD_FROM' && $tokens[($stackPtr - 1)]['type'] === 'T_YIELD_FROM') { |
||
224 | // Multi-line "yield from", no need to report it twice. |
||
225 | return; |
||
226 | } |
||
227 | |||
228 | if (isset($this->newKeywords[$tokenType]) === false) { |
||
229 | return; |
||
230 | } |
||
231 | |||
232 | $nextToken = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($end + 1), null, true); |
||
233 | $prevToken = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
||
234 | |||
235 | |||
236 | // Skip attempts to use keywords as functions or class names - the former |
||
237 | // will be reported by ForbiddenNamesAsInvokedFunctionsSniff, whilst the |
||
238 | // latter will be (partially) reported by the ForbiddenNames sniff. |
||
239 | // Either type will result in false-positives when targetting lower versions |
||
240 | // of PHP where the name was not reserved, unless we explicitly check for |
||
241 | // them. |
||
242 | if (($nextToken === false |
||
243 | || $tokens[$nextToken]['type'] !== 'T_OPEN_PARENTHESIS') |
||
244 | && ($prevToken === false |
||
245 | || $tokens[$prevToken]['type'] !== 'T_CLASS' |
||
246 | || $tokens[$prevToken]['type'] !== 'T_INTERFACE') |
||
247 | ) { |
||
248 | // Skip based on token scope condition. |
||
249 | if (isset($this->newKeywords[$tokenType]['condition']) |
||
250 | && call_user_func(array($this, $this->newKeywords[$tokenType]['condition']), $phpcsFile, $stackPtr) === true |
||
251 | ) { |
||
252 | return; |
||
253 | } |
||
254 | |||
255 | $itemInfo = array( |
||
256 | 'name' => $tokenType, |
||
257 | ); |
||
258 | $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
||
327 |