Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MethodArgumentSpaceFixer - add empty line incorrectly #3839

Merged
merged 1 commit into from Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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