Repository: PHP-CS-Fixer/PHP-CS-Fixer (PHP)

Issue:
If you use Null as an name in Enum php fixer tranform it to lowerr Case
## Bug report

If you use Null as an name in Enum php fixer tranform it to lowerr Case

### Code snippet that reproduces the problem

  ```php
<?php

declare(strict_types=1);

namespace XXX\XX\Enum;

enum SerializeDataType: string
{
    case Null = 'null';
    case Structure = 'Structure';
    case Array = 'array';
    case Enum = 'enum';
    case Default = 'default';
}
  ```
will be transformed to

  ```php
<?php

declare(strict_types=1);

namespace XXX\XX\Enum;

enum SerializeDataType: string
{
    case null = 'null';   // <-- Null => null
    case Structure = 'Structure';
    case Array = 'array';
    case Enum = 'enum';
    case Default = 'default';
}
  ```





Discussion:
Thanks for reporting,
I think this will be addressed in https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/6367 , if you can please try out that code and let me know.



Patch:
diff --git a/src/Fixer/Casing/ConstantCaseFixer.php b/src/Fixer/Casing/ConstantCaseFixer.php
index 5b71c649d14..b4fa37e0279 100644
--- a/src/Fixer/Casing/ConstantCaseFixer.php
+++ b/src/Fixer/Casing/ConstantCaseFixer.php
@@ -110,6 +110,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
             if (
                 $this->isNeighbourAccepted($tokens, $tokens->getPrevMeaningfulToken($index))
                 && $this->isNeighbourAccepted($tokens, $tokens->getNextMeaningfulToken($index))
+                && !$this->isEnumCaseName($tokens, $index)
             ) {
                 $tokens[$index] = new Token([$token->getId(), $fixFunction($token->getContent())]);
             }
@@ -151,4 +152,25 @@ private function isNeighbourAccepted(Tokens $tokens, int $index): bool
 
         return !$token->isGivenKind($forbiddenTokens);
     }
+
+    private function isEnumCaseName(Tokens $tokens, int $index): bool
+    {
+        if (!\defined('T_ENUM') || !$tokens->isTokenKindFound(T_ENUM)) { // @TODO: drop condition when PHP 8.1+ is required
+            return false;
+        }
+
+        $prevIndex = $tokens->getPrevMeaningfulToken($index);
+
+        if (null === $prevIndex || !$tokens[$prevIndex]->isGivenKind(T_CASE)) {
+            return false;
+        }
+
+        if (!$tokens->isTokenKindFound(T_SWITCH)) {
+            return true;
+        }
+
+        $prevIndex = $tokens->getPrevTokenOfKind($prevIndex, [[T_ENUM], [T_SWITCH]]);
+
+        return null !== $prevIndex && $tokens[$prevIndex]->isGivenKind(T_ENUM);
+    }
 }


Test patch:
diff --git a/tests/Fixer/Casing/ConstantCaseFixerTest.php b/tests/Fixer/Casing/ConstantCaseFixerTest.php
index 23c1c787e86..e5a2f968623 100644
--- a/tests/Fixer/Casing/ConstantCaseFixerTest.php
+++ b/tests/Fixer/Casing/ConstantCaseFixerTest.php
@@ -25,70 +25,6 @@
  */
 final class ConstantCaseFixerTest extends AbstractFixerTestCase
 {
-    /**
-     * @dataProvider provideLowerGeneratedCases
-     */
-    public function testFixLowerGeneratedCases(string $expected, ?string $input = null): void
-    {
-        $this->fixer->configure(['case' => 'lower']);
-        $this->doTest($expected, $input);
-    }
-
-    /**
-     * @dataProvider provideUpperGeneratedCases
-     */
-    public function testFixUpperGeneratedCases(string $expected, ?string $input = null): void
-    {
-        $this->fixer->configure(['case' => 'upper']);
-        $this->doTest($expected, $input);
-    }
-
-    public function provideLowerGeneratedCases(): \Generator
-    {
-        foreach (['true', 'false', 'null'] as $case) {
-            yield [
-                sprintf('<?php $x = %s;', $case),
-                sprintf('<?php $x = %s;', strtoupper($case)),
-            ];
-
-            yield [
-                sprintf('<?php $x = %s;', $case),
-                sprintf('<?php $x = %s;', ucfirst($case)),
-            ];
-
-            yield [sprintf('<?php $x = new %s;', ucfirst($case))];
-
-            yield [sprintf('<?php $x = new %s;', strtoupper($case))];
-
-            yield [sprintf('<?php $x = "%s story";', $case)];
-
-            yield [sprintf('<?php $x = "%s";', $case)];
-        }
-    }
-
-    public function provideUpperGeneratedCases(): \Generator
-    {
-        foreach (['true', 'false', 'null'] as $case) {
-            yield [
-                sprintf('<?php $x = %s;', strtoupper($case)),
-                sprintf('<?php $x = %s;', $case),
-            ];
-
-            yield [
-                sprintf('<?php $x = %s;', strtoupper($case)),
-                sprintf('<?php $x = %s;', ucfirst($case)),
-            ];
-
-            yield [sprintf('<?php $x = new %s;', ucfirst($case))];
-
-            yield [sprintf('<?php $x = new %s;', strtoupper($case))];
-
-            yield [sprintf('<?php $x = "%s story";', $case)];
-
-            yield [sprintf('<?php $x = "%s";', $case)];
-        }
-    }
-
     /**
      * @dataProvider provideFixCases
      */
@@ -162,6 +98,70 @@ class Foo
         ];
     }
 
