Setting Up PHP_CodeSniffer for Drupal 7 and 8· Drupal 8 GSoC 2015 PHP_CodeSniffer Drupal Planet
My GSoC project requires me to work on both - Drupal 7 and 8 version of site_audit. So, I had to set up PHP_CodeSniffer such that it can work with both the versions of Drupal. The Coder standards are different for Drupal 7 and Drupal 8. This post deals with how to set up CodeSniffer in such a suituation. This will require installing coder module twice, once for Drupal 7 standards and other for Drupal 8 standards.
CodeSniffer can be installed using composer by the following command
composer global require squizlabs/PHP_CodeSniffer:\>=2 #make phpcs command globally availabe sudo ln -s ~/.composer/vendor/bin/phpcs /usr/local/bin
Installing Coder for Drupal 8
Coder provides the set of sniffs for CodeSniffer. We need two copies of this, one for Drupal 7 and other for Drupal 8. Install the first copy using Composer
composer global require drupal/coder
Installing Coder for Drupal 7
Install the second copy using drush
drush dl coder-7.x --destination=$HOME/.drush
.drushdirectory inside your home folder.
Creating Aliases for usage
Different aliases can be created for CodeSniffer with Drupal 7 standards and CodeSniffer with Drupal 8 standards. To create aliases, add the following into your
#drupalcs8 alias for testing Drupal 8 coding standards alias drupalcs8="$HOME/.composer/vendor/squizlabs/php_codesniffer/scripts/phpcs --standard=~/.composer/vendor/drupal/coder/coder_sniffer/Drupal --extensions='php,module,inc,install,test,profile,theme,js,css,info,txt'" #drupalcs7 for testing Drupal 7 coding standards alias drupalcs7="phpcs --standard=~/.drush/coder/coder_sniffer/Drupal --extensions='php,module,inc,install,test,profile,theme,js,css,info,txt'"
To test some code for Drupal 8 coding standards, run
drupalcs8 [path to file/folder to check]. Similarly,
drupalcs7 can be used to test for Drupal 7 coding standards.