Skip to content
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

Merged
merged 5 commits into from
Jul 19, 2016

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jul 14, 2016

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 hook names an error and non-underscore punctuation a warning. Should that also be an error ?

@GaryJones
Copy link
Member

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 / in them, like acf/save_post.

@jrfnl
Copy link
Member Author

jrfnl commented Jul 14, 2016

adding as a valid unit test to check the new sniff doesn't complain, is hook names with / in them

@GaryJones That would throw a warning based on the below phrase in the rule as stated in the handbook:

Separate words via underscores.

I choose warning for that part as core often doesn't comply with this rule either - they often use dashes instead of underscores - and changing hook names would break backward compatibility.

@westonruter
Copy link
Member

@jrfnl
Copy link
Member Author

jrfnl commented Jul 14, 2016

@westonruter Same goes there - it would throw a warning, not an error.

@jrfnl jrfnl force-pushed the WPCS/feature/lowercase-filter-names branch from 09bbb45 to 0a7da58 Compare July 14, 2016 18:08
@jrfnl
Copy link
Member Author

jrfnl commented Jul 14, 2016

Rebased to force a travis check against the new WPCS codestyle rules.

@GaryJones
Copy link
Member

It's not so much a separate word, as a prefix.

@JDGrimes
Copy link
Contributor

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 core, and if some of us don't want to use it we can silence it.

@jrfnl
Copy link
Member Author

jrfnl commented Jul 15, 2016

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:

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.
Exceptions I found were:

  • attachment_innerHTML
  • xmlrpc_call_success_blogger_deletePost up to xmlrpc_call_success_wp_newComment (12 hooks around line 2088)

Core, however, is inconsistent about applying the second part of the rule about using _ for punctuation. Dashes are most frequently used as an alternative, often even combined with underscores within the same hook name. Often you'll find this is when they refer to a function name within the hook - the 'words' within the function name are then separated with dashes while the other 'words' are separated with underscores.

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.

@jrfnl
Copy link
Member Author

jrfnl commented Jul 15, 2016

Just thinking - I could add an option for people to provide one-character punctuation exceptions via the phpcs.xml config.

What do you think ?

@jrfnl
Copy link
Member Author

jrfnl commented Jul 15, 2016

New commit added - this creates the ability to provide additional allowed word delimiters via phpcs.xml or via the inline @codingStandardsChangeSetting directive.

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 warning or should this now be raised to error ?

80 => 1,
81 => 1,
);
break;
Copy link
Member

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).

Copy link
Member Author

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.

jrfnl added 5 commits July 17, 2016 20:39
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.
@jrfnl jrfnl force-pushed the WPCS/feature/lowercase-filter-names branch from ec817f4 to 8ad78ce Compare July 17, 2016 18:39
@jrfnl
Copy link
Member Author

jrfnl commented Jul 17, 2016

Rebased to solve merge conflict.

@jrfnl
Copy link
Member Author

jrfnl commented Jul 19, 2016

Anyone got anymore feedback on this ?

@JDGrimes JDGrimes added this to the 0.10.0 milestone Jul 19, 2016
@JDGrimes JDGrimes merged commit 1f76327 into WordPress:develop Jul 19, 2016
@jrfnl jrfnl deleted the WPCS/feature/lowercase-filter-names branch July 19, 2016 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants