diff --git a/src/Fixer/PhpTag/LinebreakAfterOpeningTagFixer.php b/src/Fixer/PhpTag/LinebreakAfterOpeningTagFixer.php index 5d59e52f392..5eef330eaba 100644 --- a/src/Fixer/PhpTag/LinebreakAfterOpeningTagFixer.php +++ b/src/Fixer/PhpTag/LinebreakAfterOpeningTagFixer.php @@ -53,6 +53,11 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) return; } + // ignore if linebreak already present + if (false !== strpos($tokens[0]->getContent(), "\n")) { + return; + } + $newlineFound = false; foreach ($tokens as $token) { if ($token->isWhitespace() && false !== strpos($token->getContent(), "\n")) { @@ -67,7 +72,6 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) return; } - $token = $tokens[0]; - $tokens[0] = new Token([$token->getId(), rtrim($token->getContent()).$this->whitespacesConfig->getLineEnding()]); + $tokens[0] = new Token([T_OPEN_TAG, rtrim($tokens[0]->getContent()).$this->whitespacesConfig->getLineEnding()]); } } diff --git a/tests/Fixer/PhpTag/LinebreakAfterOpeningTagFixerTest.php b/tests/Fixer/PhpTag/LinebreakAfterOpeningTagFixerTest.php index c02ac888c2d..a11e627c96d 100644 --- a/tests/Fixer/PhpTag/LinebreakAfterOpeningTagFixerTest.php +++ b/tests/Fixer/PhpTag/LinebreakAfterOpeningTagFixerTest.php @@ -81,6 +81,11 @@ public function provideFixCases() $foo = $bar; ?>', ], + [ + str_replace("\n", "\r\n", '