Просмотр исходного кода

Do not add an empty line at EOF if the PHP tags have been closed

Possum 9 лет назад
Родитель
Сommit
6f76f4afce

+ 2 - 2
README.rst

@@ -199,8 +199,8 @@ Choose from the list of available fixers:
                 keywords looks like single words.
 
 * **eof_ending** [PSR-2]
-                A file must always end with an empty
-                line feed.
+                A file must always end with a single
+                empty line feed.
 
 * **function_call_space** [PSR-2]
                 When making a method or function call,

+ 19 - 6
Symfony/CS/Fixer/PSR2/EofEndingFixer.php

@@ -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.';
     }
 
     /**

+ 42 - 7
Symfony/CS/Tests/Fixer/PSR2/EofEndingFixerTest.php

@@ -29,6 +29,9 @@ class EofEndingFixerTest extends AbstractFixerTestBase
     public function provideCases()
     {
         return array(
+            array(
+                "<?php\n",
+            ),
             array(
                 '<?php
 $a = 1;
@@ -52,17 +55,49 @@ $a = 3;
 ',
             ),
             array(
-                "<?php\r\$a = 1;\n",
-                "<?php\r\$a = 1;",
+                "<?php\r\n\$a = 4;\n",
+                "<?php\r\n\$a = 4;",
             ),
             array(
-                "<?php\r\$a = 1;\n",
-                "<?php\r\$a = 1;\r",
+                // test not changing line break characters,
+                // this is not the responsibility of this fixer
+                "<?php\r\n\$a = 5;\r\n",
+                "<?php\r\n\$a = 5;\r\n    \r\n",
             ),
             array(
-                // test for not adding an empty line in empty file
-                '',
-            ),
+                '<?php
+$a = 6;
+
+//test
+
+?>
+  ', ),
+            array(
+                // test for not adding an empty line after PHP tag has been closed
+                '<?php
+$a = 7;
+
+//test
+
+?>', ),
+            array(
+                // test for not adding an empty line after PHP tag has been closed
+                '<?php
+$a = 8;
+//test
+?>
+Outside of PHP tags rendering
+
+', ),
+array(
+                // test for not adding an empty line after PHP tag has been closed
+                "<?php
+//test
+?>
+inline 1
+<?php
+
+?>Inline2\r\n", ),
         );
     }
 }

+ 1 - 0
Symfony/CS/Tests/FixerTest.php

@@ -198,6 +198,7 @@ class FixerTest extends \PHPUnit_Framework_TestCase
             array($fixers['phpdoc_var_without_name'], $fixers['phpdoc_trim']),
             array($fixers['phpdoc_order'], $fixers['phpdoc_trim']),
             array($fixers['unused_use'], $fixers['line_after_namespace']),
+            array($fixers['linefeed'], $fixers['eof_ending']),
         );
 
         $docFixerNames = array_filter(

+ 2 - 2
Symfony/CS/Tokenizer/Token.php

@@ -390,12 +390,12 @@ class Token
      */
     public function isWhitespace(array $opts = array())
     {
-        $whitespaces = isset($opts['whitespaces']) ? $opts['whitespaces'] : " \t\n\r\0\x0B";
-
         if ($this->isArray && !$this->isGivenKind(T_WHITESPACE)) {
             return false;
         }
 
+        $whitespaces = isset($opts['whitespaces']) ? $opts['whitespaces'] : " \t\n\r\0\x0B";
+
         return '' === trim($this->content, $whitespaces);
     }