Update PHPCS default rule set#161
Conversation
The .phpcs.xml.dist rule file is detected and implemented automatically, reducing complexity for running phpcs. The updated rules account for new wpcs sniff properties including the ability to check global prefix and i18n implementation with the correct text domain when specified. The values have been left blank so the sniffs are generic, but the rules are in place to make it easier to get started by scaffold.
|
Yes, I would indeed love feedback from @GaryJones and @jrfnl to know whether this would be a good default starting point for new plugins. |
|
@NicktheGeek For the blanks, it would be helpful to add comments so that developers immediately know where to turn the dials. |
|
@schlessera good idea. |
|
@schlessera added the comments. |
templates/.phpcs.xml.dist
Outdated
| <arg name="basepath" value="."/><!-- Strip the file paths down to the relevant bit --> | ||
| <arg name="colors" /> | ||
| <arg name="extensions" value="php"/> | ||
| <arg name="report" value="full"/> |
There was a problem hiding this comment.
Remove the reports args - the default is a full report anyway, and any different reports should be decided upon at runtime.
There was a problem hiding this comment.
FYI: I totally agree with @GaryJones' advice about the report.
There was a problem hiding this comment.
Heh - the advice is what I took from Juliette!
templates/.phpcs.xml.dist
Outdated
| @@ -0,0 +1,44 @@ | |||
| <?xml version="1.0"?> | |||
| <ruleset name="WordPress Coding Standards for Plugins"> | |||
There was a problem hiding this comment.
This naming is a bit misleading - perhaps `"WordPress Coding Standards for this plugin"? is better, and less implies that there's a WPCS specifically for Plugins?
There was a problem hiding this comment.
I'm pulling this name from the current version, but I am good to do this naming change.
There was a problem hiding this comment.
May I suggest "WPCS based custom ruleset for plugins" or "WordPress Coding Standards based custom ruleset for your plugin/for plugin xyz" ?
Makes it very clear that this is a custom ruleset and not a standard. There is a significant difference between those two and naming this right will help people find the right information if they would search for more info.
To explain the difference:
- A standard can add new sniffs, can be installed and referenced by name.
- A custom ruleset uses existing standards and sniffs and may add configuration for these.
templates/.phpcs.xml.dist
Outdated
| <arg value="sp"/> <!-- Show sniff and progress --> | ||
| <arg name="basepath" value="."/><!-- Strip the file paths down to the relevant bit --> | ||
| <arg name="colors" /> | ||
| <arg name="extensions" value="php"/> |
There was a problem hiding this comment.
Another potentially useful arg is <arg name="parallel" value="8"/> - the number can vary, but 8 is a nice starting point.
There was a problem hiding this comment.
👍 Comment to add along-side it: <!-- Enables parallel processing when available for faster results. -->
templates/.phpcs.xml.dist
Outdated
| <!-- How to scan --> | ||
| <arg value="sp"/> <!-- Show sniff and progress --> | ||
| <arg name="basepath" value="."/><!-- Strip the file paths down to the relevant bit --> | ||
| <arg name="colors" /> |
There was a problem hiding this comment.
Remove the space before the slash.
templates/.phpcs.xml.dist
Outdated
|
|
||
| <!-- How to scan --> | ||
| <arg value="sp"/> <!-- Show sniff and progress --> | ||
| <arg name="basepath" value="."/><!-- Strip the file paths down to the relevant bit --> |
There was a problem hiding this comment.
Either is fine as long as the [.]phpcs.xml.dist file is located in the project root.
templates/.phpcs.xml.dist
Outdated
|
|
||
| <!-- Rules: Check PHP version compatibility --> | ||
| <config name="testVersion" value="5.3-"/> | ||
| <rule ref="PHPCompatibility"/> |
There was a problem hiding this comment.
Since this is a WordPress plugin/theme, then it can reference the PHPCompatibilityWP ruleset instead - would need the composer.json template updated as well to pull that package in.
There was a problem hiding this comment.
Since that is outstanding, should this remain as is and implement a change for it with #63?
There was a problem hiding this comment.
I would most definitely recommend changing this already to use the PHPCompatibilityWP standard instead as it will prevent false positives for things WP back-fills/poly-fills.
templates/.phpcs.xml.dist
Outdated
| <rule ref="PHPCompatibility"/> | ||
|
|
||
| <!-- Rules: WordPress Coding Standards --> | ||
| <config name="minimum_supported_wp_version" value="3.3"/> |
There was a problem hiding this comment.
This seems particularly low. I personally use current - 2 versions (so, 4.7 right now), and WPCS default is current - 3 versions (so, 4.6 for WPCS 1.0.0).
templates/.phpcs.xml.dist
Outdated
| <!-- Rules: WordPress Coding Standards --> | ||
| <config name="minimum_supported_wp_version" value="3.3"/> | ||
| <rule ref="WordPress"> | ||
| <exclude name="WordPress.VIP"/> |
There was a problem hiding this comment.
Might be worth waiting until WPCS 1.0.0 drops, which already removes WordPress-VIP from the WordPress ruleset.
There was a problem hiding this comment.
This will work either way, even if the WPCS VIP deprecation would not make it into 1.0.0, so I'd leave it for now.
templates/.phpcs.xml.dist
Outdated
| <rule ref="WordPress.NamingConventions.PrefixAllGlobals"> | ||
| <properties> | ||
| <!-- Value: provide the function, class, and variable prefixes used. Separate multiple prefixes with a comma. --> | ||
| <property name="prefixes" type="array" value=""/> |
There was a problem hiding this comment.
@jrfnl may know better, but having a my-plugin placeholder here may better point folks towards changing it to something useful when it gives some warnings on the first run, rather than an empty value which may either blow things up, or not show any related violations (and therefore not drawing attention to changing it) at all.
There was a problem hiding this comment.
having a my-plugin placeholder here may better point folks towards changing it to something useful when it gives some warnings on the first run
I concur with @GaryJones, please do add a placeholder to give people incentive to change it.
may either blow things up, or not show any related violations
An empty value won't blow things up, but will basically disable the sniff.
templates/.phpcs.xml.dist
Outdated
| <rule ref="WordPress.WP.I18n"> | ||
| <properties> | ||
| <!-- Value: provide the text domain used. --> | ||
| <property name="text_domain" type="array" value=""/> |
There was a problem hiding this comment.
I think WPCS 1.0.0 now supports a better way of adding in array properties - individual entries, instead of a comma-separated value.
There was a problem hiding this comment.
Not WPCS, but PHPCS 3.3.0+. So if you would want to use the new array format, you would need to make sure that people are aware that they will need a very recent PHPCS version.
There was a problem hiding this comment.
@jrfnl will the current CSV method work with PHPCS 3.3.0+? If so it seams like it might be better to wait before implementing that even by reference.
|
Rushing the review, and @jrfnl can fill in some blanks (or I can another day), but overall +1 for the PR to make it a much nicer starting point to implementing WPCS. Thanks @NicktheGeek! |
|
thanks you @GaryJones after you helped me get this on GFWA I've used it on several projects and I can't tell you the number of times it has found a minor typo in the text domain and saved me so much headache down the road. That one feature is worth the price of admission. |
change the the ruleset name and add a comment explaining parellel processing.
|
@jrfnl Thanks! I've added the changes you recommended. @schlessera this may need some additional feedback from @jrfnl or @GaryJones but so far I've implemented almost everything suggested. There are a couple of comments regarding WPCS 1.0.0 but IMO we are better if this is implemented now and an issue created to update when 1.0.0 comes out with additional improvements. I'm good either way though pending release timeline for WP CLI 2.0 and WPCS 1.0.0. |
jrfnl
left a comment
There was a problem hiding this comment.
@schlessera Thanks for the ping. @NicktheGeek I've left some comments inline.
@jrfnl will the current CSV method work with PHPCS 3.3.0+? If so it seams like it might be better to wait before implementing that even by reference.
Yes, the current comma-delimited string format will still work, but it will be removed in PHPCS 4.0.
For more information about this change and the new format - much more readable! -, see the PHPCS 3.3.0 release notes: https://github.com/squizlabs/PHP_CodeSniffer/releases/tag/3.3.0
templates/.phpcs.xml.dist
Outdated
|
|
||
| <!-- What to scan --> | ||
| <file>.</file> | ||
| <exclude-pattern>vendor/</exclude-pattern> |
There was a problem hiding this comment.
Be aware that exclude-pattern values are interpreted as regular expressions, so this snippet would exclude every directory which included the string vendor/, i.e. /some-other-vendor/ would also be excluded.
While this may be something which will rarely cause issues, you can stabilize this to just the Composer vendor directory by using something along the lines of ^/vendor/* or the slightly more generic ./vendor/
There was a problem hiding this comment.
For more information about how the exclude-patterns are interpreted, see: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders
There was a problem hiding this comment.
Darn... my bad. The more generic version should be /vendor/. The . which I'd inadvertently placed in front of it will not be interpreted as root, but as a regex wildcard, so, while it shouldn't give any problems, it also isn't necessary.
templates/.phpcs.xml.dist
Outdated
| <file>.</file> | ||
| <exclude-pattern>vendor/</exclude-pattern> | ||
|
|
||
| <!-- How to scan --> |
There was a problem hiding this comment.
I would recommend adding a link here to the PHPCS wiki so people will know where to find more information about these options.
You can either use the generic wiki link: https://github.com/squizlabs/PHP_CodeSniffer/wiki
Or links to both these pages as good starting points for people:
- Usage instructions: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
- Annotated ruleset: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
| <arg name="extensions" value="php"/> | ||
| <arg name="parallel" value="8"/><!-- Enables parallel processing when available for faster results. --> | ||
|
|
||
| <!-- Rules: Check PHP version compatibility --> |
There was a problem hiding this comment.
Indentation is off here. Tabs vs spaces.
| <arg name="extensions" value="php"/> | ||
| <arg name="parallel" value="8"/><!-- Enables parallel processing when available for faster results. --> | ||
|
|
||
| <!-- Rules: Check PHP version compatibility --> |
There was a problem hiding this comment.
As per the earlier comments, using PHPCompatibilityWP will be the better choice.
I'd also recommend adding a link to the PHPCompatibilityWP repo as a comment, again to help people to more easily find out more info: https://github.com/PHPCompatibility/PHPCompatibilityWP
Maybe also add a link for more info about what the testVersion means and how it works: https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions
You may also want to add a comment saying that people can change the testVersion to suit their project.
templates/.phpcs.xml.dist
Outdated
| <config name="testVersion" value="5.3-"/> | ||
| <rule ref="PHPCompatibility"/> | ||
|
|
||
| <!-- Rules: WordPress Coding Standards --> |
There was a problem hiding this comment.
Along the same lines as some of my other comments, you may want to add a link here to the WPCS repo and/or to the WPCS wiki page about configuring sniffs:
templates/.phpcs.xml.dist
Outdated
| <property name="text_domain" type="array" value="my-plugin"/> | ||
| </properties> | ||
| </rule> | ||
| <rule ref="WordPress.WhiteSpace.ControlStructureSpacing"> |
There was a problem hiding this comment.
This is more a personal preference, than a recommended setting. I'm not sure if it belong in a scaffold.
templates/.phpcs.xml.dist
Outdated
| <properties> | ||
| <property name="blank_line_check" value="true"/> | ||
| </properties> | ||
| </rule> |
There was a problem hiding this comment.
I haven't looked at the scaffold in detail, but in case it also adds a PHPUnit test set-up, you may want to add an additional set of custom properties for that: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#custom-unit-test-classes
templates/.phpcs.xml.dist
Outdated
| <!-- How to scan --> | ||
| <arg value="sp"/> <!-- Show sniff and progress --> | ||
| <arg name="basepath" value="./"/><!-- Strip the file paths down to the relevant bit --> | ||
| <arg name="colors"/> |
There was a problem hiding this comment.
Just a side-note: colors will only work on certain platforms. Not sure by heart, but IIRC it works on Linux and Mac, but not on Windows.
|
Forgot to respond to this:
WPCS 1.0.0 was due to come out last week, but there's still one issue which needs to be fixed. Expect it very soon though. All the same, for the ruleset as it is, AFAICS, no changes would be needed for WPCS 1.0.0, so you're good. |
also converts spaces to tabs
|
The failure looks like some behat tests need updating to reference |
|
@GaryJones @jrfnl will it be ok for this to not have the leading .? I think it is required to be automatically used but I want to make sure I'm understanding the reason. |
Do you mean for the actual file name Yes, no problem. Allowing a leading So for the custom ruleset with a leading dot to work, the minimum PHPCS requirement would have to be set at 3.1.0. The file loading order (as of PHPCS 3.1.1) is:
In other words, not having the leading Refs: |
And beyond what @jrfnl just said, whether it's right for PHPCS 3.1[.1] to be the minimum version for scaffolded plugins is a different matter, though for new plugins I don't see much reason why the default shouldn't be 3.3. |
|
I went ahed and updated the tests to solve for the file ext change |
| @@ -21,7 +21,7 @@ phpunit.xml.dist | |||
| multisite.xml | |||
| multisite.xml.dist | |||
| phpcs.xml | |||
There was a problem hiding this comment.
You may also want to add .phpcs.xml here and possibly to the .gitignore file too ?
There was a problem hiding this comment.
@jrfnl thanks! I added .phpcs.xml but .gitignore is already in the file higher up.
|
FYI: in the mean time, both PHPCS 3.3.1 as well as WPCS 1.0.0 have been released. |
|
Thanks to @NicktheGeek for the pull request, and thanks to @GaryJones & @jrfnl for the extensive review! |
change to .phpcs.xml.dist and update rules
The .phpcs.xml.dist rule file is detected and implemented automatically, reducing complexity for running phpcs.
The updated rules account for new wpcs sniff properties including the ability to check global prefix and i18n implementation with the correct text domain when specified.
The values have been left blank so the sniffs are generic, but the rules are in place to make it easier to get started by scaffold.
This is tangentially related to #109
This is also loosely related to #63 as updating composer scaffolds could work with this change.
@GaryJones may be able to provide additional feedback on this file change.