Skip to content

Commit

Permalink
MethodArgumentSpaceFixer - add empty line incorrectly
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum authored and keradus committed Jul 6, 2018
1 parent 0df921d commit d7d8e61
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
39 changes: 39 additions & 0 deletions tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php
Expand Up @@ -83,6 +83,45 @@ public function testFixWithDifferentLineEndings(
public function provideFixCases()
{
return [
[
'<?php
// space '.'
$var1 = $a->some_method(
$var2
);
// space '.'
$var2 = some_function(
$var2
);
// space '.'
$var2a = $z[1](
$var2a
);
'.'
$var3 = function( $a, $b ) { };
',
'<?php
// space '.'
$var1 = $a->some_method(
$var2);
// space '.'
$var2 = some_function(
$var2);
// space '.'
$var2a = $z[1](
$var2a
);
'.'
$var3 = function( $a , $b ) { };
',
[
'on_multiline' => 'ensure_fully_multiline',
],
],
'default' => [
'<?php xyz("", "", "", "");',
'<?php xyz("","","","");',
Expand Down

0 comments on commit d7d8e61

Please sign in to comment.