diff --git a/src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php b/src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php index a8d5639d30c..03ce292a08c 100644 --- a/src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php +++ b/src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php @@ -285,6 +285,10 @@ private function ensureSingleLine(Tokens $tokens, $index) return true; } + /** + * @param Tokens $tokens + * @param int $startFunctionIndex + */ private function ensureFunctionFullyMultiline(Tokens $tokens, $startFunctionIndex) { // find out what the indentation is @@ -295,17 +299,31 @@ private function ensureFunctionFullyMultiline(Tokens $tokens, $startFunctionInde [[T_WHITESPACE]] ); $searchIndex = $prevWhitespaceTokenIndex; - } while ($prevWhitespaceTokenIndex + } while (null !== $prevWhitespaceTokenIndex && false === strpos($tokens[$prevWhitespaceTokenIndex]->getContent(), "\n") ); - $existingIndentation = $prevWhitespaceTokenIndex - ? ltrim($tokens[$prevWhitespaceTokenIndex]->getContent(), "\n\r") - : ''; + + if (null === $prevWhitespaceTokenIndex) { + $existingIndentation = ''; + } else { + $existingIndentation = $tokens[$prevWhitespaceTokenIndex]->getContent(); + $lastLineIndex = strrpos($existingIndentation, "\n"); + $existingIndentation = false === $lastLineIndex + ? $existingIndentation + : substr($existingIndentation, $lastLineIndex + 1) + ; + } $indentation = $existingIndentation.$this->whitespacesConfig->getIndent(); $endFunctionIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startFunctionIndex); + if (!$this->isNewline($tokens[$endFunctionIndex - 1])) { - $tokens->ensureWhitespaceAtIndex($endFunctionIndex, 0, $this->whitespacesConfig->getLineEnding().$existingIndentation); + $tokens->ensureWhitespaceAtIndex( + $endFunctionIndex, + 0, + $this->whitespacesConfig->getLineEnding().$existingIndentation + ); + ++$endFunctionIndex; } diff --git a/tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php b/tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php index 565f3e4a3a8..6fbbcc289af 100644 --- a/tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php +++ b/tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php @@ -83,6 +83,45 @@ public function testFixWithDifferentLineEndings( public function provideFixCases() { return [ + [ + 'some_method( + $var2 +); + +// space '.' +$var2 = some_function( + $var2 +); + + // space '.' + $var2a = $z[1]( + $var2a + ); + '.' + $var3 = function( $a, $b ) { }; +', + 'some_method( + $var2); + +// space '.' +$var2 = some_function( + $var2); + + // space '.' + $var2a = $z[1]( + $var2a + ); + '.' + $var3 = function( $a , $b ) { }; +', + [ + 'on_multiline' => 'ensure_fully_multiline', + ], + ], 'default' => [ '