+    /**
+     * @dataProvider provideLowerGeneratedCases
+     */
+    public function testFixLowerGeneratedCases(string $expected, ?string $input = null): void
+    {
+        $this->fixer->configure(['case' => 'lower']);
+        $this->doTest($expected, $input);
+    }
+
+    public function provideLowerGeneratedCases(): iterable
+    {
+        foreach (['true', 'false', 'null'] as $case) {
+            yield [
+                sprintf('<?php $x = %s;', $case),
+                sprintf('<?php $x = %s;', strtoupper($case)),
+            ];
+
+            yield [
+                sprintf('<?php $x = %s;', $case),
+                sprintf('<?php $x = %s;', ucfirst($case)),
+            ];
+
+            yield [sprintf('<?php $x = new %s;', ucfirst($case))];
+
+            yield [sprintf('<?php $x = new %s;', strtoupper($case))];
+
+            yield [sprintf('<?php $x = "%s story";', $case)];
+
+            yield [sprintf('<?php $x = "%s";', $case)];
+        }
+    }
+
+    /**
+     * @dataProvider provideUpperGeneratedCases
+     */
+    public function testFixUpperGeneratedCases(string $expected, ?string $input = null): void
+    {
+        $this->fixer->configure(['case' => 'upper']);
+        $this->doTest($expected, $input);
+    }
+
+    public function provideUpperGeneratedCases(): iterable
+    {
+        foreach (['true', 'false', 'null'] as $case) {
+            yield [
+                sprintf('<?php $x = %s;', strtoupper($case)),
+                sprintf('<?php $x = %s;', $case),
+            ];
+
+            yield [
+                sprintf('<?php $x = %s;', strtoupper($case)),
+                sprintf('<?php $x = %s;', ucfirst($case)),
+            ];
+
+            yield [sprintf('<?php $x = new %s;', ucfirst($case))];
+
+            yield [sprintf('<?php $x = new %s;', strtoupper($case))];
+
+            yield [sprintf('<?php $x = "%s story";', $case)];
+
+            yield [sprintf('<?php $x = "%s";', $case)];
+        }
+    }
+
     /**
      * @dataProvider provideFix80Cases
      * @requires PHP 8.0
@@ -171,11 +171,9 @@ public function testFix80(string $expected, ?string $input = null): void
         $this->doTest($expected, $input);
     }
 
-    public static function provideFix80Cases(): array
+    public static function provideFix80Cases(): iterable
     {
-        return [
-            ['<?php class Foo { public function Bar() { return $this?->False; } }'],
-        ];
+        yield ' nullsafe operator' => ['<?php class Foo { public function Bar() { return $this?->False; } }'];
     }
 
     /**
@@ -187,9 +185,9 @@ public function testFix81(string $expected, ?string $input = null): void
         $this->doTest($expected, $input);
     }
 
-    public static function provideFix81Cases(): \Generator
+    public static function provideFix81Cases(): iterable
     {
-        yield [
+        yield 'final class const' => [
             '<?php
                 class Foo
                 {
@@ -207,5 +205,57 @@ class Foo
                 }
             ',
         ];
+
+        yield 'enum and switch' => [
+            '<?php
+                enum Foo
+                {
+                    case True;
+                    case False;
+                    case Null;
+
+                    public function methodWithSwitch(mixed $value): void
+                    {
+                        switch ($value) {
+                            case true:
+                            case false:
+                            case null:
+                                break;
+                        }
+                    }
+                }
+            ',
+            '<?php
+                enum Foo
+                {
+                    case True;
+                    case False;
+                    case Null;
+
+                    public function methodWithSwitch(mixed $value): void
+                    {
+                        switch ($value) {
+                            case TRUE:
+                            case FALSE:
+                            case NULL:
+                                break;
+                        }
+                    }
+                }
+            ',
+        ];
+
+        yield 'enum' => [
+            '<?php
+                $y = false;
+                enum Foo: string { case FALSE = "false"; }
+                $x = true;
+            ',
+            '<?php
+                $y = FALSE;
+                enum Foo: string { case FALSE = "false"; }
+                $x = TRUE;
+            ',
+        ];
     }
 }