-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Since PHPCS 3.x now has a minimum PHP requirement of PHP 5.4, I was wondering whether it might be an idea to refactor a number of groups of methods in the File class to traits.
The reasoning behind this is two-fold:
- Only a limited number of sniffs use the methods I have in mind for this, so having them in traits rather than in the
Fileclass makes the re-useable code more modular and easier to maintain. - Over the years I have written a number of additional token walking utility methods which I think belong in PHPCS rather than in the WPCS/PHPCompatibility standards in which they now live.
Traits would allow for these methods to be added to PHPCS more easily without making theFileclass even larger than it already is.
Timing
The transition could be made in PHPCS 3.x.
In PHPCS 3.x, the File class would in that case use the new traits and for the transitioned methods just call the method in the trait under the hood.
The methods in the File class would be deprecated and could be removed in PHPCS 4.x.
This would allow external standards some time to implement the new way of doing things and make the BC break in PHPCS 4.x smaller as it can already be mitigated beforehand.
Proposal details
To be concrete, I'm currently thinking of the following Traits:
DeclarationName, containing:getName(), previously calledgetDeclarationName()
FunctionDeclaration, containing:getParameters(), previously calledgetMethodParameters()getProperties(), previously calledgetMethodProperties()- and using
DeclarationNamefor thegetName()method
PropertyDeclaration, containing:getMemberProperties()
ObjectDeclarationcontaining:getClassProperties()findExtendedClassName()findImplementedInterfaceName()findExtendedInterfaceNames()- open PR File: add newFile::findExtendedInterfaceNames()utility method #2128- and using
DeclarationNamefor thegetName()method
Genericcontaining:isReference()
ScopeConditions, containing:hasCondition()getCondition()
Additionally, I'd be happy to pull the following based on battle-tested code I've written for WPCS/PHPCompatibility:
FunctionCall, containing:hasParameters()getParameterCount()getParameters()getParameter()(to get the details for the parameter at a specific position)
These methods all work on both function calls as well as for splitting an array into the individual items and could be extended to coverisset(),unset()andlist()as well.
- And if there would be any interest in it -
FQClassNames, containing:getNameFromNewToken()getNameFromDoubleColonToken()getExtendedName()getFQName()isNamespaced()determineNamespace()getDeclaredNamespaceName()
For the Generic trait, I could also offer an additional isShortList() method.
I'm also still working on the logic for a TStringAnalyser which should reliably determine whether, for instance, a T_STRING token represents a function call, the use of a global constant or for instance a use statement alias.
Open questions
What I'd currently like to know, is:
- Does the principle of this sound like a good idea for PHPCS ?
- If so, does the implementation proposal for PHPCS 3.x/4.x sound like a good idea ?
- If so, where should the traits live ? I.e. , should they be placed in a dedicated
Traitsdirectory or, for instance, inUtil/Traitsor should the directory be called something completely different ? - If so, feedback on the proposed initial Traits as outline above (trait names, function groupings etc)
- And would there be interest in the additional traits/methods as outlined above ?