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

bug #415 Short tag fix for #303, second approach. (keradus)

This PR was squashed before being merged into the 0.5.x-dev branch (closes #415).

Discussion
----------

Short tag fix for #303, second approach.

Included new test case and rewrite fix method in ShortTagFixer to use Tokens.
This time no short_open_tag option is needed.

Commits
-------

5e5e630 Short tag fix for #303, second approach.
Fabien Potencier 10 лет назад
Родитель
Сommit
4bfe11d31a

+ 2 - 1
Symfony/CS/Fixer/PhpClosingTagFixer.php

@@ -48,7 +48,8 @@ class PhpClosingTagFixer implements FixerInterface
 
     public function getPriority()
     {
-        return 0;
+        // should be run before the ShortTagFixer
+        return 5;
     }
 
     public function supports(\SplFileInfo $file)

+ 56 - 3
Symfony/CS/Fixer/ShortTagFixer.php

@@ -12,16 +12,69 @@
 namespace Symfony\CS\Fixer;
 
 use Symfony\CS\FixerInterface;
+use Symfony\CS\Tokens;
 
 /**
- * @author Fabien Potencier <fabien@symfony.com>
+ * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  */
 class ShortTagFixer implements FixerInterface
 {
     public function fix(\SplFileInfo $file, $content)
     {
-        // [Structure] Never use short tags (<?)
-        return preg_replace('/<\?(\s|$)/', '<?php$1', $content);
+        // replace all <? with <?php to replace all short open tags even without short_open_tag option enabled
+        $newContent = preg_replace('/<\?(\s|$)/', '<?php$1', $content);
+
+        /* the following code is magic to revert previous replacements which should NOT be replaced, for example incorrectly replacing
+         * > echo '<? ';
+         * with
+         * > echo '<?php ';
+        */
+        $tokens = Tokens::fromCode($newContent);
+        $tokensOldContent = '';
+        $tokensOldContentLength = 0;
+
+        foreach ($tokens as $token) {
+            if ($token->isGivenKind(T_OPEN_TAG)) {
+                $tokenContent = $token->content;
+
+                if ('<?php' !== substr($content, $tokensOldContentLength, 5)) {
+                    $tokenContent = '<? ';
+                }
+
+                $tokensOldContent .= $tokenContent;
+                $tokensOldContentLength += strlen($tokenContent);
+                continue;
+            }
+
+            if ($token->isGivenKind(array(T_COMMENT, T_DOC_COMMENT, T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE, T_STRING, ))) {
+                $tokenContent = '';
+                $tokenContentLength = 0;
+                $parts = explode('<?php ', $token->content);
+                $iLast = count($parts) - 1;
+
+                foreach ($parts as $i => $part) {
+                    $tokenContent .= $part;
+                    $tokenContentLength += strlen($part);
+
+                    if ($i !== $iLast) {
+                        if ('<?php' === substr($content, $tokensOldContentLength + $tokenContentLength, 5)) {
+                            $tokenContent .= '<?php ';
+                            $tokenContentLength += 5;
+                        } else {
+                            $tokenContent .= '<? ';
+                            $tokenContentLength += 3;
+                        }
+                    }
+                }
+
+                $token->content = $tokenContent;
+            }
+
+            $tokensOldContent .= $token->content;
+            $tokensOldContentLength += strlen($token->content);
+        }
+
+        return $tokens->generateCode();
     }
 
     public function getLevel()

+ 18 - 3
Symfony/CS/Tests/Fixer/ShortTagFixerTest.php

@@ -11,7 +11,7 @@
 
 namespace Symfony\CS\Tests\Fixer;
 
-use Symfony\CS\Fixer\ShortTagFixer;
+use Symfony\CS\Fixer\ShortTagFixer as Fixer;
 
 class ShortTagFixerTest extends \PHPUnit_Framework_TestCase
 {
@@ -20,7 +20,7 @@ class ShortTagFixerTest extends \PHPUnit_Framework_TestCase
      */
     public function testOneLineFix($expected, $input)
     {
-        $fixer = new ShortTagFixer();
+        $fixer = new Fixer();
         $file = $this->getTestFile();
 
         $this->assertSame($expected, $fixer->fix($file, $input));
@@ -32,6 +32,7 @@ class ShortTagFixerTest extends \PHPUnit_Framework_TestCase
             array('<?php echo \'Foo\';', '<? echo \'Foo\';'),
             array('<?= echo \'Foo\';', '<?= echo \'Foo\';'),
             array('<?php echo \'Foo\'; ?> PLAIN TEXT', '<?php echo \'Foo\'; ?> PLAIN TEXT'),
+            array('<?php $query = "SELECT .... FROM my_table WHERE id <? LIMIT 1";', '<? $query = "SELECT .... FROM my_table WHERE id <? LIMIT 1";'),
             array('PLAIN TEXT<?php echo \'Foo\'; ?>', 'PLAIN TEXT<?php echo \'Foo\'; ?>'),
             array('<?php
 
@@ -42,7 +43,21 @@ echo \'Foo\';
 
 echo \'Foo\';
 
-')
+'),
+            array(
+                "<?php if ('<?php' === '<?') { }",
+                "<? if ('<?php' === '<?') { }",
+            ),
+            array(
+                'foo <?php  echo "-"; echo "aaa <?php bbb <? ccc"; echo \'<? \'; /* <? */ /** <? */ ?> bar <?php echo "<? ";',
+                'foo <?  echo "-"; echo "aaa <?php bbb <? ccc"; echo \'<? \'; /* <? */ /** <? */ ?> bar <? echo "<? ";'
+            ),
+            array(
+                '<?php
+// Replace all <? with <?php !',
+                '<?php
+// Replace all <? with <?php !',
+            ),
         );
     }