`NamingConventions/ValidVariableName`: how should promoted properties be handled?
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
VariableNotSnakeCaseerror code (notPropertyNotSnakeCase) - Are checked against the
$wordpress_mixed_case_varsallow 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 thePropertyNotSnakeCaseerror code and the$allowed_mixed_case_member_var_namesallow list)? - As a function parameter (keeping current behavior: processed by
processVariable()using theVariableNotSnakeCaseerror code and the$wordpress_mixed_case_varsallow 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
- 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.
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.
See my reply in the other issue as it also applies here: https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1302#issuecomment-3482906117