Skip to content

Commit b00afd0

Browse files
jrfnlschlessera
authored andcommitted
Implement CS checking based on the WP_CLI_CS ruleset (#80)
* Composer: use wp-cli-tests ^2.1 ... as that version contains the new WPCliCS PHPCS standard. * .gitignore/.distignore: ignore phpcs/phpunit config files * `.distignore`: no need to distribute the phpcs/phpunit config files. * `.gitignore`: Allow devs to overwrite config files for PHPUnit and PHPCS - The `.dist` files should be committed. However, for their personal use, devs can overrule those files with versions without `.dist`. Those personal versions should never be committed. As a side-note: for PHPCS, having a personal version while still using the original `.dist` file is made very easy, as you can just import the `.dist` file as a starting point, like so: ```xml <?xml version="1.0"?> <ruleset name="WP-CLI"> <rule ref="./phpcs.xml.dist"/> </ruleset> ``` * PHPCS: add a ruleset for this project The file is completely documented in-line. * PHPCS: whitelist some code for select sniffs
1 parent c2b1997 commit b00afd0

7 files changed

+98
-2
lines changed

.distignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
.travis.yml
77
behat.yml
88
circle.yml
9+
phpcs.xml.dist
10+
phpunit.xml.dist
911
bin/
1012
features/
1113
utils/

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ vendor/
66
*.tar.gz
77
composer.lock
88
*.log
9+
phpunit.xml
10+
phpcs.xml
11+
.phpcs.xml

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"wp-cli/db-command": "^1.3 || ^2",
1919
"wp-cli/entity-command": "^1.3 || ^2",
2020
"wp-cli/extension-command": "^1.2 || ^2",
21-
"wp-cli/wp-cli-tests": "^2.0.7"
21+
"wp-cli/wp-cli-tests": "^2.1"
2222
},
2323
"config": {
2424
"process-timeout": 7200,

phpcs.xml.dist

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="WP-CLI-language">
3+
<description>Custom ruleset for WP-CLI language-command</description>
4+
5+
<!--
6+
#############################################################################
7+
COMMAND LINE ARGUMENTS
8+
For help understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
9+
For help using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
10+
#############################################################################
11+
-->
12+
13+
<!-- What to scan. -->
14+
<file>.</file>
15+
16+
<!-- Show progress. -->
17+
<arg value="p"/>
18+
19+
<!-- Strip the filepaths down to the relevant bit. -->
20+
<arg name="basepath" value="./"/>
21+
22+
<!-- Check up to 8 files simultaneously. -->
23+
<arg name="parallel" value="8"/>
24+
25+
<!--
26+
#############################################################################
27+
USE THE WP_CLI_CS RULESET
28+
#############################################################################
29+
-->
30+
31+
<rule ref="WP_CLI_CS"/>
32+
33+
<!--
34+
#############################################################################
35+
PROJECT SPECIFIC CONFIGURATION FOR SNIFFS
36+
#############################################################################
37+
-->
38+
39+
<!-- For help understanding the `testVersion` configuration setting:
40+
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
41+
<config name="testVersion" value="5.4-"/>
42+
43+
<!-- Verify that everything in the global namespace is either namespaced or prefixed.
44+
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
45+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
46+
<properties>
47+
<property name="prefixes" type="array">
48+
<element value="WP_CLI\Language"/><!-- Namespaces. -->
49+
<element value="wpcli_language"/><!-- Global variables and such. -->
50+
</property>
51+
</properties>
52+
</rule>
53+
54+
<!--
55+
#############################################################################
56+
SELECTIVE EXCLUSIONS
57+
#############################################################################
58+
-->
59+
60+
<!-- The objects containing these properties come from the wordpress.org API, so cannot be renamed.
61+
Used in file: CommandWithTranslation.php.
62+
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#mixed-case-property-name-exceptions
63+
Related: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1623 -->
64+
<rule ref="WordPress.NamingConventions.ValidVariableName">
65+
<properties>
66+
<property name="customPropertiesWhitelist" type="array">
67+
<element value="Language"/>
68+
<element value="Name"/>
69+
<element value="Type"/>
70+
<element value="Version"/>
71+
</property>
72+
</properties>
73+
</rule>
74+
75+
<!-- Exclude existing namespaces from the prefix rule as it would break BC to prefix them now. -->
76+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound">
77+
<exclude-pattern>*/src/WP_CLI/(CommandWithTranslation|LanguagePackUpgrader)\.php$</exclude-pattern>
78+
</rule>
79+
80+
<!-- Exclude existing classes from the prefix rule as it would break BC to prefix them now. -->
81+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
82+
<exclude-pattern>*/src/(Core|Plugin|Site_Switch|Theme)_Language_Command\.php$</exclude-pattern>
83+
<exclude-pattern>*/src/Language_Namespace\.php$</exclude-pattern>
84+
</rule>
85+
86+
</ruleset>

src/Core_Language_Command.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public function uninstall( $args, $assoc_args ) {
311311
*
312312
* @subcommand update
313313
*/
314-
public function update( $args, $assoc_args ) {
314+
public function update( $args, $assoc_args ) { // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found -- Overruling the documentation, so not useless ;-).
315315
parent::update( $args, $assoc_args );
316316
}
317317

src/Plugin_Language_Command.php

+1
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ public function update( $args, $assoc_args ) {
466466
* @return array
467467
*/
468468
private function get_all_plugins() {
469+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Using WP native hook.
469470
return apply_filters( 'all_plugins', get_plugins() );
470471
}
471472
}

src/WP_CLI/LanguagePackUpgrader.php

+4
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ public function download_package( $package ) {
5757
* @param bool $reply Whether to bail without returning the package. Default is false.
5858
* @param string $package The package file name.
5959
* @param \WP_Upgrader $this The WP_Upgrader instance.
60+
*
61+
* @phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Using WP native hook.
6062
*/
6163
$reply = apply_filters( 'upgrader_pre_download', false, $package, $this );
64+
// phpcs:enable
65+
6266
if ( false !== $reply ) {
6367
return $reply;
6468
}

0 commit comments

Comments
 (0)