Drupal coder and codesniffer will help you to check the coding standards in your custom code. Here is the steps for install and run the coding violation test on your command line.
Steps
-
Install the Coder module as a dev requirement, along with the Composer installer for PHP_CodeSniffer coding standards:
$ composer require --dev drupal/coder dealerdirect/phpcodesniffer-composer-installer
-
Verify the installation is working:
$ ./vendor/bin/phpcs -i
(should list Drupal and DrupalPractice in its output)
-
Once it's working, you can start using phpcs which was installed into the vendor/bin directory:
./vendor/bin/phpcs \ --standard="Drupal,DrupalPractice" -n \ --extensions="php,module,inc,install,test,profile,theme,css,txt,md,yml" \ web/themes/harivenu \ web/modules/custom
Additional information
-
List enabled Drupal coding standards rules
$ phpcs --standard="Drupal" -e
-
Check only one rule
$ phpcs --standard="Drupal" --extensions="php,module,inc,install,test,profile,theme,css,txt,md,yml" --sniffs=Drupal.Classes.UnusedUseStatement web/modules/custom
-
Check Drupal best practices
$ phpcs --standard="DrupalPractice" --extensions="php,module,inc,install,test,profile,theme,css,txt,md,yml" web/modules/custom
-
Automatically fix coding standards
$ phpcbf --standard="Drupal" --extensions="php,module,inc,install,test,profile,theme,css,txt,md,yml" web/modules/custom