Skip to content

Conversation

@kukulich
Copy link
Contributor

Based on #3478

@kukulich kukulich mentioned this pull request Nov 20, 2021
8 tasks
@kukulich kukulich force-pushed the php81-enum-case branch 2 times, most recently from a96c894 to ae3617b Compare November 20, 2021 22:06
Copy link
Contributor

@jrfnl jrfnl left a 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
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Member

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:
}
Copy link
Contributor

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.

@gsherwood gsherwood added this to the 3.7.0 milestone Nov 21, 2021
@kukulich kukulich force-pushed the php81-enum-case branch 3 times, most recently from c26d7b3 to 39d34c5 Compare December 17, 2021 08:21
@kukulich
Copy link
Contributor Author

Rebased and ready for review.

@kukulich kukulich marked this pull request as ready for review January 17, 2022 09:03
gsherwood added a commit that referenced this pull request Mar 7, 2022
@gsherwood gsherwood merged commit 0e060e0 into squizlabs:master Mar 7, 2022
@gsherwood
Copy link
Member

Thanks a lot for this PR, and for keeping it rebased.

I've merged it in without the performance improvements that come from using Tokenizer::recurseScopeMap(). If real-world performance suffers due to this change, I can merge that in.

@kukulich kukulich deleted the php81-enum-case branch April 19, 2022 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants