How to install Drupal coding standard checker for command line?
Submitted by webmasterCoder is a Drupal module to check for coding standard issues in custom modules and themes. It can check any files for coding standard issues according to Drupal specifications. This method uses latest composer and drush to install latest coder. Feel free to skip the following to corresponding parts you need.
-
Install composer
Check if composer is installed,
which composer
If this is empty, proceed with below command. Else skip.
curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer which composer
-
Install coder
composer global require drupal/coder sudo ln -s ~/.composer/vendor/bin/phpcs /usr/local/bin sudo ln -s ~/.composer/vendor/bin/phpcbf /usr/local/bin phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer
-
Install the latest version of Drush
If you have installed drush with package manager, remove it, as it would be probably old version.
sudo apt-get remove drush
Use the following to install latest drush.
Add the line export PATH="$HOME/.composer/vendor/bin:$PATH" to end of ~/.bashrcsource ~/.bashrc composer global require drush/drush:dev-master drush --version
If it shows drush version, drush is installed correctly.
-
Download coder module
drush pm-download coder --destination=$HOME/.drush drush cache-clear drush
-
Install PHP Code Sniffer
composer global require squizlabs/PHP_CodeSniffer:\>=2 phpcs --version
Now you can check standards like this.
phpcs --standard=Drupal /path/to/example.module
Make Alias
Its better to use an alias for drupal itself, like drupalcs. Follow the steps below for it.
echo $SHELL vi ~/.bashrc
Append the following lines to it.
alias drupalcs="phpcs --standard=Drupal --extensions='php,module,inc,install,test,profile,theme,js,css,info,txt'" alias drupalcs="phpcs --standard=DrupalPractice --extensions='php,module,inc,install,test,profile,theme,js,css,info,txt'"
Restart the terminal or use
source ~/.bashrc
You can now use like this.
drupalcs /path/to/module
Or for a stricter compliance,
drupalcsp /path/to/module
Hope this helps. Happy Coding. Also, feel free to get in touch with us if any queries.