The NamingConventions/ValidHookName sniff needs a quick review for correct handling of double backslashes.
When single quoted strings are used, the "namespace-like" prefix for hooks should be fine.
However, when double quoted strings are used, the namespace separator(s) in the hook name prefix need to be escaped as they could otherwise result in a PHP escape character in the hook name.
// Correct (single quoted).
apply_filter( 'Namespace\Like\Prefix\hookname', $var);
// Incorrect (double quoted - the `\` needs to be escaped).
apply_filter( "Namespace\Like\Prefix\hookname", $var);
// Correct (double quoted).
apply_filter( "Namespace\\Like\\Prefix\\hookname", $var);
The sniff should:
Not throw false positives when the prefix is correct, but uses escaped backslashes in a double quoted string.
Possibly throw an error when a double quoted string is used for the hook name, but the backslashes aren't escaped.
Implementation note:
This requires that the actual surrounding quotes for the text string is checked as PHPCS will tokenize double quotes strings without interpolation as single quoted strings. (T_CONSTANT_ENCAPSED_STRING versus T_DOUBLE_QUOTED_STRING).
Inspired by a question in Slack from @Djennez :
The NamingConventions/ValidHookName sniff needs a quick review for correct handling of double backslashes.
When single quoted strings are used, the "namespace-like" prefix for hooks should be fine. However, when double quoted strings are used, the namespace separator(s) in the hook name prefix need to be escaped as they could otherwise result in a PHP escape character in the hook name.
The sniff should:
Implementation note:
T_CONSTANT_ENCAPSED_STRING
versusT_DOUBLE_QUOTED_STRING
).