Skip to content

Commit a61b89f

Browse files
authored
Merge pull request #44 from jrfnl/feature/add-wpclics-phpcs-standard
Create a `WP_CLI_CS` standard for PHP_CodeSniffer
2 parents c807d67 + 8ada83a commit a61b89f

File tree

10 files changed

+270
-78
lines changed

10 files changed

+270
-78
lines changed

.distignore

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ vendor/
88
*.txt
99
*.log
1010
composer.lock
11+
phpunit.xml
12+
phpcs.xml
13+
.phpcs.xml

.readme-partials/USING.md

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
To make use of the WP-CLI testing framework, you need to complete the following steps from within the package you want to add them to:
22

33
1. Add the testing framework as a development requirement:
4-
```bash
5-
composer require --dev wp-cli/wp-cli-tests
6-
```
4+
```bash
5+
composer require --dev wp-cli/wp-cli-tests
6+
```
77

88
2. Add the required test scripts to the `composer.json` file:
9-
```json
10-
"scripts": {
9+
```json
10+
"scripts": {
1111
"behat": "run-behat-tests",
1212
"behat-rerun": "rerun-behat-tests",
1313
"lint": "run-linter-tests",
@@ -20,31 +20,62 @@ To make use of the WP-CLI testing framework, you need to complete the following
2020
"@phpunit",
2121
"@behat"
2222
]
23-
}
24-
```
25-
You can of course remove the ones you don't need.
23+
}
24+
```
25+
You can of course remove the ones you don't need.
2626
2727
3. Optionally add a modified process timeout to the `composer.json` file to make sure scripts can run until their work is completed:
28-
```json
29-
"config": {
30-
"process-timeout": 1800
31-
},
32-
```
33-
The timeout is expressed in seconds.
28+
```json
29+
"config": {
30+
"process-timeout": 1800
31+
},
32+
```
33+
The timeout is expressed in seconds.
3434
3535
4. Optionally add a `behat.yml` file to the package root with the following content:
36-
```json
37-
default:
38-
paths:
39-
features: features
36+
```json
37+
default:
38+
paths:
39+
features: features
4040
bootstrap: vendor/wp-cli/wp-cli-tests/features/bootstrap
41-
```
42-
This will make sure that the automated Behat system works across all platforms. This is needed on Windows.
41+
```
42+
This will make sure that the automated Behat system works across all platforms. This is needed on Windows.
4343
44-
5. Update your composer dependencies and regenerate your autoloader and binary folders:
45-
```bash
46-
composer update
47-
```
44+
5. Optionally add a `phpcs.xml.dist` file to the package root to enable code style and best practice checks using PHP_CodeSniffer.
45+
46+
Example of a minimal custom ruleset based on the defaults set in the WP-CLI testing framework:
47+
```xml
48+
<?xml version="1.0"?>
49+
<ruleset name="WP-CLI-PROJECT-NAME">
50+
<description>Custom ruleset for WP-CLI PROJECT NAME</description>
51+
52+
<!-- What to scan. -->
53+
<file>.</file>
54+
55+
<!-- Show progress. -->
56+
<arg value="p"/>
57+
58+
<!-- Strip the filepaths down to the relevant bit. -->
59+
<arg name="basepath" value="./"/>
60+
61+
<!-- Check up to 8 files simultaneously. -->
62+
<arg name="parallel" value="8"/>
63+
64+
<!-- For help understanding the `testVersion` configuration setting:
65+
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
66+
<config name="testVersion" value="5.4-"/>
67+
68+
<!-- Rules: Include the base ruleset for WP-CLI projects. -->
69+
<rule ref="WP_CLI_CS"/>
70+
71+
</ruleset>
72+
```
73+
74+
All other [PHPCS configuration options](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset) are, of course, available.
75+
6. Update your composer dependencies and regenerate your autoloader and binary folders:
76+
```bash
77+
composer update
78+
```
4879
4980
You are now ready to use the testing framework from within your package.
5081

