- Issue created by @dww
-
klausi β
authored 338469ee on 8.3.x
feat(ValidEnumCase): Add UpperCamel name sniff for enum cases (#3492126...
-
klausi β
authored 338469ee on 8.3.x
- π¦πΉAustria klausi π¦πΉ Vienna
Makes sense!
I introduced a new ValidEnumCase sniff in https://github.com/pfrenssen/coder/pull/254
I applied the same rules as for class names.
1) Must start with capital letter
2) Must not contain underscores.Other UpperCamelCase checking can be problematic, sometimes developers want to use abbreviations in the name. For example "FirstJSONCase". I think Symfony prefers "FirstJsonCase", but we don't enforce that in Coder (yet?).
I'm open to enforce that, can be a follow-up.
No fixer has been implemented as that could break working code (Coder cannot update all references to a changed name). Same as for class names.
- π¦πΉAustria klausi π¦πΉ Vienna
One small improvement we can do is check if there are only upper case characters in the case/class name. That will still allow "FirstJSONCase" but will forbid "FIRSTJSONCASE". We can also apply that to class/interface names.
- π¦πΉAustria klausi π¦πΉ Vienna
Oh hm, turns out we don't want "FirstJSONClassName" already since 2013!!!! https://www.drupal.org/docs/develop/standards/php/php-coding-standards#s... β
Started work in https://github.com/pfrenssen/coder/pull/255
But I think we need to spin this out into a dedicated issue as this will check a lot more stuff and might surprise developers.
- π¦πΉAustria klausi π¦πΉ Vienna
- π§πͺBelgium kristiaanvandeneynde Antwerp, Belgium
Other UpperCamelCase checking can be problematic, sometimes developers want to use abbreviations in the name. For example "FirstJSONCase". I think Symfony prefers "FirstJsonCase", but we don't enforce that in Coder (yet?).
Are you sure about that? I recall trying to add methods like FirstJSONCase and getting scolded at by GitLab CI, telling me to turn it into FirstJsonCase. Not entirely sure myself, to be honest, just vaguely remembering this.
- π¦πΉAustria klausi π¦πΉ Vienna
Yep I'm sure at least for Coder where we don't show an error for "FirstJSONCase" - I created π Enforce UpperCamel naming for class, enum, interface, trait + forbid upper case acronyms Active to discuss if we want to enforce it in the future (quite a few breaking changes for Drupal core).
- π·π΄Romania claudiu.cristea Arad π·π΄
This just broke CAS module https://git.drupalcode.org/project/cas/-/jobs/3943973
- π¦πΉAustria klausi π¦πΉ Vienna
The sniff is correct, you need to write "Http" and "Https" for you case statements (UpperCamel naming). That helps not confusing them with constants.
Or you can make "phpcs:ignore" comments to suppress the error.
- π·π΄Romania claudiu.cristea Arad π·π΄
Of course, but the change didn't account for existing cases out in the wild
- π¦πΉAustria klausi π¦πΉ Vienna
That is expected when we make new sniffs, they will find problems that were not reported before. Sot it did not break your enum, it found your broken enum :-)