Browse Source

SetTypeToCastFixer - support PHP 7.3

Kuba Werłos 6 years ago
parent
commit
52ffa3979d

+ 4 - 0
src/Fixer/Alias/SetTypeToCastFixer.php

@@ -189,6 +189,10 @@ settype($bar, "null");
         $closeParenthesisIndex
     ) {
         $tokens->clearTokenAndMergeSurroundingWhitespace($closeParenthesisIndex);
+        $prevIndex = $tokens->getPrevMeaningfulToken($closeParenthesisIndex);
+        if ($tokens[$prevIndex]->equals(',')) {
+            $tokens->clearTokenAndMergeSurroundingWhitespace($prevIndex);
+        }
         $tokens->clearTokenAndMergeSurroundingWhitespace($secondArgumentStart);
         $tokens->clearTokenAndMergeSurroundingWhitespace($commaIndex);
         $tokens->clearTokenAndMergeSurroundingWhitespace($firstArgumentStart);

+ 50 - 0
tests/Fixer/Alias/SetTypeToCastFixerTest.php

@@ -217,4 +217,54 @@ $foo#5
             ],
         ];
     }
+
+    /**
+     * @param string $expected
+     * @param string $input
+     *
+     * @requires PHP 7.3
+     * @dataProvider provideFix73Cases
+     */
+    public function testFix73($expected, $input)
+    {
+        $this->doTest($expected, $input);
+    }
+
+    public function provideFix73Cases()
+    {
+        return [
+            'null cast' => [
+                '<?php $foo = null;',
+                '<?php settype($foo, "null");',
+            ],
+            'boolean' => [
+                '<?php $foo = (bool) $foo;',
+                '<?php settype($foo, "boolean", );',
+            ],
+            'comments with line breaks' => [
+                '<?php #0
+#1
+$foo = (int) $foo#2
+#3
+#4
+#5
+#6
+#7
+#8
+#9
+;#10',
+                '<?php #0
+#1
+settype#2
+#3
+(#4
+$foo#5
+,#6
+"integer"#7
+,#8
+)#9
+;#10',
+            ],
+        ];
+    }
 }

+ 2 - 0
tests/Fixtures/Integration/misc/PHP7_3.test

@@ -54,6 +54,7 @@ final class MyTest extends \PHPUnit_Framework_TestCase
     }
 }
 random_int($a, $b, ); // `random_api_migration` rule
+$foo = (int) $foo; // `set_type_to_cast` rule
 foo(null === $a, ); // `yoda_style` rule
 
 --INPUT--
@@ -92,4 +93,5 @@ final class MyTest extends \PHPUnit_Framework_TestCase
     }
 }
 rand($a, $b, ); // `random_api_migration` rule
+settype($foo, "integer", ); // `set_type_to_cast` rule
 foo($a === null, ); // `yoda_style` rule