Skip to content

Refactor suggestion / move utility functions from File to static classes #2189

@jrfnl

Description

@jrfnl

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:

  1. Only a limited number of sniffs use the methods I have in mind for this, so having them in traits rather than in the File class makes the re-useable code more modular and easier to maintain.
  2. 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 the File class 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 called getDeclarationName()
  • FunctionDeclaration, containing:
    • getParameters(), previously called getMethodParameters()
    • getProperties(), previously called getMethodProperties()
    • and using DeclarationName for the getName() method
  • PropertyDeclaration, containing:
    • getMemberProperties()
  • ObjectDeclaration containing:
  • Generic containing:
    • 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 cover isset(), unset() and list() 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 Traits directory or, for instance, in Util/Traits or 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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions