Skip to content

Commit

Permalink
bug #4548 HeredocIndentationFixer - remove whitespace in empty lines …
Browse files Browse the repository at this point in the history
…(gharlan)

This PR was merged into the 2.15 branch.

Discussion
----------

HeredocIndentationFixer - remove whitespace in empty lines

replaces #4422

Commits
-------

8768cbc HeredocIndentationFixer - remove whitespace in empty lines
  • Loading branch information
SpacePossum committed Oct 8, 2019
2 parents 3136200 + 8768cbc commit 7a68ba5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/Fixer/Whitespace/HeredocIndentationFixer.php
Expand Up @@ -94,8 +94,9 @@ private function fixIndentation(Tokens $tokens, $start, $end)

Preg::match('/^[ \t]*/', $tokens[$end]->getContent(), $matches);
$currentIndent = $matches[0];
$currentIndentLength = \strlen($currentIndent);

$content = $indent.substr($tokens[$end]->getContent(), \strlen($currentIndent));
$content = $indent.substr($tokens[$end]->getContent(), $currentIndentLength);
$tokens[$end] = new Token([T_END_HEREDOC, $content]);

if ($end === $start + 1) {
Expand All @@ -107,19 +108,35 @@ private function fixIndentation(Tokens $tokens, $start, $end)
continue;
}

$regexEnd = $last && !$currentIndent ? '(?!$)' : '';
$content = Preg::replace('/(?<=\v)'.$currentIndent.$regexEnd.'/', $indent, $tokens[$index]->getContent());
$content = $tokens[$index]->getContent();

if ('' !== $currentIndent) {
$content = Preg::replace('/(?<=\v)(?!'.$currentIndent.')[ \t]+/', '', $content);
}

$regexEnd = $last && !$currentIndent ? '(?!\v|$)' : '(?!\v)';
$content = Preg::replace('/(?<=\v)'.$currentIndent.$regexEnd.'/', $indent, $content);

$tokens[$index] = new Token([$tokens[$index]->getId(), $content]);
}

++$index;

if ($tokens[$index]->isGivenKind(T_ENCAPSED_AND_WHITESPACE)) {
$content = $indent.substr($tokens[$index]->getContent(), \strlen($currentIndent));
$tokens[$index] = new Token([T_ENCAPSED_AND_WHITESPACE, $content]);
} else {
if (!$tokens[$index]->isGivenKind(T_ENCAPSED_AND_WHITESPACE)) {
$tokens->insertAt($index, new Token([T_ENCAPSED_AND_WHITESPACE, $indent]));

return;
}

$content = $tokens[$index]->getContent();

if (!\in_array($content[0], ["\r", "\n"], true) && (!$currentIndent || $currentIndent === substr($content, 0, $currentIndentLength))) {
$content = $indent.substr($content, $currentIndentLength);
} elseif ($currentIndent) {
$content = Preg::replace('/^(?!'.$currentIndent.')[ \t]+/', '', $content);
}

$tokens[$index] = new Token([T_ENCAPSED_AND_WHITESPACE, $content]);
}

/**
Expand Down
68 changes: 68 additions & 0 deletions tests/Fixer/Whitespace/HeredocIndentationFixerTest.php
Expand Up @@ -67,6 +67,24 @@ public function provideFixCases()
<<<'INPUT'
<?php
foo(<<<EOD
EOD
);
INPUT
,
],
[
<<<'EXPECTED'
<?php
foo(<<<EOD
EOD
);
EXPECTED
,
<<<'INPUT'
<?php
foo(<<<EOD
EOD
);
INPUT
Expand All @@ -77,6 +95,7 @@ public function provideFixCases()
<?php
foo(<<<EOD
abc
def
EOD
);
Expand All @@ -86,6 +105,7 @@ public function provideFixCases()
<?php
foo(<<<EOD
abc
def
EOD
);
Expand All @@ -96,17 +116,21 @@ public function provideFixCases()
<<<'EXPECTED'
<?php
foo(<<<'EOD'
abc
def
EOD
);
EXPECTED
,
<<<'INPUT'
<?php
foo(<<<'EOD'
abc
def
EOD
);
INPUT
Expand Down Expand Up @@ -176,6 +200,50 @@ public function provideFixCases()
INPUT
,
],
[
/* EXPECTED */ '
<?php
foo(<<<EOD
'.'
abc
'.'
def
'.'
EOD
);',
/* INPUT */ '
<?php
foo(<<<EOD
'.'
abc
'.'
def
'.'
EOD
);',
],
[
/* EXPECTED */ '
<?php
foo(<<<EOD
abc
def
EOD
);',
/* INPUT */ '
<?php
foo(<<<EOD
'.'
abc
'.'
def
'.'
EOD
);',
],
];
}

Expand Down

0 comments on commit 7a68ba5

Please sign in to comment.