-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
PHP 8.1: Added enum
support to sniffs
#3482
Conversation
enum
support to valid sniffsenum
support to sniffs
6d83d31
to
7fa55f9
Compare
8f70e33
to
22375be
Compare
Rebased on master and ready for review. |
First, this is terrific. Thank you. Curious as to any opinions on treating enums with the exact same sniff rules as classes, especially PSR2 and PSR12 standards. I'm in 2 minds. Given they are basically classes, treating them in the same way makes sense to me. But the PSR standards obviously don't mention enums, so it's quite possible someone reports a bug that PHPCS is enforcing rules that are not stated in the PSR. I think there is an argument to say they are implied in the PSR, given the line :
and the contents of the RFC itself:
But keen for other opinions. |
Just my two cents: FYI: I'm hoping to review most of the work in this PR tomorrow (if still needed by then). |
@kukulich I've started reviewing - shall I pull a PR with various tweaks to your branch to get through the small stuff ? |
@jrfnl Feel free :) |
PR kukulich#1 has been pulled with some small improvements.
That concludes part 1 of the review (= review current fixes). For part 2, I'm now going to look at an additional set of results from token searches to see if any of the additional sniffs flagged need tests/sniff fixes for enum support. |
Not 100% done yet, but here are my preliminary findings: Additional sniffs for which tests/support should be added (I've prepared the commits for a few already):
Note sure about the |
fadddfc
to
aadc30c
Compare
Rebased and green. |
I'm not sure if this is the right place to mention this, so apologies if not. If I have an enum with some methods, like this: <?php declare(strict_types = 1);
namespace Model\Language;
enum Language: string
{
case CS = 'cs';
case EN = 'en';
/** @return string[] */
private static function getNames(): array
{
return [
self::CS->value => 'language.name.cs',
self::EN->value => 'language.name.en',
];
}
} Then firstly
|
* Test case file 1 should contain both a duplicate and a non-duplicate enum. * Test case file 2 should contain an enum which was already declared in file 1 to verify cross-file detection.
Ensure that all aspects of the sniff are tested for enums. While these tests pass, if you look at the actual error message output, you will see some weirdness (declaration name for the `enum` set as `string`). This should be fixed once PR 3534 has been merged.
Ensure that all aspects of the sniff are tested for enums. While these tests pass, if you look at the actual error message output, you will see some weirdness (declaration name for the `enum` set as `string`). This should be fixed once PR 3534 has been merged.
Ensure that backed enum signatures are included in the tests. Note: the adjusted tests _without a space between the enum name and the colon_ will fail until PR 3534 has been merged.
Test more aspects of the sniff are applied correctly to enums.
The enum test was placed _below_ a test which was marked as testing a specific situation at the end of a file, which invalidated the "end of file" test. Fixed by moving the new test up.
Add a _valid_ test case for a backed enum. Note: this new test will fail until PR 3534 has been merged.
Let's also test the implements keyword when used with an enum.
Ensure that backed enum signatures are included in the tests. As this sniff retrieves the class name itself instead of using the `File::getDeclarationName()` method, these tests _will_ find the name correctly (not mistake the type for the name), but _may_ include the colon in the name as the "name end" determination checks on scope opener and whitespace and doesn't take the colon for the type declaration into account.
Only use tabs when in the test cases when they serve a specific purpose.
Let's also test that `$this` in a non-static enum method doesn't get flagged.
The test as-it-was, wasn't actually testing anything. As the sniff is about control structures, the `enum` needs to be nested in one before the test would fail without the fix,
Hello, |
|
@jrfnl @kukulich I'm keen to get a release out with at least some ENUM support. I think what's here already is great. While more support could be added, I'd like to know if either of you see any blockers to getting this merged in. I know I'm not going to have time to work on any of the additional sniff changes, but I can at least get this merged and released to partial support is available. |
I would be happy with release with partial enum support :) |
Happy for this to be merged. I could pull the extra changes which I worked on already either to this PR or separately, whichever is preferred. Not all sniffs having support is IMO not a blocker for the release. However, I do think it would be good if the below three from my previous comment got sorted before the release:
Other than that for a release to go out, please be aware that IMO supporting intersection types will require the introduction of a new token (similar to what was done for union types), so that would mean that support for intersection types would need to wait until |
Great! We are really looking forward to this! Can we expect new version this week? |
Almost 5 months since the release of PHP 8.1 Enums - lets merge and release! |
This is now merged. Thanks all. |
Based on #3478