-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PHP 8.1: Added T_ENUM_CASE
#3483
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
Conversation
a96c894 to
ae3617b
Compare
jrfnl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kukulich 👋🏻 I've had an initial look at this and left some comments for you to think over.
| Convert enum "case" to T_ENUM_CASE | ||
| */ | ||
|
|
||
| if ($tokenIsArray === true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels very inefficient. This basically means that for any T_CASE token, the tokenizer will do a lot of token walking to get this right, which for long switch statements with lots of cases will cause a lot of extra processing.
At the same time, I understand it makes sense to have a separate token for this as the switch T_CASE token will get scope, while this token shouldn't (similar to the problem we faced with match structures and the default keyword), which makes it neigh impossible to move the retokenization to processAdditional() as the scope setting would get skewed in that case.
I'm going to have a further think about this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels very inefficient.
Yes, but it took me couple of hours to create this "final" version that solve all situations I found.
I've just figured out one small optimisation for enum case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried this out on some very long SWITCH statements and it doesn't seem to add too much time, but that's going to change based on the complexity of each CASE block. I'm not super worried though.
An alternative place to attempt this change would be when the scope map is being created. I haven't dug into it because it's not what it's normally used for, but it should be aware we're in an ENUM and then the T_ENUM_CASE logic could be selectively applied.
The token itself could be changed here, but it might be cleaner to use this function to skip the scope map logic for T_ENUM_CASE and then use processAdditional() to switch over the tokens properly.
I'm running short on time at the moment, but I will give this a go when I get some.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did some testing and it's definitely faster doing the work in Tokenizer::recurseScopeMap(), and a less code as well. It does require adding T_CASE to the tstringContexts member var otherwise the CASE values are not converted to T_STRING using the new method.
| case CONSTANT = 1: | ||
| /* testIsNotEnumCaseIsCaseInsensitive */ | ||
| cAsE CONSTANT: | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend adding far more extensive tests for varying combinations of enum and switch, as well as covering as many situations as one can think of where case can be used and should be tokenized as T_STRING instead of T_CASE or T_ENUM_CASE.
See the test cases for the DefaultKeywordTest for inspiration.
c26d7b3 to
39d34c5
Compare
39d34c5 to
f4115be
Compare
|
Rebased and ready for review. |
|
Thanks a lot for this PR, and for keeping it rebased. I've merged it in without the performance improvements that come from using |
Based on #3478