Browse Source

SwitchContinueToBreakFixer - Fix candidate check

SpacePossum 3 years ago
parent
commit
3247c37c86

+ 1 - 1
src/Fixer/ControlStructure/NoBreakCommentFixer.php

@@ -78,7 +78,7 @@ switch ($foo) {
      */
     public function isCandidate(Tokens $tokens): bool
     {
-        return $tokens->isAnyTokenKindsFound([T_CASE, T_DEFAULT]);
+        return $tokens->isTokenKindFound(T_SWITCH);
     }
 
     /**

+ 3 - 3
src/Fixer/ControlStructure/SwitchCaseSemicolonToColonFixer.php

@@ -63,7 +63,7 @@ final class SwitchCaseSemicolonToColonFixer extends AbstractFixer
      */
     public function isCandidate(Tokens $tokens): bool
     {
-        return $tokens->isAnyTokenKindsFound([T_CASE, T_DEFAULT]);
+        return $tokens->isTokenKindFound(T_SWITCH);
     }
 
     /**
@@ -81,7 +81,7 @@ final class SwitchCaseSemicolonToColonFixer extends AbstractFixer
         }
     }
 
-    protected function fixSwitchCase(Tokens $tokens, int $index): void
+    private function fixSwitchCase(Tokens $tokens, int $index): void
     {
         $ternariesCount = 0;
         do {
@@ -112,7 +112,7 @@ final class SwitchCaseSemicolonToColonFixer extends AbstractFixer
         }
     }
 
-    protected function fixSwitchDefault(Tokens $tokens, int $index): void
+    private function fixSwitchDefault(Tokens $tokens, int $index): void
     {
         do {
             if ($tokens[$index]->equalsAny([':', ';', [T_DOUBLE_ARROW]])) {

+ 1 - 1
src/Fixer/ControlStructure/SwitchCaseSpaceFixer.php

@@ -54,7 +54,7 @@ final class SwitchCaseSpaceFixer extends AbstractFixer
      */
     public function isCandidate(Tokens $tokens): bool
     {
-        return $tokens->isAnyTokenKindsFound([T_CASE, T_DEFAULT]);
+        return $tokens->isTokenKindFound(T_SWITCH);
     }
 
     /**

+ 1 - 1
src/Fixer/ControlStructure/SwitchContinueToBreakFixer.php

@@ -79,7 +79,7 @@ switch ($foo) {
      */
     public function isCandidate(Tokens $tokens): bool
     {
-        return $tokens->isAllTokenKindsFound([T_SWITCH, T_CONTINUE, T_LNUMBER]) && !$tokens->hasAlternativeSyntax();
+        return $tokens->isAllTokenKindsFound([T_SWITCH, T_CONTINUE]) && !$tokens->hasAlternativeSyntax();
     }
 
     /**

+ 6 - 0
tests/Fixer/ControlStructure/NoBreakCommentFixerTest.php

@@ -1242,6 +1242,12 @@ enum UserStatus: string {
   case Suspended = \'S\';
   case CanceledByUser = \'C\';
 }
+
+switch($a) { // pass the `is candidate` check
+    case 1:
+        echo 1;
+        break;
+}
 ',
         ];
     }

+ 17 - 0
tests/Fixer/ControlStructure/SwitchContinueToBreakFixerTest.php

@@ -491,6 +491,23 @@ switch($a) {
 ',
             ];
         }
+
+        yield [
+            '<?php
+            switch($a) {
+                case "a":
+                    echo __FILE__;
+                    break;
+            }
+            ',
+            '<?php
+            switch($a) {
+                case "a":
+                    echo __FILE__;
+                    continue;
+            }
+            ',
+        ];
     }
 
     /**

+ 19 - 0
tests/Fixer/ControlStructure/YodaStyleFixerTest.php

@@ -712,6 +712,25 @@ function a() {
                 '<?php function test() {return yield $a === 1;};',
             ],
         ];
+
+        yield [
+            '<?php
+$a = 1;
+switch ($a) {
+    case 1 === $a:
+        echo 123;
+        break;
+}
+',
+            '<?php
+$a = 1;
+switch ($a) {
+    case $a === 1:
+        echo 123;
+        break;
+}
+',
+        ];
     }
 
     /**

+ 17 - 2
tests/Fixer/Whitespace/BlankLineBeforeStatementFixerTest.php

@@ -184,15 +184,20 @@ switch ($a) {
     case 1:
         return 1;
 
-    case 2:
+    case 2;
         return 2;
+
+    case 3:
+        return 3;
 }',
                 '<?php
 switch ($a) {
     case 1:
         return 1;
-    case 2:
+    case 2;
         return 2;
+    case 3:
+        return 3;
 }',
             ],
         ];
@@ -350,6 +355,11 @@ switch ($a) {
 
     default:
         return 2;
+}
+
+switch ($a1) {
+    default:
+        return 22;
 }',
                 '<?php
 switch ($a) {
@@ -357,6 +367,11 @@ switch ($a) {
         return 1;
     default:
         return 2;
+}
+
+switch ($a1) {
+    default:
+        return 22;
 }',
             ],
         ];

+ 2 - 2
tests/Tokenizer/Analyzer/ReferenceAnalyzerTest.php

@@ -75,7 +75,7 @@ class Foo {
         yield ['<?php foreach($foos as &$foo) {}'];
         yield ['<?php foreach($foos as $key => &$foo) {}'];
 
-        if (PHP_VERSION >= 70100) {
+        if (\PHP_VERSION_ID >= 70100) {
             yield ['<?php function foo(?int &$bar) {};'];
         }
     }
@@ -113,7 +113,7 @@ class Foo {
         yield ['<?php foreach($foos as $foo) { $foo & $bar; }'];
         yield ['<?php if ($foo instanceof Bar & 0b01010101) {}'];
 
-        if (PHP_VERSION >= 70100) {
+        if (\PHP_VERSION_ID >= 70100) {
             yield ['<?php function foo(?int $bar = BAZ & QUX) {};'];
         }
     }

+ 2 - 0
tests/Tokenizer/Transformer/FirstClassCallableTransformerTest.php

@@ -90,6 +90,8 @@ $obj->$methodStr(  /* */  ... /* */  );
                 foo(...$args, named: $arg);
                 foo(named: $arg, ...$args);
                 foo(...$nulls);
+                $fruits = ["banana", "orange", ...$parts, "watermelon"];
+                $a = [...$array1, ...$array2];
             ',
         ];
     }