Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 16 additions & 50 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@ script:
# Run ruleset tests.
- ./bin/ruleset-tests

env:
# `master` is now 3.x.
- PHPCS_BRANCH="dev-master"
# Lowest supported release in the 3.x series with which VIPCS is compatible.
- PHPCS_BRANCH="3.3.1"

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- "7.4snapshot"

# Rather than a `matrix` property, we use build stages. This allows early
# build failure for basic linting and sniffing issues.
# @link https://docs.travis-ci.com/user/build-stages/
#
# PHP >= 7.3 & PHPCS < 3.3.1 is known to fail.
# @link https://github.com/squizlabs/PHP_CodeSniffer/issues/2086.
#
# We can't use Matrix Expansion here, since we need to exclude jobs for the known failures,
# which Matrix Expansion does not yet support. We list each combination instead.
# @link https://docs.travis-ci.com/user/conditional-builds-stages-jobs/#conditional-jobs

stages:
- lint
Expand All @@ -43,11 +50,10 @@ stages:
jobs:
allow_failures:
- php: "7.4snapshot"
fast_finish: true
include:

- stage: lint
php: 7.2
php: 7.3
env: PHPCS_BRANCH="dev-master"
before_install: phpenv config-rm xdebug.ini || echo 'No xdebug config.'
install: false
Expand All @@ -70,52 +76,12 @@ jobs:
- libxml2-utils

- stage: sniff
php: 7.2
php: 7.3
env: PHPCS_BRANCH="dev-master"
before_install: phpenv config-rm xdebug.ini || echo 'No xdebug config.'
install: composer install --dev --no-suggest
script:
# Run PHPCS against VIPCS.
- ./bin/phpcs

# PHPCS 3.2.3 is the lowest supported release
- stage: test
php: 5.6
env: PHPCS_BRANCH="dev-master"

- stage: test
php: 5.6
env: PHPCS_BRANCH="3.2.3"

- stage: test
php: 7.0
env: PHPCS_BRANCH="dev-master"

- stage: test
php: 7.0
env: PHPCS_BRANCH="3.2.3"

- stage: test
php: 7.1
env: PHPCS_BRANCH="dev-master"

- stage: test
php: 7.1
env: PHPCS_BRANCH="3.2.3"

- stage: test
php: 7.2
env: PHPCS_BRANCH="dev-master"

- stage: test
php: 7.2
env: PHPCS_BRANCH="3.2.3"

# Only use PHPCS_BRANCH="dev-master" from this point, because PHPCS 3.3.1+ is needed to support PHP 7.3+
- stage: test
php: 7.3
env: PHPCS_BRANCH="dev-master"

- stage: test
php: "7.4snapshot"
env: PHPCS_BRANCH="dev-master"
45 changes: 22 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
# VIP-Coding-Standards
# VIP Coding Standards

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

This project contains 2 PHP Codesniffer rulesets:
This project contains two rulesets:

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

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

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

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

## Installation
## Minimal requirements

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

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

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.
`composer require automattic/vipwpcs`, or `composer g require automattic/vipwpcs` if installing globally.

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).
This will install the latest compatible versions of PHPCS and WPCS.

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.

`phpcs --config-set installed_paths [/path/to/wpcsstandard],[path/to/vipcsstandard],etc`
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.

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

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

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

### Setup note
## Contribution

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:
Please see [CONTRIBUTION.md](CONTRIBUTING.md).

`phpcs --config-set installed_paths [/path/to/standard],[path/to/standard]`
## License

Note the comma separating each standard.
Licensed under [GPL-2.0-or-later](LICENSE.md).
6 changes: 3 additions & 3 deletions WordPress-VIP-Go/ruleset-test.inc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ wpcom_vip_file_get_contents( $bar ); // Ok.

// WordPress.Security.NonceVerification.NoNonceVerification
function bar_foo() {
if ( ! isset( $_POST['test'] ) ) { // Warning.
if ( ! isset( $_POST['test'] ) ) { // Error.
return;
}
}
Expand All @@ -80,7 +80,7 @@ function foo_bar() {
}

// WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
do_something( $_POST ); // Ok.
do_something( $_POST ); // Error.
if ( isset( $_POST['foo2'] ) ) {
bar( wp_unslash( $_POST['foo2'] ) ); // Warning.
}
Expand Down Expand Up @@ -473,7 +473,7 @@ $args = array(
);
$query_args['orderby'] = 'rand'; // Error.

// WordPressVIPMinimum.Performance.RegexpCompare
// WordPressVIPMinimum.Performance.RegexpCompare
$query_args = array(
'posts_per_page' => 1,
'post_status' => 'draft',
Expand Down
3 changes: 2 additions & 1 deletion WordPress-VIP-Go/ruleset-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
60 => 1,
63 => 1,
66 => 1,
72 => 1,
83 => 1,
165 => 1,
180 => 1,
181 => 1,
Expand Down Expand Up @@ -162,7 +164,6 @@
575 => 1,
],
'warnings' => [
72 => 1,
85 => 1,
90 => 1,
94 => 1,
Expand Down
27 changes: 0 additions & 27 deletions WordPressVIPMinimum/PHPCSCompatibility.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class DeclarationCompatibilitySniff extends AbstractScopeSniff {
*
* @var string
*/
private $_currentClass = '';
private $currentClass = '';

/**
* A list of functions in the current class.
*
* @var string[]
*/
private $_functionList = [];
private $functionList = [];

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

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

if ( $className !== $this->_currentClass ) {
if ( $className !== $this->currentClass ) {
$this->loadFunctionNamesInScope( $phpcsFile, $currScope );
$this->_currentClass = $className;
$this->currentClass = $className;
}

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

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

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

Expand Down Expand Up @@ -354,16 +354,16 @@ private function generateParamList( $methodSignature ) {
* @return void
*/
protected function loadFunctionNamesInScope( File $phpcsFile, $currScope ) {
$this->_functionList = [];
$tokens = $phpcsFile->getTokens();
$this->functionList = [];
$tokens = $phpcsFile->getTokens();

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

$next = $phpcsFile->findNext( T_STRING, $i );
$this->_functionList[] = trim( $tokens[ $next ]['content'] );
$next = $phpcsFile->findNext( T_STRING, $i );
$this->functionList[] = trim( $tokens[ $next ]['content'] );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace WordPressVIPMinimum\Sniffs\Classes;

use WordPress\AbstractClassRestrictionsSniff;
use WordPressCS\WordPress\AbstractClassRestrictionsSniff;

/**
* WordPressVIPMinimum_Sniffs_Classes_RestrictedExtendClassesSniff.
Expand Down
2 changes: 1 addition & 1 deletion WordPressVIPMinimum/Sniffs/Files/IncludingFileSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace WordPressVIPMinimum\Sniffs\Files;

use WordPress\AbstractFunctionRestrictionsSniff;
use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff;
use PHP_CodeSniffer\Util\Tokens;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace WordPressVIPMinimum\Sniffs\Functions;

use PHP_CodeSniffer\Files\File;
use WordPressVIPMinimum\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

Expand All @@ -22,13 +21,6 @@
*/
class CheckReturnValueSniff extends Sniff {

/**
* Tokens of the whole file.
*
* @var array
*/
private $_tokens = [];

/**
* Pairs we are about to check.
*
Expand Down
Loading