|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ChiefTools\PhpCsFixer\Fixer; |
| 4 | + |
| 5 | +use SplFileInfo; |
| 6 | +use PhpCsFixer\AbstractFixer; |
| 7 | +use PhpCsFixer\Tokenizer\Token; |
| 8 | +use PhpCsFixer\Tokenizer\Tokens; |
| 9 | +use PhpCsFixer\FixerDefinition\CodeSample; |
| 10 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 11 | +use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface; |
| 12 | +use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
| 13 | + |
| 14 | +class MultilineMethodChainingFixer extends AbstractFixer implements WhitespacesAwareFixerInterface |
| 15 | +{ |
| 16 | + public function getName(): string |
| 17 | + { |
| 18 | + return 'ChiefTools/multiline_method_chaining'; |
| 19 | + } |
| 20 | + |
| 21 | + public function getDefinition(): FixerDefinitionInterface |
| 22 | + { |
| 23 | + return new FixerDefinition( |
| 24 | + 'Every object operator after the first multiline chain segment should start on a new line.', |
| 25 | + [ |
| 26 | + new CodeSample( |
| 27 | + <<<'PHP' |
| 28 | +<?php |
| 29 | +
|
| 30 | +$items = $service->items() |
| 31 | + ->filter()->values(); |
| 32 | + |
| 33 | +PHP, |
| 34 | + ), |
| 35 | + ], |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** Must run before MethodChainingIndentationFixer. */ |
| 40 | + public function getPriority(): int |
| 41 | + { |
| 42 | + return 1; |
| 43 | + } |
| 44 | + |
| 45 | + public function isCandidate(Tokens $tokens): bool |
| 46 | + { |
| 47 | + return $tokens->isAnyTokenKindsFound(Token::getObjectOperatorKinds()); |
| 48 | + } |
| 49 | + |
| 50 | + protected function applyFix(SplFileInfo $file, Tokens $tokens): void |
| 51 | + { |
| 52 | + $lineEnding = $this->whitespacesConfig->getLineEnding(); |
| 53 | + |
| 54 | + for ($index = 1, $count = count($tokens); $index < $count; $index++) { |
| 55 | + if (!$tokens[$index]->isObjectOperator() || $this->startsOnNewLine($tokens, $index)) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + $previousOperatorIndex = $this->previousChainObjectOperator($tokens, $index); |
| 60 | + |
| 61 | + if ($previousOperatorIndex === null || !$this->startsOnNewLine($tokens, $previousOperatorIndex)) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + $newline = new Token([T_WHITESPACE, $lineEnding]); |
| 66 | + |
| 67 | + if ($tokens[$index - 1]->isWhitespace()) { |
| 68 | + $tokens[$index - 1] = $newline; |
| 69 | + |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + $tokens->insertAt($index, $newline); |
| 74 | + $index++; |
| 75 | + $count++; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private function previousChainObjectOperator(Tokens $tokens, int $index): ?int |
| 80 | + { |
| 81 | + $segmentEndIndex = $tokens->getPrevMeaningfulToken($index); |
| 82 | + |
| 83 | + while ($segmentEndIndex !== null && $this->closesBlockOfType($tokens[$segmentEndIndex], Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE)) { |
| 84 | + $openingBracketIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $segmentEndIndex); |
| 85 | + $segmentEndIndex = $tokens->getPrevMeaningfulToken($openingBracketIndex); |
| 86 | + } |
| 87 | + |
| 88 | + if ($segmentEndIndex === null) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + if ($tokens[$segmentEndIndex]->equals(')')) { |
| 93 | + $openingParenthesisIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $segmentEndIndex); |
| 94 | + $memberIndex = $tokens->getPrevMeaningfulToken($openingParenthesisIndex); |
| 95 | + |
| 96 | + if ($memberIndex === null) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + $segmentEndIndex = $memberIndex; |
| 101 | + } elseif ($this->closesBlockOfType($tokens[$segmentEndIndex], Tokens::BLOCK_TYPE_DYNAMIC_PROP_BRACE)) { |
| 102 | + $openingBraceIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_DYNAMIC_PROP_BRACE, $segmentEndIndex); |
| 103 | + $segmentEndIndex = $openingBraceIndex; |
| 104 | + } |
| 105 | + |
| 106 | + $operatorIndex = $tokens->getPrevMeaningfulToken($segmentEndIndex); |
| 107 | + |
| 108 | + return $operatorIndex !== null && $tokens[$operatorIndex]->isObjectOperator() |
| 109 | + ? $operatorIndex |
| 110 | + : null; |
| 111 | + } |
| 112 | + |
| 113 | + private function closesBlockOfType(Token $token, int $type): bool |
| 114 | + { |
| 115 | + $block = Tokens::detectBlockType($token); |
| 116 | + |
| 117 | + return $block !== null && !$block['isStart'] && $block['type'] === $type; |
| 118 | + } |
| 119 | + |
| 120 | + private function startsOnNewLine(Tokens $tokens, int $index): bool |
| 121 | + { |
| 122 | + $previousMeaningfulIndex = $tokens->getPrevMeaningfulToken($index); |
| 123 | + |
| 124 | + if ($previousMeaningfulIndex === null) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + for ($i = $previousMeaningfulIndex + 1; $i < $index; $i++) { |
| 129 | + if (preg_match('/\R/', $tokens[$i]->getContent())) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return false; |
| 135 | + } |
| 136 | +} |
0 commit comments