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

NoTrailingWhitespaceFixer - trim space after opening tag #3957

Merged
merged 1 commit into from
Aug 10, 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
26 changes: 23 additions & 3 deletions src/Fixer/Whitespace/NoTrailingWhitespaceFixer.php
Expand Up @@ -62,17 +62,37 @@ public function isCandidate(Tokens $tokens)
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
for ($index = count($tokens) - 1; $index >= 0; --$index) {
$token = $tokens[$index];
if (
$token->isGivenKind(T_OPEN_TAG)
&& $tokens->offsetExists($index + 1)
&& $tokens[$index + 1]->isWhitespace()
&& 1 === Preg::match('/(.*)\h$/', $token->getContent(), $openTagMatches)
&& 1 === Preg::match('/^(\R)(.*)$/s', $tokens[$index + 1]->getContent(), $whitespaceMatches)
) {
$tokens[$index] = new Token([T_OPEN_TAG, $openTagMatches[1].$whitespaceMatches[1]]);
if ('' === $whitespaceMatches[2]) {
$tokens->clearAt($index + 1);
} else {
$tokens[$index + 1] = new Token([T_WHITESPACE, $whitespaceMatches[2]]);
}

continue;
}

if (!$token->isWhitespace()) {
continue;
}

$lines = Preg::split("/([\r\n]+)/", $token->getContent(), -1, PREG_SPLIT_DELIM_CAPTURE);
$lines = Preg::split('/(\\R+)/', $token->getContent(), -1, PREG_SPLIT_DELIM_CAPTURE);
$linesSize = count($lines);

// fix only multiline whitespaces or singleline whitespaces at the end of file
if ($linesSize > 1 || !isset($tokens[$index + 1])) {
$lines[0] = rtrim($lines[0], " \t");
if (!$tokens[$index - 1]->isGivenKind(T_OPEN_TAG) || 1 !== Preg::match('/(.*)\R$/', $tokens[$index - 1]->getContent())) {
$lines[0] = rtrim($lines[0], " \t");
}

for ($i = 1; $i < $linesSize; ++$i) {
$trimmedLine = rtrim($lines[$i], " \t");
Expand Down
38 changes: 38 additions & 0 deletions tests/Fixer/Whitespace/NoTrailingWhitespaceFixerTest.php
Expand Up @@ -96,6 +96,44 @@ public function provideFixCases()
[
"<?php\necho <<<'EOT'\nInline Il y eut un \r\nrire éclatant \n \n \r\nEOT;\n\n",
],
[
"<?php\necho 'Hello World';",
"<?php \necho 'Hello World';",
],
[
"<?php\n\necho 'Hello World';",
"<?php \n\necho 'Hello World';",
],
[
"<?php\r\necho 'Hello World';",
"<?php \r\necho 'Hello World';",
],
[
"<?php\necho 'Hello World';",
"<?php \necho 'Hello World';",
],
[
"<?php\necho 'Hello World';",
"<?php \necho 'Hello World';",
],
[
'<?php ',
'<?php ',
],
[
"<?php\t",
"<?php\t\t",
],
[
'<?php ', // do not trim this as "<?php" is not valid PHP
],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add this case;

[
"<?php\n      \n   \n    ",
]
// and
[
"<?php      \n   \n    ",
]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SpacePossum added, I hope you mean 2nd one with fix (as the first set of spaces is in non-blank line).

[
"<?php\n \n \n ",
],
[
"<?php\n \n ",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a dumb question, but how is this not fixed into "<?php\n\n " instead? Different bug in the same fixer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of the fixer is a little misleading, here is description:

Remove trailing whitespace at the end of non-blank lines.

So, the only non-blank line is the one with open tag.

"<?php \n \n ",
],
];
}
}