WP_CLI_CS/ruleset.xml

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="WP_CLI_CS" xsi:noNamespaceSchemaLocation="../vendor/squizlabs/php_codesniffer/phpcs.xsd">
3+
4+
<description>Coding standard for WP-CLI projects</description>
5+
6+
<!--
7+
To include this ruleset in a WP-CLI project, use `rule ref="WP_CLI_CS"` in brackets.
8+
See the instructions in the README/USING for an example.
9+
10+
For help using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
11+
For help understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
12+
-->
13+
14+
<!--
15+
#############################################################################
16+
COMMAND LINE ARGUMENTS
17+
#############################################################################
18+
-->
19+
20+
<!-- Ignoring select files/folders for all projects.
21+
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders -->
22+
<exclude-pattern>*/.git/*</exclude-pattern>
23+
<exclude-pattern>*/node_modules/*</exclude-pattern>
24+
<exclude-pattern>*/vendor/*</exclude-pattern>
25+
26+
27+
<!--
28+
#############################################################################
29+
USE THE PHPCOMPATIBILITY RULESET
30+
31+
This checks code for PHP cross-version compatibility.
32+
See: https://github.com/PHPCompatibility/PHPCompatibility
33+
#############################################################################
34+
-->
35+
36+
<rule ref="PHPCompatibility">
37+
<!-- Only scan PHP files for PHP compatibility. -->
38+
<include-pattern>*\.php$</include-pattern>
39+
40+
<!-- Polyfill package is included with WP-CLI, so available to all projects. -->
41+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.array_columnFound"/>
42+
</rule>
43+
44+
<!--
45+
#############################################################################
46+
USE THE WORDPRESS-EXTRA RULESET
47+
48+
This checks code against the WordPress Core code style requirements, as well as
49+
a number of modern PHP style rules and other best practices which are
50+
currently not yet covered in the Core style handbook.
51+
See: https://make.wordpress.org/core/handbook/best-practices/coding-standards/
52+
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
53+
#############################################################################
54+
-->
55+
56+
<rule ref="WordPress-Extra">
57+
58+
<!-- No need for this sniff as the parallel linter command takes care of linting. -->
59+
<exclude name="Generic.PHP.Syntax"/>
60+
61+
<!-- To make autoloading easier, PSR-4 is mostly adhered to for file naming. -->
62+
<exclude name="WordPress.Files.FileName"/>
63+
64+
<!-- Output is sent to cli, not to HTML, so this sniff is not applicable to WP-CLI.
65+
Note: some output escaping may still be needed/beneficial, however this would probably
66+
require a custom sniff. -->
67+
<exclude name="WordPress.Security.EscapeOutput"/>
68+
69+
<!-- WP-CLI is intended as a developer tool, so using development functions should be fine. -->
70+
<exclude name="WordPress.PHP.DevelopmentFunctions"/>
71+
72+
<!-- Make some allowance for the fact that the code will be run in a command-line environment. -->
73+
<exclude name="Generic.PHP.BacktickOperator"/>
74+
75+
</rule>
76+
77+
78+
<!--
79+
#############################################################################
80+
SPECIFIC CONFIGURATION FOR SNIFFS
81+
#############################################################################
82+
-->
83+
84+
<!-- Verify that everything in the global namespace is either namespaced or prefixed.
85+
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
86+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
87+
<properties>
88+
<property name="prefixes" type="array">
89+
<element value="WP_CLI"/><!-- Namespaces and non-namespaced classes. -->
90+
<element value="wpcli"/><!-- Global variables and functions. -->
91+
</property>
92+
</properties>
93+
</rule>
94+
95+
<!-- Allow for silencing errors in combination with a select list of functions.
96+
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#error-silencing-use-build-in-function-whitelist -->
97+
<rule ref="WordPress.PHP.NoSilencedErrors">
98+
<properties>
99+
<property name="use_default_whitelist" value="true"/>
100+
</properties>
101+
</rule>
102+
103+
<!-- Make some allowance for the fact that the code will be run in a command-line environment.
104+
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#excluding-a-group-of-checks -->
105+
<rule ref="WordPress.PHP.DiscouragedPHPFunctions">
106+
<properties>
107+
<property name="exclude" type="array">
108+
<element value="runtime_configuration"/>
109+
<element value="system_calls"/>
110+
</property>
111+
</properties>
112+
</rule>
113+
114+
<rule ref="WordPress.WP.AlternativeFunctions">
115+
<properties>
116+
<property name="exclude" type="array">
117+
<element value="curl"/>
118+
<element value="file_get_contents"/>
119+
<element value="file_system_read"/>
120+
<!-- As PHP 5.4. is the minimum for most projects, using json_encode() is fine. -->
121+
<element value="json_encode"/>
122+
</property>
123+
</properties>
124+
</rule>
125+
126+
</ruleset>

bin/run-phpcs-tests

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22

3-
# Run the unit tests, if they exist
4-
if [ -f "phpcs.xml" ] || [ -f "phpcs.xml.dist" ] || [ -f ".phpcs.xml" ] || [ -f ".phpcs.xml.dist" ]
3+
# Run the code style check only if a configuration file exists.
4+
if [ -f ".phpcs.xml" ] || [ -f "phpcs.xml" ] || [ -f ".phpcs.xml.dist" ] || [ -f "phpcs.xml.dist" ]
55
then
66
vendor/bin/phpcs "$@"
77
fi

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@
77
],
88
"homepage": "https://wp-cli.org",
99
"license": "MIT",
10+
"type" : "phpcodesniffer-standard",
1011
"require": {
1112
"php": ">=5.4",
1213
"behat/behat": "^2.5",
1314
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.3 || ^0.5",
1415
"jakub-onderka/php-console-highlighter": "^0.3.2",
1516
"jakub-onderka/php-parallel-lint": "^1.0",
16-
"phpcompatibility/phpcompatibility-wp": "^2",
17+
"phpcompatibility/php-compatibility": "^9",
1718
"phpunit/phpunit": ">=4.8 <7",
1819
"roave/security-advisories": "dev-master",
1920
"wp-cli/config-command": "^1 || ^2",
2021
"wp-cli/core-command": "^1 || ^2",
2122
"wp-cli/eval-command": "^1 || ^2",
2223
"wp-cli/wp-cli": "^2",
23-
"wp-coding-standards/wpcs": "^1"
24+
"wp-coding-standards/wpcs": "^2.1"
2425
},
2526
"require-dev": {
2627
"wp-cli/regenerate-readme": "^2"

features/bootstrap/support.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ function assert_regex( $regex, $actual ) {
1616
}
1717

1818
function assert_equals( $expected, $actual ) {
19+
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- Deliberate loose comparison.
1920
if ( $expected != $actual ) {
2021
throw new Exception( 'Actual value: ' . var_export( $actual, true ) );
2122
}
2223
}
2324

2425
function assert_not_equals( $expected, $actual ) {
26+
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- Deliberate loose comparison.
2527
if ( $expected == $actual ) {
2628
throw new Exception( 'Actual value: ' . var_export( $actual, true ) );
2729
}

features/extra/no-mail.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
<?php
2-
3-
/*
2+
/**
43
* This file is copied as amu-plugin into new WP installs to reroute normal
54
* mails into log entries.
65
*/
76

7+
/**
8+
* Replace WP native pluggable wp_mail function for test purposes.
9+
*
10+
* @param string $to Email address.
11+
*
12+
* @phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- WP native function.
13+
*/
814
function wp_mail( $to ) {
915
// Log for testing purposes
1016
WP_CLI::log( "WP-CLI test suite: Sent email to {$to}." );
1117
}
12-
18+
// phpcs:enable

0 commit comments

Comments
 (0)