WordPress-Coding-Standards icon indicating copy to clipboard operation
WordPress-Coding-Standards copied to clipboard

`NamingConventions/ValidVariableName`: how should promoted properties be handled?

Open rodrigoprimo opened this issue 2 months ago • 2 comments

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.

rodrigoprimo avatar Nov 03 '25 22:11 rodrigoprimo

Just noting that I accidentaly created this issue with the contents of a related issue that I planned to create at the same time on the PHPCS repository. I noticed the mistake and immediately edited the issue description. Sorry for the confusion.

rodrigoprimo avatar Nov 03 '25 22:11 rodrigoprimo

See my reply in the other issue as it also applies here: https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1302#issuecomment-3482906117

jrfnl avatar Nov 03 '25 22:11 jrfnl