Skip to content

AbstractVariableSniff: how should promoted properties be handled? #1302

@rodrigoprimo

Description

@rodrigoprimo

Promoted properties (introduced in PHP 8.0) are both a function parameter and an object property. This raises the question of how the AbstractVariableSniff class should handle them when finding T_VARIABLE tokens.

Current behavior

Consider the following code:

class MyClass {
    public function __construct(
        public string $promotedProperty = 'bar',
    ) {}
}

Currently, promoted properties are routed to processVariable() (not processMemberVar()). This happens because AbstractVariableSniff checks if a variable is inside a function's parameter list and treats it as "in a function", routing it to processVariable().

This means that sniffs extending AbstractVariableSniff will process promoted properties as function parameters, not as object properties.

Questions for discussion

How should sniffs extending AbstractVariableSniff handle promoted properties?

  • As an object property (via processMemberVar())?
  • As a function parameter (keeping current behavior via processVariable())?
  • Both as an object property and as a function parameter?
  • Something else (for example, introducing a new method specifically for promoted properties)?

It is important to consider that this will affect all standards that use sniffs extending AbstractVariableSniff for variable naming validation, including external standards.

Related discussions

There is a discussion about how the Squiz.NamingConventions.ValidVariableName sniff should handle promoted properties in #512, but I thought it made sense to start a broader conversation about how AbstractVariableSniff should handle it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions