Skip to content

Commit adf7bc9

Browse files
authored
Add WPCS 2 compatibility (#423)
Add WPCS 2 compatibility
2 parents 47f20c7 + 508a72a commit adf7bc9

26 files changed

+300
-310
lines changed

.travis.yml

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,23 @@ script:
2424
# Run ruleset tests.
2525
- ./bin/ruleset-tests
2626

27+
env:
28+
# `master` is now 3.x.
29+
- PHPCS_BRANCH="dev-master"
30+
# Lowest supported release in the 3.x series with which VIPCS is compatible.
31+
- PHPCS_BRANCH="3.3.1"
32+
33+
php:
34+
- 5.6
35+
- 7.0
36+
- 7.1
37+
- 7.2
38+
- 7.3
39+
- "7.4snapshot"
40+
2741
# Rather than a `matrix` property, we use build stages. This allows early
2842
# build failure for basic linting and sniffing issues.
2943
# @link https://docs.travis-ci.com/user/build-stages/
30-
#
31-
# PHP >= 7.3 & PHPCS < 3.3.1 is known to fail.
32-
# @link https://github.com/squizlabs/PHP_CodeSniffer/issues/2086.
33-
#
34-
# We can't use Matrix Expansion here, since we need to exclude jobs for the known failures,
35-
# which Matrix Expansion does not yet support. We list each combination instead.
36-
# @link https://docs.travis-ci.com/user/conditional-builds-stages-jobs/#conditional-jobs
3744

3845
stages:
3946
- lint
@@ -43,11 +50,10 @@ stages:
4350
jobs:
4451
allow_failures:
4552
- php: "7.4snapshot"
46-
fast_finish: true
4753
include:
4854

4955
- stage: lint
50-
php: 7.2
56+
php: 7.3
5157
env: PHPCS_BRANCH="dev-master"
5258
before_install: phpenv config-rm xdebug.ini || echo 'No xdebug config.'
5359
install: false
@@ -70,52 +76,12 @@ jobs:
7076
- libxml2-utils
7177

7278
- stage: sniff
73-
php: 7.2
79+
php: 7.3
7480
env: PHPCS_BRANCH="dev-master"
7581
before_install: phpenv config-rm xdebug.ini || echo 'No xdebug config.'
7682
install: composer install --dev --no-suggest
7783
script:
7884
# Run PHPCS against VIPCS.
7985
- ./bin/phpcs
8086

81-
# PHPCS 3.2.3 is the lowest supported release
82-
- stage: test
83-
php: 5.6
84-
env: PHPCS_BRANCH="dev-master"
85-
86-
- stage: test
87-
php: 5.6
88-
env: PHPCS_BRANCH="3.2.3"
89-
90-
- stage: test
91-
php: 7.0
92-
env: PHPCS_BRANCH="dev-master"
93-
94-
- stage: test
95-
php: 7.0
96-
env: PHPCS_BRANCH="3.2.3"
97-
98-
- stage: test
99-
php: 7.1
100-
env: PHPCS_BRANCH="dev-master"
101-
102-
- stage: test
103-
php: 7.1
104-
env: PHPCS_BRANCH="3.2.3"
105-
106-
- stage: test
107-
php: 7.2
108-
env: PHPCS_BRANCH="dev-master"
109-
110-
- stage: test
111-
php: 7.2
112-
env: PHPCS_BRANCH="3.2.3"
11387

114-
# Only use PHPCS_BRANCH="dev-master" from this point, because PHPCS 3.3.1+ is needed to support PHP 7.3+
115-
- stage: test
116-
php: 7.3
117-
env: PHPCS_BRANCH="dev-master"
118-
119-
- stage: test
120-
php: "7.4snapshot"
121-
env: PHPCS_BRANCH="dev-master"

README.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
1-
# VIP-Coding-Standards
1+
# VIP Coding Standards
22

3-
This project maintains the minimum ruleset of [PHP_CodeSniffer rules](https://github.com/squizlabs/PHP_CodeSniffer) (sniffs) to validate code developed for [WordPress.com VIP](https://wpvip.com/).
3+
This project contains [PHP_CodeSniffer (PHPCS) sniffs and rulesets](https://github.com/squizlabs/PHP_CodeSniffer) to validate code developed for [WordPress.com VIP](https://wpvip.com/).
44

5-
This project contains 2 PHP Codesniffer rulesets:
5+
This project contains two rulesets:
66

7-
- `WordPressVIPMinimum` - for use on WordPress.com projects
8-
- `WordPress-VIP-Go` - for use on VIP Go projects
7+
- `WordPressVIPMinimum` - for use with projects on the (older) WordPress.com VIP platform.
8+
- `WordPress-VIP-Go` - for use with projects on the (newer) VIP Go platform.
99

10-
These rulesets contain only the rules which are considered being "blockers" and "warnings" according to the [WordPress VIP Go documentation](https://wpvip.com/documentation/vip-go/code-review-blockers-warnings-notices/)
10+
These rulesets contain only the rules which are considered to be "errors" and "warnings" according to the [WordPress VIP Go documentation](https://wpvip.com/documentation/vip-go/code-review-blockers-warnings-notices/)
1111

12-
The ruleset takes advantage of existing rules in the [WordPress-Coding-Standards](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards) project.
12+
The rulesets use rules from the [WordPress-Coding-Standards](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards) (WPCS) project.
1313

14-
Go to https://wpvip.com/documentation/phpcs-review-feedback/ to learn about why various things are flagged as errors vs warnings and what the levels mean for us.
14+
Go to https://wpvip.com/documentation/phpcs-review-feedback/ to learn about why violations are flagged as errors vs warnings and what the levels mean.
1515

16-
## Installation
16+
## Minimal requirements
1717

18-
### Note
18+
* [PHPCS 3.3.1+](https://github.com/squizlabs/PHP_CodeSniffer/releases)
19+
* [WPCS 2.*](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/releases)
1920

20-
Currently, the VIP Go Coding Standards are built on top of the WordPress Coding Standards 1.* release. If you are using `master` here, you will need to checkout [1.2.1 tag](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/releases/tag/1.2.1).
21+
## Installation
2122

22-
First, make sure you have WPCS 1.* and PHPCS v3+ installed. If you do not, please refer to the [installation instructions for installing PHP CodeSniffer for WordPress.com VIP](https://vip.wordpress.com/documentation/how-to-install-php-code-sniffer-for-wordpress-com-vip/). Note that VIPCS does not currently work with the `develop` or `master` branch of WPCS.
23+
`composer require automattic/vipwpcs`, or `composer g require automattic/vipwpcs` if installing globally.
2324

24-
You will also find additional information at the [WordPress Coding Standards for PHP_CodeSniffer project](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#installation).
25+
This will install the latest compatible versions of PHPCS and WPCS.
2526

26-
Then, clone this repo to your local machine, and add the standard to PHPCodeSniffer by appending the folder you cloned into to the end of the installed paths. e.g.
2727

28-
`phpcs --config-set installed_paths [/path/to/wpcsstandard],[path/to/vipcsstandard],etc`
28+
Please refer to the [installation instructions for installing PHP_CodeSniffer for WordPress.com VIP](https://wpvip.com/documentation/how-to-install-php-code-sniffer-for-wordpress-com-vip/) for more details.
2929

30-
Alternatively, we recommend the [PHP_CodeSniffer Standards Composer Installer Plugin](https://github.com/Dealerdirect/phpcodesniffer-composer-installer), which handles the registration of all of the installed standards, so there is no need to set the `installed_paths` config value manually, for single or multiple standards.
30+
We recommend the [PHP_CodeSniffer Standards Composer Installer Plugin](https://github.com/Dealerdirect/phpcodesniffer-composer-installer), which handles the registration of all of the installed standards, so there is no need to set the `installed_paths` config value manually, for single or multiple standards.
3131

32-
### Minimal requirements
32+
Alternatively, you should register the standard to PHPCS by appending the VIPCS directory to the end of the installed paths. e.g.
3333

34-
* [PHPCS 3+](https://github.com/squizlabs/PHP_CodeSniffer/releases)
35-
* [WPCS 1.*](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/releases)
34+
`phpcs --config-set installed_paths [/path/to/wpcsstandard],[path/to/vipcsstandard],etc`
3635

37-
### Setup note
36+
## Contribution
3837

39-
Should you wish to run both standards (WordPress.com VIP minimum standard & WordPress.com VIP coding standard), you can add both to PHPCS by running the following configuration command:
38+
Please see [CONTRIBUTION.md](CONTRIBUTING.md).
4039

41-
`phpcs --config-set installed_paths [/path/to/standard],[path/to/standard]`
40+
## License
4241

43-
Note the comma separating each standard.
42+
Licensed under [GPL-2.0-or-later](LICENSE.md).

WordPress-VIP-Go/ruleset-test.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ wpcom_vip_file_get_contents( $bar ); // Ok.
6969

7070
// WordPress.Security.NonceVerification.NoNonceVerification
7171
function bar_foo() {
72-
if ( ! isset( $_POST['test'] ) ) { // Warning.
72+
if ( ! isset( $_POST['test'] ) ) { // Error.
7373
return;
7474
}
7575
}
@@ -80,7 +80,7 @@ function foo_bar() {
8080
}
8181

8282
// WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
83-
do_something( $_POST ); // Ok.
83+
do_something( $_POST ); // Error.
8484
if ( isset( $_POST['foo2'] ) ) {
8585
bar( wp_unslash( $_POST['foo2'] ) ); // Warning.
8686
}
@@ -473,7 +473,7 @@ $args = array(
473473
);
474474
$query_args['orderby'] = 'rand'; // Error.
475475

476-
// WordPressVIPMinimum.Performance.RegexpCompare
476+
// WordPressVIPMinimum.Performance.RegexpCompare
477477
$query_args = array(
478478
'posts_per_page' => 1,
479479
'post_status' => 'draft',

WordPress-VIP-Go/ruleset-test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
60 => 1,
3737
63 => 1,
3838
66 => 1,
39+
72 => 1,
40+
83 => 1,
3941
165 => 1,
4042
180 => 1,
4143
181 => 1,
@@ -162,7 +164,6 @@
162164
575 => 1,
163165
],
164166
'warnings' => [
165-
72 => 1,
166167
85 => 1,
167168
90 => 1,
168169
94 => 1,

WordPressVIPMinimum/PHPCSCompatibility.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class DeclarationCompatibilitySniff extends AbstractScopeSniff {
2222
*
2323
* @var string
2424
*/
25-
private $_currentClass = '';
25+
private $currentClass = '';
2626

2727
/**
2828
* A list of functions in the current class.
2929
*
3030
* @var string[]
3131
*/
32-
private $_functionList = [];
32+
private $functionList = [];
3333

3434
/**
3535
* A list of classes and methods to check.
@@ -204,9 +204,9 @@ protected function processTokenWithinScope( File $phpcsFile, $stackPtr, $currSco
204204

205205
$className = $phpcsFile->getDeclarationName( $currScope );
206206

207-
if ( $className !== $this->_currentClass ) {
207+
if ( $className !== $this->currentClass ) {
208208
$this->loadFunctionNamesInScope( $phpcsFile, $currScope );
209-
$this->_currentClass = $className;
209+
$this->currentClass = $className;
210210
}
211211

212212
$methodName = $phpcsFile->getDeclarationName( $stackPtr );
@@ -300,7 +300,7 @@ protected function processTokenWithinScope( File $phpcsFile, $stackPtr, $currSco
300300
*/
301301
private function addError( $parentClassName, $methodName, $currentMethodSignature, $parentMethodSignature, $phpcsFile, $stackPtr ) {
302302

303-
$currentSignature = sprintf( '%s::%s(%s)', $this->_currentClass, $methodName, implode( ', ', $this->generateParamList( $currentMethodSignature ) ) );
303+
$currentSignature = sprintf( '%s::%s(%s)', $this->currentClass, $methodName, implode( ', ', $this->generateParamList( $currentMethodSignature ) ) );
304304

305305
$parentSignature = sprintf( '%s::%s(%s)', $parentClassName, $methodName, implode( ', ', $this->generateParamList( $parentMethodSignature ) ) );
306306

@@ -354,16 +354,16 @@ private function generateParamList( $methodSignature ) {
354354
* @return void
355355
*/
356356
protected function loadFunctionNamesInScope( File $phpcsFile, $currScope ) {
357-
$this->_functionList = [];
358-
$tokens = $phpcsFile->getTokens();
357+
$this->functionList = [];
358+
$tokens = $phpcsFile->getTokens();
359359

360360
for ( $i = ( $tokens[ $currScope ]['scope_opener'] + 1 ); $i < $tokens[ $currScope ]['scope_closer']; $i++ ) {
361361
if ( T_FUNCTION !== $tokens[ $i ]['code'] ) {
362362
continue;
363363
}
364364

365-
$next = $phpcsFile->findNext( T_STRING, $i );
366-
$this->_functionList[] = trim( $tokens[ $next ]['content'] );
365+
$next = $phpcsFile->findNext( T_STRING, $i );
366+
$this->functionList[] = trim( $tokens[ $next ]['content'] );
367367
}
368368
}
369369

WordPressVIPMinimum/Sniffs/Classes/RestrictedExtendClassesSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace WordPressVIPMinimum\Sniffs\Classes;
99

10-
use WordPress\AbstractClassRestrictionsSniff;
10+
use WordPressCS\WordPress\AbstractClassRestrictionsSniff;
1111

1212
/**
1313
* WordPressVIPMinimum_Sniffs_Classes_RestrictedExtendClassesSniff.

WordPressVIPMinimum/Sniffs/Files/IncludingFileSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace WordPressVIPMinimum\Sniffs\Files;
99

10-
use WordPress\AbstractFunctionRestrictionsSniff;
10+
use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff;
1111
use PHP_CodeSniffer\Util\Tokens;
1212

1313
/**

WordPressVIPMinimum/Sniffs/Functions/CheckReturnValueSniff.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace WordPressVIPMinimum\Sniffs\Functions;
99

10-
use PHP_CodeSniffer\Files\File;
1110
use WordPressVIPMinimum\Sniffs\Sniff;
1211
use PHP_CodeSniffer\Util\Tokens;
1312

@@ -22,13 +21,6 @@
2221
*/
2322
class CheckReturnValueSniff extends Sniff {
2423

25-
/**
26-
* Tokens of the whole file.
27-
*
28-
* @var array
29-
*/
30-
private $_tokens = [];
31-
3224
/**
3325
* Pairs we are about to check.
3426
*

0 commit comments

Comments
 (0)