Did you know that you could get much of the feedback given by the VIP team shown to you in real-time, as you code?
PHP CodeSniffer (aka PHPCS) is a tool that will help you write VIP approved code by ensuring it meets VIP coding standard. Many IDEs and text editors (e.g. PHPStorm, Sublime Text, Atom and Vim) have packages which will highlight any code which departs from VIP coding standards, or which has security or performance issues. Running this tool in your development environment or code editor allows you to fix the errors as you code, helping you develop to VIP best practices.
To install PHP CodeSniffer:
- install PHP CodeSniffer: https://github.com/squizlabs/PHP_CodeSniffer#installation. As of 2017-06-27, the WordPress Coding Standards are not compatible with PHPCS versions 3+. Until 3+ support is added, we recommend installing PHPCS using the latest 2.9.X phar files from GitHub, like so: $ curl -OL https://github.com/squizlabs/PHP_CodeSniffer/releases/download/2.9.1/phpcs.phar
- next, install the WordPress PHP CodeSniffer rules here: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#installation
When working on an existing codebase or when the WordPress coding standard does not match your team’s coding standards, you can take advantage of using the WordPress.com VIP Minimum standard for PHPCS which is also being used for automated code scanning in the Deploy Queue.
You can then integrate PHP CodeSniffer inside your favourite IDE, we have examples for a few popular ones:
- Sublime Text:
- Atom editor:
It’s also possible to run PHP CodeSniffer in your Continuous Integration build process, e.g. via Travis or Circle CI, which allows you to see issues reported against any pull requests and to receive reports of issues via email and other channels. If you’d like us to configure the rules to run on your VIP Go GitHub repository, then please let us know and we’ll be happy to set this up for you.
When submitting your code to VIP for the initial theme we expect the code to not trigger any PHP CodeSniffer blockers and as few warnings as possible. We also expect it to pass the WordPress.com VIP coding standard. When working on an existing codebase we expect the code to pass the WordPress.com VIP minimum standard but strongly encourage you to follow the WordPress.com VIP standards.
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:
phpcs --config-set installed_paths [/path/to/standard],[path/to/standard]
(note the comma separating each standard)