-
-
Notifications
You must be signed in to change notification settings - Fork 495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new sniff to check whether a hook name is valid. #599
Add new sniff to check whether a hook name is valid. #599
Conversation
Slight aside from here, but maybe worth adding as a valid unit test to check the new sniff doesn't complain, is hook names with |
@GaryJones That would throw a warning based on the below phrase in the rule as stated in the handbook:
I choose |
@westonruter Same goes there - it would throw a warning, not an error. |
09bbb45
to
0a7da58
Compare
Rebased to force a travis check against the new WPCS codestyle rules. |
It's not so much a separate word, as a prefix. |
I wonder if core is actually strict about this rule. Personally I like to use a hyphen or something other than an underscore in dynamic hook names to prevent potential conflicts: // These could conflict unexpectedly:
do_action( "prefix_{$var}" );
do_action( 'prefix_something' );
// This prevents that:
do_action( "prefix-{$var}" ); But if core is going to be strict about this, then I guess we do need to add this check to |
I had a look at this hook database when I created the sniff and based on visual inspection, it seemed that core is strict about the lower case - only 13 exceptions out of 2000+ hook names AFAICS.
Core, however, is inconsistent about applying the second part of the rule about using All the more reason why I made a difference in error levels between the two parts. Error for non-lowercase, Warning for non-underscore punctuation. |
Just thinking - I could add an option for people to provide one-character punctuation exceptions via the What do you think ? |
New commit added - this creates the ability to provide additional allowed word delimiters via Example usage: <rule ref="WordPress.NamingConventions.ValidHookName">
<properties>
<property name="additionalWordDelimiters" value="-"/>
</properties>
</rule> Commit includes unit tests. I've still left the error level for the punctuation at |
80 => 1, | ||
81 => 1, | ||
); | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This break
, and the break
below are redundant (dead code).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Artifact left from upstream code style. Removed.
Covers the `action` part of this rule in the handbook: > Use lowercase letters in variable, **action**, and function names (never camelCase). Separate words via underscores. Ref: https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#naming-conventions Review appreciated. Also: I've made non-lowercase names an `error` and non-underscore punctuation a `warning`. Should that also be an `error` ?
That particular test caused the unit tests to fail on PHP 5.2 and it was only included to ensure that the sniff would *not* run on namespaced functions.
Creates the ability to provide additional allowed word delimiters via `phpcs.xml` or via the inline `@codingStandardsChangeSetting` directive. Syntax: ```xml <rule ref="WordPress.NamingConventions.ValidHookName"> <properties> <property name="additionalWordDelimiters" value="-"/> </properties> </rule> ``` Includes unit tests.
Also: * Add function name list for the other hook functions to the `WordPress_Sniff` class as well. * Ignore deprecated hook names for the purposes of the valid hook name sniff + add unit test verifying that.
ec817f4
to
8ad78ce
Compare
Rebased to solve merge conflict. |
Anyone got anymore feedback on this ? |
Covers the
action
part of this rule in the handbook:Ref: https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#naming-conventions
Review appreciated.
Also: I've made non-lowercase hook names an
error
and non-underscore punctuation awarning
. Should that also be anerror
?