-
-
Notifications
You must be signed in to change notification settings - Fork 523
Description
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.