From 842c85f46b0d84b384d9aa7992330228218975cf Mon Sep 17 00:00:00 2001 From: James Nylen Date: Thu, 1 Jun 2017 14:45:10 -0400 Subject: [PATCH 1/5] Add a script to run `phpcs` locally --- .travis.yml | 6 +----- bin/run-phpcs.sh | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100755 bin/run-phpcs.sh diff --git a/.travis.yml b/.travis.yml index 9cbdd66250e30e..f6b2d2b5c2a495 100644 --- a/.travis.yml +++ b/.travis.yml @@ -58,11 +58,7 @@ script: fi - | if [[ "$TRAVISCI" == "phpcs" ]] ; then - find . \ - -not \( -path './node_modules' \) \ - -not \( -path './vendor' \) \ - -name '*.php' \ - | xargs -d'\n' phpcs --standard=phpcs.ruleset.xml -s + ./bin/run-phpcs.sh fi - | if [[ "$TRAVISCI" == "js" ]] ; then diff --git a/bin/run-phpcs.sh b/bin/run-phpcs.sh new file mode 100755 index 00000000000000..b0478b6d8b0f55 --- /dev/null +++ b/bin/run-phpcs.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +# Note: Before this script can successfully run `phpcs`, you will need to +# install version 2.9.x. phpcs versions 3.x are not yet compatible with the +# WordPress coding standards, see: +# +# https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/718 +# +# The easiest way to download it is to download the .phar archive from the +# latest 2.9.x release on GitHub: +# +# https://github.com/squizlabs/PHP_CodeSniffer/releases +# +# For example: +# wget https://github.com/squizlabs/PHP_CodeSniffer/releases/download/2.9.1/phpcs.phar \ +# -O ~/bin/phpcs +# chmod +x ~/bin/phpcs +# +# If ~/bin is not in your $PATH, pick another directory that is. +# +# Then you must install the WordPress-Coding-Standards repository and tell +# `phpcs` where it lives. See instructions here: +# +# https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#standalone + +# Change to the expected directory +cd "$(dirname "$0")" +cd .. + +find . \ + -not \( -path './node_modules' \) \ + -not \( -path './vendor' \) \ + -name '*.php' \ +| xargs -d'\n' phpcs --standard=phpcs.ruleset.xml -s From 4f98731e447238471f6a55a098fee6b42bce61bf Mon Sep 17 00:00:00 2001 From: James Nylen Date: Fri, 2 Jun 2017 11:10:45 -0400 Subject: [PATCH 2/5] Remove the need for a separate script to run phpcs - Name the phpcs ruleset with a default filename - Whitelist PHP files to include in the checks --- .travis.yml | 2 +- bin/run-phpcs.sh | 34 ---------------------------------- phpcs.ruleset.xml => phpcs.xml | 7 +++++-- 3 files changed, 6 insertions(+), 37 deletions(-) delete mode 100755 bin/run-phpcs.sh rename phpcs.ruleset.xml => phpcs.xml (80%) diff --git a/.travis.yml b/.travis.yml index f6b2d2b5c2a495..25a9770092cf04 100644 --- a/.travis.yml +++ b/.travis.yml @@ -58,7 +58,7 @@ script: fi - | if [[ "$TRAVISCI" == "phpcs" ]] ; then - ./bin/run-phpcs.sh + phpcs -s fi - | if [[ "$TRAVISCI" == "js" ]] ; then diff --git a/bin/run-phpcs.sh b/bin/run-phpcs.sh deleted file mode 100755 index b0478b6d8b0f55..00000000000000 --- a/bin/run-phpcs.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/sh - -# Note: Before this script can successfully run `phpcs`, you will need to -# install version 2.9.x. phpcs versions 3.x are not yet compatible with the -# WordPress coding standards, see: -# -# https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/718 -# -# The easiest way to download it is to download the .phar archive from the -# latest 2.9.x release on GitHub: -# -# https://github.com/squizlabs/PHP_CodeSniffer/releases -# -# For example: -# wget https://github.com/squizlabs/PHP_CodeSniffer/releases/download/2.9.1/phpcs.phar \ -# -O ~/bin/phpcs -# chmod +x ~/bin/phpcs -# -# If ~/bin is not in your $PATH, pick another directory that is. -# -# Then you must install the WordPress-Coding-Standards repository and tell -# `phpcs` where it lives. See instructions here: -# -# https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#standalone - -# Change to the expected directory -cd "$(dirname "$0")" -cd .. - -find . \ - -not \( -path './node_modules' \) \ - -not \( -path './vendor' \) \ - -name '*.php' \ -| xargs -d'\n' phpcs --standard=phpcs.ruleset.xml -s diff --git a/phpcs.ruleset.xml b/phpcs.xml similarity index 80% rename from phpcs.ruleset.xml rename to phpcs.xml index db418a3af44071..0bfe565715b06b 100644 --- a/phpcs.ruleset.xml +++ b/phpcs.xml @@ -1,10 +1,13 @@ - - Generally-applicable sniffs for WordPress plugins + + Sniffs for WordPress plugins, with minor modifications for Gutenberg + gutenberg.php + ./phpunit + gutenberg.php From 7db42e428954966302c6893edf593432915309e7 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Fri, 2 Jun 2017 11:21:41 -0400 Subject: [PATCH 3/5] Add documentation for running phpcs locally --- docs/coding-guidelines.md | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/docs/coding-guidelines.md b/docs/coding-guidelines.md index fc06a5430f6d7f..9efa43af3cdb71 100644 --- a/docs/coding-guidelines.md +++ b/docs/coding-guidelines.md @@ -78,3 +78,45 @@ Example: */ import VisualEditor from '../visual-editor'; ``` + +## PHP + +We use +[`phpcs` (PHP\_CodeSniffer)](https://github.com/squizlabs/PHP_CodeSniffer) +with the +[WordPress Coding Standards ruleset](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards) +to run a lot of automated checks against all PHP code in this project. This +ensures that we are consistent with WordPress PHP coding standards. + +When making any changes to the PHP code in this project, it's recommended to +install and run `phpcs` on your computer. This is a step in our Travis CI +build as well, but it is better to catch errors locally. + +You will need to install `phpcs` version 2.9.x, because the 3.x versions are +not yet compatible with the WordPress coding standards. For more information see +[this issue](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/718). + +The easiest way to get `phpcs` is to download the .phar archive from the latest +2.9.x release on GitHub: +[PHP\_CodeSniffer releases](https://github.com/squizlabs/PHP_CodeSniffer/releases). + +For example: + +```sh +wget \ + https://github.com/squizlabs/PHP_CodeSniffer/releases/download/2.9.1/phpcs.phar \ + -O ~/bin/phpcs +chmod +x ~/bin/phpcs +``` + +(If `~/bin` is not in your `$PATH`, pick another directory that is.) + +Then you must install the `WordPress-Coding-Standards` repository and tell +`phpcs` where it lives. See instructions here: + +https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#standalone + +You should now be able to run `phpcs -s` from the root directory of this +project. (The `-s` option means "show sniff codes in all reports", or "show +the name of each rule that fails". This additional output makes it much easier +to determine the specific issue with your code.) From f071756906679e125a8c8fec25d5bde4cc7f07fd Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 2 Jun 2017 12:41:15 -0700 Subject: [PATCH 4/5] Add default command line args to rulset for -s and --extensions=php --- phpcs.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpcs.xml b/phpcs.xml index 0bfe565715b06b..d8c5fd7b485b30 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -7,6 +7,8 @@ gutenberg.php ./phpunit + + From 977dcc71f45cad19117ba68a2d7af8247f3c69a7 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Mon, 5 Jun 2017 11:12:17 -0400 Subject: [PATCH 5/5] Remove '-s' argument --- .travis.yml | 2 +- docs/coding-guidelines.md | 5 +---- phpcs.xml | 5 +++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 25a9770092cf04..dae3085e5b875b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -58,7 +58,7 @@ script: fi - | if [[ "$TRAVISCI" == "phpcs" ]] ; then - phpcs -s + phpcs fi - | if [[ "$TRAVISCI" == "js" ]] ; then diff --git a/docs/coding-guidelines.md b/docs/coding-guidelines.md index 9efa43af3cdb71..d52c747d43c047 100644 --- a/docs/coding-guidelines.md +++ b/docs/coding-guidelines.md @@ -116,7 +116,4 @@ Then you must install the `WordPress-Coding-Standards` repository and tell https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#standalone -You should now be able to run `phpcs -s` from the root directory of this -project. (The `-s` option means "show sniff codes in all reports", or "show -the name of each rule that fails". This additional output makes it much easier -to determine the specific issue with your code.) +You should now be able to run `phpcs` from the root directory of this project. diff --git a/phpcs.xml b/phpcs.xml index d8c5fd7b485b30..5dbac182d815b8 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -5,11 +5,12 @@ - gutenberg.php - ./phpunit + gutenberg.php + ./phpunit + gutenberg.php