PHP_CodeSniffer is a library that tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards. It works with Drupal 6, 7, or 8.
Coder contains "sniffs" for PHP CodeSniffer. These "sniffs" tell PHP CodeSniffer whether code meets Drupal coding standards or not.
1.First, make sure Composer is installed correctly:
$ which composer
If you get composer not found or similar, follow Composer's installation instructions.
2.Install Coder (8.x-2.x) in your global Composer directory in your home directory (~/.composer):
$ composer global require drupal/coder
3.To make the phpcs and phpcbf commands available globally,
nano ~/.bash_profile
4.Add export PATH="$PATH:$HOME/.composer/vendor/bin in the end and save.
5.Register the Drupal and DrupalPractice Standard with PHPCS:
$ phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer
6.Open nano ~/.bash_profile and add following lines.
alias drupalcs="phpcs --standard=Drupal --extensions='php,module,inc,install,test,profile,theme,js,css,info,txt'"
alias drupalcsfix="phpcbf --standard=Drupal --extensions='php,module,inc,install,test,profile,theme,js,css,info,txt'"
7.Run source ~/.bash_profile
8.Now you can use the alias like this:
$ drupalcs sites/all/modules/mymodule
$ drupalcsfix sites/all/modules/mymodule