Browse Source

bug #4808 Modernize types casting fixer array curly (SpacePossum)

This PR was squashed before being merged into the 2.15 branch (closes #4808).

Discussion
----------

Modernize types casting fixer array curly

Commits
-------

1ea90104 Modernize types casting fixer array curly
SpacePossum 5 years ago
parent
commit
5eaaa62596

+ 1 - 1
src/Fixer/CastNotation/ModernizeTypesCastingFixer.php

@@ -112,7 +112,7 @@ final class ModernizeTypesCastingFixer extends AbstractFunctionReferenceFixer
 
                 $afterCloseParenthesisIndex = $tokens->getNextMeaningfulToken($closeParenthesis);
                 $afterCloseParenthesisToken = $tokens[$afterCloseParenthesisIndex];
-                $wrapInParenthesises = $afterCloseParenthesisToken->equals('[') || $afterCloseParenthesisToken->isGivenKind(T_POW);
+                $wrapInParenthesises = $afterCloseParenthesisToken->equalsAny(['[', '{']) || $afterCloseParenthesisToken->isGivenKind(T_POW);
 
                 // analyse namespace specification (root one or none) and decide what to do
                 $prevTokenIndex = $tokens->getPrevMeaningfulToken($functionName);

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

@@ -190,6 +190,9 @@ $foo#5
             'return value used IV' => [
                 '<?php $a = "123"; $b = [3 => settype($a, "integer")];',
             ],
+            'return value used V' => [
+                '<?= settype($foo, "object");',
+            ],
             'wrapped statements, fixable after removing the useless parenthesis brace' => [
                 '<?php
                     settype(/*1*//*2*/($a), \'int\');

+ 4 - 0
tests/Fixer/CastNotation/ModernizeTypesCastingFixerTest.php

@@ -209,6 +209,10 @@ intval#
                 '<?php $foo = ((string) ($x + $y))[0];',
                 '<?php $foo = strval($x + $y)[0];',
             ],
+            [
+                '<?php $foo = ((string) ($x + $y)){0};',
+                '<?php $foo = strval($x + $y){0};',
+            ],
         ];
     }
 

+ 11 - 3
tests/Fixer/Semicolon/SemicolonAfterInstructionFixerTest.php

@@ -41,14 +41,18 @@ final class SemicolonAfterInstructionFixerTest extends AbstractFixerTestCase
                 '<?php $a = [1,2,3]; echo $a{1}; ?>',
                 '<?php $a = [1,2,3]; echo $a{1} ?>',
             ],
-            [
+            'comment' => [
                 '<?php $a++;//a ?>',
                 '<?php $a++//a ?>',
             ],
-            [
+            'comment II' => [
                 '<?php $b++; /**/ ?>',
                 '<?php $b++ /**/ ?>',
             ],
+            'no space' => [
+                '<?php $b++;?>',
+                '<?php $b++?>',
+            ],
             [
                 '<?php echo 123; ?>',
                 '<?php echo 123 ?>',
@@ -58,6 +62,7 @@ final class SemicolonAfterInstructionFixerTest extends AbstractFixerTestCase
                 "<?php echo 123\n\t?>",
             ],
             ['<?php ?>'],
+            ['<?php ; ?>'],
             ['<?php if($a){}'],
             ['<?php while($a > $b){}'],
             [
@@ -90,6 +95,9 @@ A is equal to 5
             static::markTestSkipped('The short_open_tag option is required to be enabled.');
         }
 
-        $this->doTest("<?= '1_'; ?>", "<?= '1_' ?>");
+        $this->doTest(
+            "<?= '1_'; ?> <?php ?><?= 1; ?>",
+            "<?= '1_' ?> <?php ?><?= 1; ?>"
+        );
     }
 }

+ 4 - 0
tests/Fixer/Whitespace/SingleBlankLineAtEofFixerTest.php

@@ -140,6 +140,10 @@ inline 1
                 "<?php return true;\n/*\nA comment\n*/\n",
                 "<?php return true;\n/*\nA comment\n*/\n\n",
             ],
+            [
+                "<?= 1;\n",
+                '<?= 1;',
+            ],
         ];
     }