-
-
Notifications
You must be signed in to change notification settings - Fork 91
Tokens: add CLASS_MODIFIERS and PROPERTY_MODIFIERS constants #1359
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
Tokens: add CLASS_MODIFIERS and PROPERTY_MODIFIERS constants #1359
Conversation
Several core sniffs and many custom sniffs rely heavily on the Tokens class to simplify code, particularly as new features (like readonly) are added to PHP. This change adds class and property modifier constants and implements those constants in sniffs that were already hardcoding those collections of values. Semantically, these constants also do a better job of communicating what is being searched for. In one or two cases, I decided to continue including SCOPE_MODIFIERS even though all of the tokens are currently part of the PROPERTY_MODIFIERS constant. I do not know what the future holds, so I do not want to assume that will not change in the future. There should not be any changes in behavior.
1d717e3 to
e455518
Compare
|
@jrchamp Thanks for this PR. Before I even look at this - why should this functionality be in PHP_CodeSniffer itself ? Note: this functionality is already available - and has been for years - via the PHPCSUtils tokens
|
Nor me. I'd recommend doing a backtrace using |
I think that's exactly the reason.
Thus, if we can accept that the core tool needs token constants itself, then the core tool should either provide the token constants or have a hard dependency on a library that provides the token constants. This provides reliability and consistency for Standards/Sniffs that use the tool. I feel that having competing token collection implementations is a problem, particularly when they are both maintained by the same person. I believe in you, I trust you, so I guess what I want is for you to pick a winner. That way the other option can be deprecated and people can migrate over time. With v4, there was already a breaking change from static variables to class constants. I don't know how many people have already upgraded, but sooner may be better than later if more changes will be needed. For PHPCSUtils, it's still using static variables and I'm not sure how many projects have that as a dependency. So if the PHPCSUtils collections are better, but PHP_CodeSniffer is the core tool, then I would lean towards moving the better collections into the core tool and providing temporary backwards compatibility for people who are using the constants and static variables that exist today (which mirrors what you've already done in the core tool). I hope that is helpful - I somewhat feel like I'm saying the things that you already know. You're a hero and deserve so much credit for the work that you've done. I just hope that I haven't wasted your time. ❤️ |
|
@jrchamp I appreciate your thoughts on this. PHPCSUtils was born out of a historical necessity (PHPCS itself not merging fast enough, development being stalled, resistance to adding more utility functionality to the Core) and we cannot ignore that it exists now, especially as both projects now live in the same organisation. Duplicating existing functionality from Utils in PHPCS itself will increase the maintenance burden, which, with PHPCS in its current state, is already too high. In your response you already touched upon a direction I'm exporing for the future:
So, I suggest looking at PHPCS + PHPCSUtils as a tandem tooling which - for now - are still distributed separately. |
|
@jrchamp I have opened a new issue about exploring the option to have closer ties between PHPCS and PHPCSUtils: #1364 In light of this, are you okay with using PHPCSUtils for these Token collections for now ? If so, I believe this PR should be closed until there is more clarity on what direction things will go in per the discussion to be had in #1364. |
|
Yes, thank you! Closing as discussed. ❤️ |
Description
Several core sniffs and many custom sniffs rely heavily on the Tokens class to simplify code, particularly as new features (like readonly) are added to PHP. This change adds class and property modifier constants and implements those constants in sniffs that were already hardcoding those collections of values. Semantically, these constants also do a better job of communicating what is being searched for. In one or two cases, I decided to continue including SCOPE_MODIFIERS even though all of the tokens are currently part of the PROPERTY_MODIFIERS constant. I do not know what the future holds, so I do not want to assume that will not change in the future.
There should not be any changes in behavior. PSR2.Classes.PropertyDeclaration was not including T_STATIC and it's unclear to me if that was intentional or not.
Suggested changelog entry
Tokens: add CLASS_MODIFIERS and PROPERTY_MODIFIERS constants
Related issues/external references
May help fix moodlehq/moodle-cs#213
Types of changes
PR checklist