Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better enum handling #8903

Open
Fabrn opened this issue Nov 21, 2024 · 0 comments
Open

Better enum handling #8903

Fabrn opened this issue Nov 21, 2024 · 0 comments
Labels

Comments

@Fabrn
Copy link

Fabrn commented Nov 21, 2024

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 Fabrn added the feature label Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant