Conditions | 34 |
Paths | 35 |
Total Lines | 132 |
Code Lines | 60 |
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 |
||
193 | public function processNonString(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
||
194 | { |
||
195 | $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
196 | if ($nextNonEmpty === false) { |
||
197 | return; |
||
198 | } |
||
199 | |||
200 | /* |
||
201 | * Deal with anonymous classes - `class` before a reserved keyword is sometimes |
||
202 | * misidentified as `T_ANON_CLASS`. |
||
203 | * In PHPCS < 2.3.4 these were tokenized as T_CLASS no matter what. |
||
204 | */ |
||
205 | if ($tokens[$stackPtr]['type'] === 'T_ANON_CLASS' || $tokens[$stackPtr]['type'] === 'T_CLASS') { |
||
206 | $prevNonEmpty = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
||
207 | if ($prevNonEmpty !== false && $tokens[$prevNonEmpty]['type'] === 'T_NEW') { |
||
208 | return; |
||
209 | } |
||
210 | } |
||
211 | |||
212 | /* |
||
213 | * PHP 5.6 allows for use const and use function, but only if followed by the function/constant name. |
||
214 | * - `use function HelloWorld` => move to the next token (HelloWorld) to verify. |
||
215 | * - `use const HelloWorld` => move to the next token (HelloWorld) to verify. |
||
216 | */ |
||
217 | elseif ($tokens[$stackPtr]['type'] === 'T_USE' |
||
218 | && isset($this->validUseNames[strtolower($tokens[$nextNonEmpty]['content'])]) === true |
||
219 | ) { |
||
220 | $maybeUseNext = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true, null, true); |
||
221 | if ($maybeUseNext !== false && $this->isEndOfUseStatement($tokens[$maybeUseNext]) === false) { |
||
222 | // Prevent duplicate messages: `const` is T_CONST in PHPCS 1.x and T_STRING in PHPCS 2.x. |
||
223 | if ($this->isLowPHPCS === true) { |
||
224 | return; |
||
225 | } |
||
226 | $nextNonEmpty = $maybeUseNext; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /* |
||
231 | * Deal with visibility modifiers. |
||
232 | * - `use HelloWorld { sayHello as protected; }` => valid, bow out. |
||
233 | * - `use HelloWorld { sayHello as private myPrivateHello; }` => move to the next token to verify. |
||
234 | */ |
||
235 | elseif ($tokens[$stackPtr]['type'] === 'T_AS' |
||
236 | && isset($this->allowed_modifiers[$tokens[$nextNonEmpty]['code']]) === true |
||
237 | && $this->inUseScope($phpcsFile, $stackPtr) === true |
||
238 | ) { |
||
239 | $maybeUseNext = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true, null, true); |
||
240 | if ($maybeUseNext === false || $this->isEndOfUseStatement($tokens[$maybeUseNext]) === true) { |
||
241 | return; |
||
242 | } |
||
243 | |||
244 | $nextNonEmpty = $maybeUseNext; |
||
245 | } |
||
246 | |||
247 | /* |
||
248 | * Deal with functions declared to return by reference. |
||
249 | */ |
||
250 | elseif ($tokens[$stackPtr]['type'] === 'T_FUNCTION' |
||
251 | && $tokens[$nextNonEmpty]['type'] === 'T_BITWISE_AND' |
||
252 | ) { |
||
253 | $maybeUseNext = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true, null, true); |
||
254 | if ($maybeUseNext === false) { |
||
255 | // Live coding. |
||
256 | return; |
||
257 | } |
||
258 | |||
259 | $nextNonEmpty = $maybeUseNext; |
||
260 | } |
||
261 | |||
262 | /* |
||
263 | * Deal with nested namespaces. |
||
264 | */ |
||
265 | elseif ($tokens[$stackPtr]['type'] === 'T_NAMESPACE') { |
||
266 | if ($tokens[$stackPtr + 1]['code'] === T_NS_SEPARATOR) { |
||
267 | // Not a namespace declaration, but use of, i.e. namespace\someFunction(); |
||
268 | return; |
||
269 | } |
||
270 | |||
271 | $endToken = $phpcsFile->findNext(array(T_SEMICOLON, T_OPEN_CURLY_BRACKET), ($stackPtr + 1), null, false, null, true); |
||
272 | $namespaceName = trim($phpcsFile->getTokensAsString(($stackPtr + 1), ($endToken - $stackPtr - 1))); |
||
273 | if (empty($namespaceName) === true) { |
||
274 | return; |
||
275 | } |
||
276 | |||
277 | $namespaceParts = explode('\\', $namespaceName); |
||
278 | foreach ($namespaceParts as $namespacePart) { |
||
279 | $partLc = strtolower($namespacePart); |
||
280 | if (isset($this->invalidNames[$partLc]) === false) { |
||
281 | continue; |
||
282 | } |
||
283 | |||
284 | // Find the token position of the part which matched. |
||
285 | for ($i = ($stackPtr + 1); $i < $endToken; $i++) { |
||
286 | if ($tokens[$i]['content'] === $namespacePart) { |
||
287 | $nextNonEmpty = $i; |
||
288 | break; |
||
289 | } |
||
290 | } |
||
291 | } |
||
292 | unset($i, $namespacePart, $partLc); |
||
293 | } |
||
294 | |||
295 | |||
296 | $nextContentLc = strtolower($tokens[$nextNonEmpty]['content']); |
||
297 | if (isset($this->invalidNames[$nextContentLc]) === false) { |
||
298 | return; |
||
299 | } |
||
300 | |||
301 | /* |
||
302 | * Deal with PHP 7 relaxing the rules. |
||
303 | * "As of PHP 7.0.0 these keywords are allowed as property, constant, and method names |
||
304 | * of classes, interfaces and traits, except that class may not be used as constant name." |
||
305 | */ |
||
306 | if ((($tokens[$stackPtr]['type'] === 'T_FUNCTION' |
||
307 | && $this->inClassScope($phpcsFile, $stackPtr, false) === true) |
||
308 | || ($tokens[$stackPtr]['type'] === 'T_CONST' |
||
309 | && $this->isClassConstant($phpcsFile, $stackPtr) === true |
||
310 | && $nextContentLc !== 'class') |
||
311 | ) && $this->supportsBelow('5.6') === false |
||
312 | ) { |
||
313 | return; |
||
314 | } |
||
315 | |||
316 | if ($this->supportsAbove($this->invalidNames[$nextContentLc])) { |
||
317 | $data = array( |
||
318 | $tokens[$nextNonEmpty]['content'], |
||
319 | $this->invalidNames[$nextContentLc], |
||
320 | ); |
||
321 | $this->addError($phpcsFile, $stackPtr, $tokens[$nextNonEmpty]['content'], $data); |
||
322 | } |
||
323 | |||
324 | }//end processNonString() |
||
325 | |||
410 |