Browse Source

PHP8.1 - Enum (start)

SpacePossum 3 years ago
parent
commit
ac37dd68e7

+ 1 - 1
doc/rules/control_structure/no_unneeded_control_parentheses.rst

@@ -12,7 +12,7 @@ Configuration
 
 List of control statements to fix.
 
-Allowed types: ``array``
+Allowed values: a subset of ``['break', 'clone', 'continue', 'echo_print', 'return', 'switch_case', 'yield', 'yield_from']``
 
 Default value: ``['break', 'clone', 'continue', 'echo_print', 'return', 'switch_case', 'yield']``
 

+ 6 - 8
src/Fixer/ControlStructure/NoBreakCommentFixer.php

@@ -122,17 +122,15 @@ switch ($foo) {
     protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
     {
         for ($index = \count($tokens) - 1; $index >= 0; --$index) {
-            if (!$tokens[$index]->isGivenKind([T_CASE, T_DEFAULT])) {
+            if ($tokens[$index]->isGivenKind(T_DEFAULT)) {
+                if ($tokens[$tokens->getNextMeaningfulToken($index)]->isGivenKind(T_DOUBLE_ARROW)) {
+                    continue; // this is "default" from "match"
+                }
+            } elseif (!$tokens[$index]->isGivenKind(T_CASE)) {
                 continue;
             }
 
-            $caseColonIndex = $tokens->getNextTokenOfKind($index, [':', ';', [T_DOUBLE_ARROW]]);
-
-            if ($tokens[$caseColonIndex]->isGivenKind(T_DOUBLE_ARROW)) {
-                continue; // this is "default" from "match"
-            }
-
-            $this->fixCase($tokens, $caseColonIndex);
+            $this->fixCase($tokens, $tokens->getNextTokenOfKind($index, [':', ';']));
         }
     }
 

+ 5 - 8
src/Fixer/ControlStructure/NoUnneededControlParenthesesFixer.php

@@ -16,6 +16,7 @@ namespace PhpCsFixer\Fixer\ControlStructure;
 
 use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\Fixer\ConfigurableFixerInterface;
+use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
@@ -44,14 +45,6 @@ final class NoUnneededControlParenthesesFixer extends AbstractFixer implements C
         'yield_from' => ['lookupTokens' => T_YIELD_FROM, 'neededSuccessors' => [';', ')']],
     ];
 
-    /**
-     * Dynamic option set on constructor.
-     */
-    public function __construct()
-    {
-        parent::__construct();
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -62,6 +55,7 @@ final class NoUnneededControlParenthesesFixer extends AbstractFixer implements C
         foreach (self::$loops as $loop) {
             $types[] = (array) $loop['lookupTokens'];
         }
+
         $types = array_merge(...$types);
 
         return $tokens->isAnyTokenKindsFound($types);
@@ -140,6 +134,7 @@ yield(2);
                     $token->equals('(') ? Tokens::BLOCK_TYPE_PARENTHESIS_BRACE : Tokens::BLOCK_TYPE_BRACE_CLASS_INSTANTIATION,
                     $blockStartIndex
                 );
+
                 $blockEndNextIndex = $tokens->getNextMeaningfulToken($blockEndIndex);
 
                 if (!$tokens[$blockEndNextIndex]->equalsAny($loop['neededSuccessors'])) {
@@ -148,6 +143,7 @@ yield(2);
 
                 if (\array_key_exists('forbiddenContents', $loop)) {
                     $forbiddenTokenIndex = $tokens->getNextTokenOfKind($blockStartIndex, $loop['forbiddenContents']);
+
                     // A forbidden token is found and is inside the parenthesis.
                     if (null !== $forbiddenTokenIndex && $forbiddenTokenIndex < $blockEndIndex) {
                         continue;
@@ -174,6 +170,7 @@ yield(2);
         return new FixerConfigurationResolver([
             (new FixerOptionBuilder('statements', 'List of control statements to fix.'))
                 ->setAllowedTypes(['array'])
+                ->setAllowedValues([new AllowedValueSubset(array_keys(self::$loops))])
                 ->setDefault([
                     'break',
                     'clone',

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

@@ -1215,4 +1215,34 @@ switch ($foo) {
             ',
         ];
     }
+
+    /**
+     * @dataProvider provideFix81Cases
+     * @requires PHP 8.1
+     */
+    public function testFix81(string $expected, ?string $input = null): void
+    {
+        $this->doTest($expected, $input);
+    }
+
+    public function provideFix81Cases(): \Generator
+    {
+        yield 'enums' => [
+            '<?php
+enum Suit {
+    case Hearts;
+    case Diamonds;
+    case Clubs;
+    case Spades;
+}
+
+enum UserStatus: string {
+  case Pending = \'P\';
+  case Active = \'A\';
+  case Suspended = \'S\';
+  case CanceledByUser = \'C\';
+}
+',
+        ];
+    }
 }

+ 31 - 0
tests/Fixer/ControlStructure/NoUnneededControlParenthesesFixerTest.php

@@ -489,6 +489,37 @@ final class NoUnneededControlParenthesesFixerTest extends AbstractFixerTestCase
         ];
     }
 
+    /**
+     * @dataProvider provideFix81Cases
+     * @requires PHP 8.1
+     */
+    public function testFix81(string $expected, ?string $input = null): void
+    {
+        $this->fixer->configure(['statements' => ['switch_case']]);
+        $this->doTest($expected, $input);
+    }
+
+    public function provideFix81Cases(): \Generator
+    {
+        yield 'enums' => [
+            '<?php
+enum Suit {
+    case Hearts ;
+case Diamonds  ;
+case Clubs ;
+case Spades   ;
+}
+
+enum UserStatus: string {
+    case    Pending = "P";
+  case  Active = "A";
+  case   Suspended = "S";
+  case CanceledByUser = "C"  ;
+}
+',
+        ];
+    }
+
     private function fixerTest(string $expected, ?string $input = null, ?string $fixStatement = null, bool $legacy = false): void
     {
         // Default config. Fixes all statements.

+ 1 - 1
tests/Fixer/LanguageConstruct/SingleSpaceAfterConstructFixerTest.php

@@ -3147,7 +3147,7 @@ ENUM Suit {
 ',
             '<?php
 ENUM     Suit {
-    case Hearts;
+    case     Hearts;
 }
 ',
         ];