Skip to content

NamingConventions/ValidVariableName: how should promoted properties be handled? #2639

@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 WordPress.NamingConventions.ValidVariableName sniff should classify and validate them.

For example, consider the following code:

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

Current behavior

Currently, promoted constructor properties are processed by processVariable() (not processMemberVar()). This means they:

  • Use the VariableNotSnakeCase error code (not PropertyNotSnakeCase)
  • Are checked against the $wordpress_mixed_case_vars allow list (not $allowed_mixed_case_member_var_names)

This behavior is inherited from AbstractVariableSniff, which treats variables inside function parameter lists as "in a function" and routes them to processVariable().

Questions for discussion

How should the sniff treat promoted properties?

  • As an object property (processed by processMemberVar() using the PropertyNotSnakeCase error code and the $allowed_mixed_case_member_var_names allow list)?
  • As a function parameter (keeping current behavior: processed by processVariable() using the VariableNotSnakeCase error code and the $wordpress_mixed_case_vars allow list)?
  • Both as an object property and as a function parameter?
  • Something else?

While both object properties and function parameters follow snake_case in WordPress, this distinction matters for error codes and allow lists. And potentially other cases that I'm missing.

Additional considerations

  1. Are there other sniffs in WPCS that might be affected by this?

Related discussions

I opened an issue in the PHPCS repository to discuss how the AbstractVariableSniff class should handle promoted properties.

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