|
@@ -12,6 +12,8 @@
|
|
|
namespace Symfony\CS\Fixer\PSR2;
|
|
|
|
|
|
use Symfony\CS\AbstractFixer;
|
|
|
+use Symfony\CS\Tokenizer\Token;
|
|
|
+use Symfony\CS\Tokenizer\Tokens;
|
|
|
|
|
|
/**
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
@@ -23,15 +25,26 @@ class EofEndingFixer extends AbstractFixer
|
|
|
*/
|
|
|
public function fix(\SplFileInfo $file, $content)
|
|
|
{
|
|
|
- // [Structure] A file must always end with a linefeed character
|
|
|
+ $tokens = Tokens::fromCode($content);
|
|
|
|
|
|
- $content = rtrim($content);
|
|
|
+ $count = $tokens->count();
|
|
|
+ if (0 === $count) {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+
|
|
|
+ $token = $tokens[$count - 1];
|
|
|
+ if ($token->isGivenKind(array(T_INLINE_HTML, T_CLOSE_TAG, T_OPEN_TAG))) {
|
|
|
+ return $content;
|
|
|
+ }
|
|
|
|
|
|
- if ('' !== $content) {
|
|
|
- return $content."\n";
|
|
|
+ if ($token->isWhitespace()) {
|
|
|
+ $lineBreak = false === strrpos($token->getContent(), "\r") ? "\n" : "\r\n";
|
|
|
+ $token->setContent($lineBreak);
|
|
|
+ } else {
|
|
|
+ $tokens->insertAt($count, new Token(array(T_WHITESPACE, "\n")));
|
|
|
}
|
|
|
|
|
|
- return $content;
|
|
|
+ return $tokens->generateCode();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -39,7 +52,7 @@ class EofEndingFixer extends AbstractFixer
|
|
|
*/
|
|
|
public function getDescription()
|
|
|
{
|
|
|
- return 'A file must always end with an empty line feed.';
|
|
|
+ return 'A file must always end with a single empty line feed.';
|
|
|
}
|
|
|
|
|
|
/**
|