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

PHP 8: Adds support for constructor property promotion #74

Merged
merged 1 commit into from
May 13, 2021

Conversation

cfroystad
Copy link
Collaborator

@cfroystad cfroystad commented Apr 29, 2021

RFC
Documentation

Introduces support for constructor property promotion. Initially, I considered adding a new node type for the constructor, but PHP will support using the class name as a constructor name until PHP 9, which makes that difficult/impossible. Thus, the broader support for parsing property promotions even in methods/functions where this is not valid.

Useful facts:

  • Not all parameters needs to be promoted
  • Promoted parameters may occur anywhere in the parameter list
  • A promoted parameter MUST always have a visibility modifier
  • Methods with name equal to the class name is a valid (but deprecated) constructor in the global namespace until PHP 9

For consideration:

Simplifications/judgement calls. Let me know if you'd like me to change any of these:

  1. Property promotion will never be valid in a function, but only in a method. However, the two share a lot of code in the parser. Thus, I've opted to allow the parsing, but defer the handling of this to any intelligence using the parser.
  2. Callable is not a valid type for property promotion. Any handling of this has, for simplicity, been defered to any intelligence using the parser.

New nodes:

  • property_promotion_parameter

Example:

class Point {
    public function __construct(
        public float $x = 0.0,
        float $y = 0.0,
        private float $z = 0.0
    ) {}
}

Parses into:

(class_declaration
  name: (name)
  body: (declaration_list
    (method_declaration
      (visibility_modifier)
      name: (name)
      parameters: (formal_parameters
        (property_promotion_parameter
          visibility: (visibility_modifier)
          type: (type_list
            (primitive_type)
          )
          name: (variable_name (name))
          default_value: (float)
        )
        (simple_parameter
          type: (type_list (primitive_type))
          name: (variable_name (name))
          default_value: (float)
        )
        (property_promotion_parameter
          visibility: (visibility_modifier)
          type: (type_list (primitive_type))
          name: (variable_name (name))
          default_value: (float)
        )
      )
      body: (compound_statement)
    )
  )

NOTE: If this adds another method parameter type, thus either this PR or #69 will need adaptation depending on which is merged first.

Checklist:

  • All tests pass in CI.
  • There are sufficient tests for the new fix/feature (see tests in expressions.txt)
  • Grammar rules have not been renamed unless absolutely necessary (0 rules renamed)
  • The conflicts section hasn't grown too much (0 new conflicts)
  • The parser size hasn't grown too much (master: 1664 , PR: 1672)

@cfroystad cfroystad mentioned this pull request Apr 29, 2021
32 tasks
@cfroystad cfroystad force-pushed the constructor_promotion branch from 03b931f to 36587cf Compare May 13, 2021 08:59
@cfroystad cfroystad merged commit fad507b into tree-sitter:master May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant