Skip to content

Commit d7d8e61

Browse files
SpacePossumkeradus
authored andcommitted
MethodArgumentSpaceFixer - add empty line incorrectly
1 parent 0df921d commit d7d8e61

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ private function ensureSingleLine(Tokens $tokens, $index)
285285
return true;
286286
}
287287

288+
/**
289+
* @param Tokens $tokens
290+
* @param int $startFunctionIndex
291+
*/
288292
private function ensureFunctionFullyMultiline(Tokens $tokens, $startFunctionIndex)
289293
{
290294
// find out what the indentation is
@@ -295,17 +299,31 @@ private function ensureFunctionFullyMultiline(Tokens $tokens, $startFunctionInde
295299
[[T_WHITESPACE]]
296300
);
297301
$searchIndex = $prevWhitespaceTokenIndex;
298-
} while ($prevWhitespaceTokenIndex
302+
} while (null !== $prevWhitespaceTokenIndex
299303
&& false === strpos($tokens[$prevWhitespaceTokenIndex]->getContent(), "\n")
300304
);
301-
$existingIndentation = $prevWhitespaceTokenIndex
302-
? ltrim($tokens[$prevWhitespaceTokenIndex]->getContent(), "\n\r")
303-
: '';
305+
306+
if (null === $prevWhitespaceTokenIndex) {
307+
$existingIndentation = '';
308+
} else {
309+
$existingIndentation = $tokens[$prevWhitespaceTokenIndex]->getContent();
310+
$lastLineIndex = strrpos($existingIndentation, "\n");
311+
$existingIndentation = false === $lastLineIndex
312+
? $existingIndentation
313+
: substr($existingIndentation, $lastLineIndex + 1)
314+
;
315+
}
304316

305317
$indentation = $existingIndentation.$this->whitespacesConfig->getIndent();
306318
$endFunctionIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startFunctionIndex);
319+
307320
if (!$this->isNewline($tokens[$endFunctionIndex - 1])) {
308-
$tokens->ensureWhitespaceAtIndex($endFunctionIndex, 0, $this->whitespacesConfig->getLineEnding().$existingIndentation);
321+
$tokens->ensureWhitespaceAtIndex(
322+
$endFunctionIndex,
323+
0,
324+
$this->whitespacesConfig->getLineEnding().$existingIndentation
325+
);
326+
309327
++$endFunctionIndex;
310328
}
311329

tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,45 @@ public function testFixWithDifferentLineEndings(
8383
public function provideFixCases()
8484
{
8585
return [
86+
[
87+
'<?php
88+
// space '.'
89+
$var1 = $a->some_method(
90+
$var2
91+
);
92+
93+
// space '.'
94+
$var2 = some_function(
95+
$var2
96+
);
97+
98+
// space '.'
99+
$var2a = $z[1](
100+
$var2a
101+
);
102+
'.'
103+
$var3 = function( $a, $b ) { };
104+
',
105+
'<?php
106+
// space '.'
107+
$var1 = $a->some_method(
108+
$var2);
109+
110+
// space '.'
111+
$var2 = some_function(
112+
$var2);
113+
114+
// space '.'
115+
$var2a = $z[1](
116+
$var2a
117+
);
118+
'.'
119+
$var3 = function( $a , $b ) { };
120+
',
121+
[
122+
'on_multiline' => 'ensure_fully_multiline',
123+
],
124+
],
86125
'default' => [
87126
'<?php xyz("", "", "", "");',
88127
'<?php xyz("","","","");',

0 commit comments

Comments
 (0)