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

Tokenizer/PHP: bug fix for static typed properties with union/intersection types #3867

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Aug 5, 2023

Description

Just like the var keyword, the static keyword can also be used stand-alone with property declarations. https://3v4l.org/sbaDM

In that case, the tokenization of the | operator was not changed to T_TYPE_UNION and the & operator was not changed to T_TYPE_INTERSECTION as the static keyword can also be used in return type declarations, so was seen as part of the type declaration.

Fixed now by removing the T_STATIC token from the $allowed list before walking backwards from the operator.

Includes tests.

Note: this does mean that one test for the File::getMemberProperties() method needs to be changed, but as that was testing an illegal syntax anyway, I'm not concerned about making this change.

Suggested changelog entry

Tokenizer/PHP: union type and intersection type operators were not correctly tokenized as such for static typed properties without explicit visibility.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

@jrfnl jrfnl added this to the 3.8.0 milestone Aug 5, 2023
jrfnl added a commit to jrfnl/PHP_CodeSniffer that referenced this pull request Sep 3, 2023
…mprove performance

I noticed a PHPCS run on a particular code base being quite slow. Running the Performance report from PR 3810 showed the `Generic.PHP.LowerCaseConstant` sniff being the slowest sniff taking 27 seconds on a total sniff run time of 87 seconds (with a few hundred sniffs being run).

A closer look at the sniff pointed to the "skip type declarations for typed properties" part of the sniff as the culprit - in combination with the code base in question having a lot of array properties containing large arrays with mostly `true/false/null` values.

Every single one of those `true/false/null` values would trigger a condition check and then a check whether the token was in the property value, causing lots of token walking for those long arrays.

This PR should fix that by changing the logic for skipping over the property type declaration.

Instead of checking for each `true/false/null` whether it is a property type (or value), the sniff now listens to all property modifier keywords and skips over the type declaration from there, meaning that `true/false/null` found within property values will no longer need to do a conditions check/"am I a value or a type?" check.

For this particular code base, with this change, the sniff run time goes down from 27 seconds to  0.102 seconds.

Includes additional tests for the "property type skipping" code to verify it works correctly, but also that it won't cause issues with too much/the wrong things being skipped.

> Note: an additional performance boost could be gained by not recording metrics and bowing out early for any `true/false/null` which are already lowercase, but that would change the functionality of the sniff.
…ction types

Just like the `var` keyword, the `static` keyword can also be used stand-alone with property declarations.
https://3v4l.org/sbaDM

In that case, the tokenization of the `|` operator was not changed to `T_TYPE_UNION` and the `&` operator was not changed to `T_TYPE_INTERSECTION` as the `static` keyword can also be used in return type declarations, so was seen as part of the type declaration.

Fixed now by removing the `T_STATIC` token from the `$allowed` list before walking backwards from the operator.

Includes tests.

Note: this does mean that one test for the `File::getMemberProperties()` method needs to be changed, but as that was testing an illegal syntax anyway, I'm not concerned about making this change.
@jrfnl
Copy link
Contributor Author

jrfnl commented Dec 2, 2023

Closing as replaced by PHPCSStandards/PHP_CodeSniffer#78

@jrfnl jrfnl closed this Dec 2, 2023
@jrfnl jrfnl deleted the feature/tokenizer-php-bugfix-static-typed-props-union-intersection branch December 2, 2023 02:31
@jrfnl
Copy link
Contributor Author

jrfnl commented Dec 8, 2023

FYI: this fix is included in today's PHP_CodeSniffer 3.8.0 release.

As per #3932, development on PHP_CodeSniffer will continue in the PHPCSStandards/PHP_CodeSniffer repository. If you want to stay informed, you may want to start "watching" that repo (or watching releases from that repo).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant