Conditions | 93 |
Paths | 511 |
Total Lines | 331 |
Code Lines | 188 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
309 | private function fixIndents(Tokens $tokens): void |
||
310 | { |
||
311 | $classyTokens = Token::getClassyTokenKinds(); |
||
312 | $classyAndFunctionTokens = array_merge([T_FUNCTION], $classyTokens); |
||
313 | $controlTokens = $this->getControlTokens(); |
||
314 | $indentTokens = array_filter( |
||
315 | array_merge($classyAndFunctionTokens, $controlTokens), |
||
316 | static function (int $item) { |
||
317 | return T_SWITCH !== $item; |
||
318 | } |
||
319 | ); |
||
320 | $tokensAnalyzer = new TokensAnalyzer($tokens); |
||
321 | |||
322 | for ($index = 0, $limit = \count($tokens); $index < $limit; ++$index) { |
||
323 | $token = $tokens[$index]; |
||
324 | |||
325 | // if token is not a structure element - continue |
||
326 | if (!$token->isGivenKind($indentTokens)) { |
||
327 | continue; |
||
328 | } |
||
329 | |||
330 | // do not change indent for `while` in `do ... while ...` |
||
331 | if ( |
||
332 | $token->isGivenKind(T_WHILE) |
||
333 | && $tokensAnalyzer->isWhilePartOfDoWhile($index) |
||
334 | ) { |
||
335 | continue; |
||
336 | } |
||
337 | |||
338 | if ( |
||
339 | $this->configuration['allow_single_line_anonymous_class_with_empty_body'] |
||
340 | && $token->isGivenKind(T_CLASS) |
||
341 | ) { |
||
342 | $prevIndex = $tokens->getPrevMeaningfulToken($index); |
||
343 | if ($tokens[$prevIndex]->isGivenKind(T_NEW)) { |
||
344 | $braceStartIndex = $tokens->getNextTokenOfKind($index, ['{']); |
||
345 | $braceEndIndex = $tokens->getNextMeaningfulToken($braceStartIndex); |
||
346 | |||
347 | if ('}' === $tokens[$braceEndIndex]->getContent() && !$this->isMultilined($tokens, $index, $braceEndIndex)) { |
||
348 | $index = $braceEndIndex; |
||
349 | |||
350 | continue; |
||
351 | } |
||
352 | } |
||
353 | } |
||
354 | |||
355 | if ( |
||
356 | $this->configuration['allow_single_line_closure'] |
||
357 | && $token->isGivenKind(T_FUNCTION) |
||
358 | && $tokensAnalyzer->isLambda($index) |
||
359 | ) { |
||
360 | $braceEndIndex = $tokens->findBlockEnd( |
||
361 | Tokens::BLOCK_TYPE_CURLY_BRACE, |
||
362 | $tokens->getNextTokenOfKind($index, ['{']) |
||
363 | ); |
||
364 | |||
365 | if (!$this->isMultilined($tokens, $index, $braceEndIndex)) { |
||
366 | $index = $braceEndIndex; |
||
367 | |||
368 | continue; |
||
369 | } |
||
370 | } |
||
371 | |||
372 | if ($token->isGivenKind($classyAndFunctionTokens)) { |
||
373 | $startBraceIndex = $tokens->getNextTokenOfKind($index, [';', '{']); |
||
374 | $startBraceToken = $tokens[$startBraceIndex]; |
||
375 | } else { |
||
376 | $parenthesisEndIndex = $this->findParenthesisEnd($tokens, $index); |
||
377 | $startBraceIndex = $tokens->getNextNonWhitespace($parenthesisEndIndex); |
||
378 | $startBraceToken = $tokens[$startBraceIndex]; |
||
379 | } |
||
380 | |||
381 | // structure without braces block - nothing to do, e.g. do { } while (true); |
||
382 | if (!$startBraceToken->equals('{')) { |
||
383 | continue; |
||
384 | } |
||
385 | |||
386 | $nextNonWhitespaceIndex = $tokens->getNextNonWhitespace($startBraceIndex, " \t"); |
||
387 | $nextNonWhitespace = $tokens[$nextNonWhitespaceIndex]; |
||
388 | |||
389 | /* if CLOSE_TAG is after { on the same line, do not indent. e.g. <?php if ($condition) { ?> */ |
||
390 | if ($nextNonWhitespace->isGivenKind(T_CLOSE_TAG)) { |
||
391 | continue; |
||
392 | } |
||
393 | |||
394 | /* if CLOSE_TAG is after { on the next line and a comment on this line, do not indent. e.g. <?php if ($condition) { // \n?> */ |
||
395 | if ($nextNonWhitespace->isComment() && $tokens[$tokens->getNextMeaningfulToken($nextNonWhitespaceIndex)]->isGivenKind(T_CLOSE_TAG)) { |
||
396 | continue; |
||
397 | } |
||
398 | |||
399 | $endBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startBraceIndex); |
||
400 | |||
401 | $indent = WhitespacesAnalyzer::detectIndent($tokens, $index); |
||
402 | |||
403 | // fix indent near closing brace |
||
404 | $tokens->ensureWhitespaceAtIndex($endBraceIndex - 1, 1, $this->whitespacesConfig->getLineEnding().$indent); |
||
405 | |||
406 | // fix indent between braces |
||
407 | $lastCommaIndex = $tokens->getPrevTokenOfKind($endBraceIndex - 1, [';', '}']); |
||
408 | |||
409 | $nestLevel = 1; |
||
410 | for ($nestIndex = $lastCommaIndex; $nestIndex >= $startBraceIndex; --$nestIndex) { |
||
411 | $nestToken = $tokens[$nestIndex]; |
||
412 | |||
413 | if ($nestToken->equalsAny([')', [CT::T_BRACE_CLASS_INSTANTIATION_CLOSE]])) { |
||
414 | $nestIndex = $tokens->findBlockStart( |
||
415 | $nestToken->equals(')') ? Tokens::BLOCK_TYPE_PARENTHESIS_BRACE : Tokens::BLOCK_TYPE_BRACE_CLASS_INSTANTIATION, |
||
416 | $nestIndex |
||
417 | ); |
||
418 | |||
419 | continue; |
||
420 | } |
||
421 | |||
422 | if (1 === $nestLevel) { |
||
423 | // Next token is the beginning of a line that can be indented when |
||
424 | // the current token is a `;`, a `}` or the opening `{` of current |
||
425 | // scope. Current token may also be a comment that follows `;` or |
||
426 | // `}`, in which case indentation will only be fixed if this |
||
427 | // comment is followed by a newline. |
||
428 | $nextLineCanBeIndented = false; |
||
429 | if ($nestToken->equalsAny([';', '}'])) { |
||
430 | $nextLineCanBeIndented = true; |
||
431 | } elseif ($this->isCommentWithFixableIndentation($tokens, $nestIndex)) { |
||
432 | for ($i = $nestIndex; $i > $startBraceIndex; --$i) { |
||
433 | if ($tokens[$i]->equalsAny([';', '}'])) { |
||
434 | $nextLineCanBeIndented = true; |
||
435 | |||
436 | break; |
||
437 | } |
||
438 | |||
439 | if (!$tokens[$i]->isWhitespace() && !$tokens[$i]->isComment()) { |
||
440 | break; |
||
441 | } |
||
442 | } |
||
443 | |||
444 | if ($nextLineCanBeIndented || $i === $startBraceIndex) { |
||
445 | $nextToken = $tokens[$nestIndex + 1]; |
||
446 | $nextLineCanBeIndented = $nextToken->isWhitespace() && 1 === Preg::match('/\R/', $nextToken->getContent()); |
||
447 | } |
||
448 | } |
||
449 | |||
450 | if (!$nextLineCanBeIndented) { |
||
451 | continue; |
||
452 | } |
||
453 | |||
454 | $nextNonWhitespaceNestIndex = $tokens->getNextNonWhitespace($nestIndex); |
||
455 | $nextNonWhitespaceNestToken = $tokens[$nextNonWhitespaceNestIndex]; |
||
456 | |||
457 | if ( |
||
458 | // next Token is not a comment on its own line |
||
459 | !($nextNonWhitespaceNestToken->isComment() && ( |
||
460 | !$tokens[$nextNonWhitespaceNestIndex - 1]->isWhitespace() |
||
461 | || !Preg::match('/\R/', $tokens[$nextNonWhitespaceNestIndex - 1]->getContent()) |
||
462 | )) |
||
463 | // and it is not a `$foo = function () {};` situation |
||
464 | && !($nestToken->equals('}') && $nextNonWhitespaceNestToken->equalsAny([';', ',', ']', [CT::T_ARRAY_SQUARE_BRACE_CLOSE]])) |
||
465 | // and it is not a `Foo::{bar}()` situation |
||
466 | && !($nestToken->equals('}') && $nextNonWhitespaceNestToken->equals('(')) |
||
467 | // and it is not a `${"a"}->...` and `${"b{$foo}"}->...` situation |
||
468 | && !($nestToken->equals('}') && $tokens[$nestIndex - 1]->equalsAny(['"', "'", [T_CONSTANT_ENCAPSED_STRING], [T_VARIABLE]])) |
||
469 | // and next token is not a closing tag that would break heredoc/nowdoc syntax |
||
470 | && !($tokens[$nestIndex - 1]->isGivenKind(T_END_HEREDOC) && $nextNonWhitespaceNestToken->isGivenKind(T_CLOSE_TAG)) |
||
471 | ) { |
||
472 | if ( |
||
473 | ( |
||
474 | self::LINE_NEXT !== $this->configuration['position_after_control_structures'] |
||
475 | && $nextNonWhitespaceNestToken->isGivenKind($this->getControlContinuationTokens()) |
||
476 | && !$tokens[$tokens->getPrevNonWhitespace($nextNonWhitespaceNestIndex)]->isComment() |
||
477 | ) |
||
478 | || $nextNonWhitespaceNestToken->isGivenKind(T_CLOSE_TAG) |
||
479 | || ( |
||
480 | self::LINE_NEXT !== $this->configuration['position_after_control_structures'] |
||
481 | && $nextNonWhitespaceNestToken->isGivenKind(T_WHILE) |
||
482 | && $tokensAnalyzer->isWhilePartOfDoWhile($nextNonWhitespaceNestIndex) |
||
483 | ) |
||
484 | ) { |
||
485 | $whitespace = ' '; |
||
486 | } else { |
||
487 | $nextToken = $tokens[$nestIndex + 1]; |
||
488 | $nextWhitespace = ''; |
||
489 | |||
490 | if ($nextToken->isWhitespace()) { |
||
491 | $nextWhitespace = rtrim($nextToken->getContent(), " \t"); |
||
492 | |||
493 | if ('' !== $nextWhitespace) { |
||
494 | $nextWhitespace = Preg::replace( |
||
495 | sprintf('/%s$/', $this->whitespacesConfig->getLineEnding()), |
||
496 | '', |
||
497 | $nextWhitespace, |
||
498 | 1 |
||
499 | ); |
||
500 | } |
||
501 | } |
||
502 | |||
503 | $whitespace = $nextWhitespace.$this->whitespacesConfig->getLineEnding().$indent; |
||
504 | |||
505 | if (!$nextNonWhitespaceNestToken->equals('}')) { |
||
506 | $determineIsIndentableBlockContent = static function (int $contentIndex) use ($tokens): bool { |
||
507 | if (!$tokens[$contentIndex]->isComment()) { |
||
508 | return true; |
||
509 | } |
||
510 | |||
511 | if (!$tokens[$tokens->getPrevMeaningfulToken($contentIndex)]->equals(';')) { |
||
512 | return true; |
||
513 | } |
||
514 | |||
515 | $nextIndex = $tokens->getNextMeaningfulToken($contentIndex); |
||
516 | |||
517 | if (!$tokens[$nextIndex]->equals('}')) { |
||
518 | return true; |
||
519 | } |
||
520 | |||
521 | $nextNextIndex = $tokens->getNextMeaningfulToken($nextIndex); |
||
522 | |||
523 | if (null === $nextNextIndex) { |
||
524 | return true; |
||
525 | } |
||
526 | |||
527 | if ($tokens[$nextNextIndex]->equalsAny([ |
||
528 | [T_ELSE], |
||
529 | [T_ELSEIF], |
||
530 | ',', |
||
531 | ])) { |
||
532 | return false; |
||
533 | } |
||
534 | |||
535 | return true; |
||
536 | }; |
||
537 | |||
538 | // add extra indent only if current content is not a comment for content outside of current block |
||
539 | if ($determineIsIndentableBlockContent($nestIndex + 2)) { |
||
540 | $whitespace .= $this->whitespacesConfig->getIndent(); |
||
541 | } |
||
542 | } |
||
543 | } |
||
544 | |||
545 | $this->ensureWhitespaceAtIndexAndIndentMultilineComment($tokens, $nestIndex + 1, $whitespace); |
||
546 | } |
||
547 | } |
||
548 | |||
549 | if ($nestToken->equals('}')) { |
||
550 | ++$nestLevel; |
||
551 | |||
552 | continue; |
||
553 | } |
||
554 | |||
555 | if ($nestToken->equals('{')) { |
||
556 | --$nestLevel; |
||
557 | |||
558 | continue; |
||
559 | } |
||
560 | } |
||
561 | |||
562 | // fix indent near opening brace |
||
563 | if (isset($tokens[$startBraceIndex + 2]) && $tokens[$startBraceIndex + 2]->equals('}')) { |
||
564 | $tokens->ensureWhitespaceAtIndex($startBraceIndex + 1, 0, $this->whitespacesConfig->getLineEnding().$indent); |
||
565 | } else { |
||
566 | $nextToken = $tokens[$startBraceIndex + 1]; |
||
567 | $nextNonWhitespaceToken = $tokens[$tokens->getNextNonWhitespace($startBraceIndex)]; |
||
568 | |||
569 | // set indent only if it is not a case, when comment is following { on same line |
||
570 | if ( |
||
571 | !$nextNonWhitespaceToken->isComment() |
||
572 | || ($nextToken->isWhitespace() && 1 === substr_count($nextToken->getContent(), "\n")) // preserve blank lines |
||
573 | ) { |
||
574 | $this->ensureWhitespaceAtIndexAndIndentMultilineComment( |
||
575 | $tokens, |
||
576 | $startBraceIndex + 1, |
||
577 | $this->whitespacesConfig->getLineEnding().$indent.$this->whitespacesConfig->getIndent() |
||
578 | ); |
||
579 | } |
||
580 | } |
||
581 | |||
582 | if ($token->isGivenKind($classyTokens) && !$tokensAnalyzer->isAnonymousClass($index)) { |
||
583 | if (self::LINE_SAME === $this->configuration['position_after_functions_and_oop_constructs'] && !$tokens[$tokens->getPrevNonWhitespace($startBraceIndex)]->isComment()) { |
||
584 | $ensuredWhitespace = ' '; |
||
585 | } else { |
||
586 | $ensuredWhitespace = $this->whitespacesConfig->getLineEnding().$indent; |
||
587 | } |
||
588 | |||
589 | $tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, $ensuredWhitespace); |
||
590 | } elseif ( |
||
591 | $token->isGivenKind(T_FUNCTION) && !$tokensAnalyzer->isLambda($index) |
||
592 | || ( |
||
593 | self::LINE_NEXT === $this->configuration['position_after_control_structures'] && $token->isGivenKind($controlTokens) |
||
594 | || ( |
||
595 | self::LINE_NEXT === $this->configuration['position_after_anonymous_constructs'] |
||
596 | && ( |
||
597 | $token->isGivenKind(T_FUNCTION) && $tokensAnalyzer->isLambda($index) |
||
598 | || $token->isGivenKind(T_CLASS) && $tokensAnalyzer->isAnonymousClass($index) |
||
599 | ) |
||
600 | ) |
||
601 | ) |
||
602 | ) { |
||
603 | $isAnonymousClass = $token->isGivenKind($classyTokens) && $tokensAnalyzer->isAnonymousClass($index); |
||
604 | |||
605 | $closingParenthesisIndex = $tokens->getPrevTokenOfKind($startBraceIndex, [')']); |
||
606 | if (null === $closingParenthesisIndex && !$isAnonymousClass) { |
||
607 | continue; |
||
608 | } |
||
609 | |||
610 | if ( |
||
611 | !$isAnonymousClass |
||
612 | && $tokens[$closingParenthesisIndex - 1]->isWhitespace() |
||
613 | && false !== strpos($tokens[$closingParenthesisIndex - 1]->getContent(), "\n") |
||
614 | ) { |
||
615 | if (!$tokens[$startBraceIndex - 2]->isComment()) { |
||
616 | $tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, ' '); |
||
617 | } |
||
618 | } else { |
||
619 | if ( |
||
620 | self::LINE_SAME === $this->configuration['position_after_functions_and_oop_constructs'] |
||
621 | && ( |
||
622 | $token->isGivenKind(T_FUNCTION) && !$tokensAnalyzer->isLambda($index) |
||
623 | || $token->isGivenKind($classyTokens) && !$tokensAnalyzer->isAnonymousClass($index) |
||
624 | ) |
||
625 | && !$tokens[$tokens->getPrevNonWhitespace($startBraceIndex)]->isComment() |
||
626 | ) { |
||
627 | $ensuredWhitespace = ' '; |
||
628 | } else { |
||
629 | $ensuredWhitespace = $this->whitespacesConfig->getLineEnding().$indent; |
||
630 | } |
||
631 | |||
632 | $tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, $ensuredWhitespace); |
||
633 | } |
||
634 | } else { |
||
635 | $tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, ' '); |
||
636 | } |
||
637 | |||
638 | // reset loop limit due to collection change |
||
639 | $limit = \count($tokens); |
||
640 | } |
||
1009 |