rector icon indicating copy to clipboard operation
rector copied to clipboard

Better enum handling

Open Fabrn opened this issue 1 year ago • 0 comments

Feature Request

Hello ! While working with enums at my job, I did encounter some limitations while trying go from "fake enums" to "real enums". What I call fake enums is what we used to do in PHP 7 :

class FakeEnum
{
    public const ENUM_CASE = 'value';

    private function __construct() {}
}

And what I called a "real enum" is a pure PHP 8, which follows the RFC's recommandation for the cases' case (PascalCase) (yes that's weird) :

enum RealEnum: string
{
    case EnumCase = 'value';
}

Diff

-public const ENUM_CASE = 'value';
+case EnumCase = 'value';

Also we should be able to update our code like so :

-$isValid = MyEnum::ENUM_CASE === $value;
+$isValid = MyEnum::EnumCase->value === $value;

I don't know if it is out of scope but I think the real could also rename the class and remove the private constructor :

-class MyEnum
+enum MyEnum: string
-private function __construct() {}

I may have missed something, maybe Rector already handles everything listed above. In that case I am sorry for the fake feature request 😄 However, if that's not the case and you guys agree with my feature request, I can obviously contribute to the rule creation if needed. I am also opened to discussion obvisouly.

Thanks, Fabien

Fabrn avatar Nov 21 '24 17:11 Fabrn