Browse Source

bug #5561 NoMixedEchoPrintFixer: fix for conditions without curly brackets (kubawerlos)

This PR was squashed before being merged into the 2.18 branch.

Discussion
----------

NoMixedEchoPrintFixer: fix for conditions without curly brackets

Fixes https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/5539

In separate PR I will refactor other test cases to have more shared snippets to convert in both ways, I didn't want to do that in this PR to make this PR simpler.

Commits
-------

8d8d053e2 NoMixedEchoPrintFixer: fix for conditions without curly brackets
Dariusz Ruminski 3 years ago
parent
commit
f669ea6ecf

+ 1 - 1
src/Fixer/Alias/NoMixedEchoPrintFixer.php

@@ -153,7 +153,7 @@ final class NoMixedEchoPrintFixer extends AbstractFixer implements Configuration
     {
         $prevToken = $tokens[$tokens->getPrevMeaningfulToken($index)];
 
-        if (!$prevToken->equalsAny([';', '{', '}', [T_OPEN_TAG]])) {
+        if (!$prevToken->equalsAny([';', '{', '}', ')', [T_OPEN_TAG], [T_ELSE]])) {
             return;
         }
 

+ 43 - 8
tests/Fixer/Alias/NoMixedEchoPrintFixerTest.php

@@ -30,6 +30,7 @@ final class NoMixedEchoPrintFixerTest extends AbstractFixerTestCase
      * @param null|string $input
      *
      * @dataProvider provideEchoToPrintFixCases
+     * @dataProvider provideEchoToPrintFixNewCases
      */
     public function testFixEchoToPrint($expected, $input = null)
     {
@@ -129,21 +130,28 @@ final class NoMixedEchoPrintFixerTest extends AbstractFixerTestCase
                 echo "bar";
                 ',
             ],
-            [
-                "<div><?php print 'foo' ?></div>",
-                "<div><?php echo 'foo' ?></div>",
-            ],
             [
                 '<?=$foo?>',
             ],
         ];
     }
 
+    public static function provideEchoToPrintFixNewCases()
+    {
+        foreach (self::getCodeSnippetsToConvertBothWays() as $name => $codeSnippet) {
+            yield [
+                sprintf($codeSnippet, 'print'),
+                sprintf($codeSnippet, 'echo'),
+            ];
+        }
+    }
+
     /**
      * @param string      $expected
      * @param null|string $input
      *
      * @dataProvider providePrintToEchoFixCases
+     * @dataProvider providePrintToEchoFixNewCases
      */
     public function testFixPrintToEcho($expected, $input = null)
     {
@@ -267,13 +275,19 @@ final class NoMixedEchoPrintFixerTest extends AbstractFixerTestCase
                 print "bar";
                 ',
             ],
-            [
-                "<div><?php echo 'foo' ?></div>",
-                "<div><?php print 'foo' ?></div>",
-            ],
         ];
     }
 
+    public static function providePrintToEchoFixNewCases()
+    {
+        foreach (self::getCodeSnippetsToConvertBothWays() as $name => $codeSnippet) {
+            yield [
+                sprintf($codeSnippet, 'echo'),
+                sprintf($codeSnippet, 'print'),
+            ];
+        }
+    }
+
     /**
      * @group legacy
      * @expectedDeprecation Passing NULL to set default configuration is deprecated and will not be supported in 3.0, use an empty array instead.
@@ -335,4 +349,25 @@ final class NoMixedEchoPrintFixerTest extends AbstractFixerTestCase
 
         static::assertSame($expected, $reflectionProperty->getValue($fixer));
     }
+
+    private static function getCodeSnippetsToConvertBothWays()
+    {
+        yield 'inside of HTML' => '<div><?php %1$s "foo" ?></div>';
+
+        yield 'foreach without curly brackets' => '<?php
+            %1$s "There will be foos: ";
+            foreach ($foos as $foo)
+                %1$s $foo;
+            %1$s "End of foos";
+        ';
+
+        yield 'if and else without curly brackets' => '<?php
+            if ($foo)
+                %1$s "One";
+            elseif ($bar)
+                %1$s "Two";
+            else
+                %1$s "Three";
+        ';
+    }
 }