Skip to content

PHP 7.4: arrow functions #2523

Closed
Closed
@jrfnl

Description

@jrfnl

PHP 7.4 will contain the ability to declare arrow functions (short, one statement closures). This RFC has already been implemented in the PHP 7.4 branch.

Arrow functions will make code sniffing more complicated as the T_DOUBLE_ARROW token is now also used for those, quite apart from these functions being declared without "scope"/curly braces, making it interesting to determine the end of a statement, particularly when these would be used within arrays, ternaries or even in nested arrow functions.

I imagine, it may even be desirable to assign a different token to the T_DOUBLE_ARROW when used for arrow functions.

Based on the RFC and the implementation details, I foresee that - at least - the following changes will need to be made:

  • Back-fill the new T_FN token for PHP < 7.4.
  • Add T_FN to the condition related to anonymous structures in the File::getDeclarationName() method.
  • Allow for T_FN in the File::getMethodParameters() method.
  • Allow for T_FN in the File::getMethodProperties() method.
  • Register T_FN with various sniffs which sniff function declarations.
  • Any sniff/function relying on the T_DOUBLE_ARROW only being used for array key/value separation will also need adjusting (depending on whether or not a different token will be used or not).

Userland external standards will also need to be aware of this and will need to make similar changes in their sniffs for the last two action points.

There are a lot of code examples available in the RFC, but IMO, more will be needed to test the above identified issues around arrow functions in arrays, in ternaries and nested arrow functions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions