First you need to install PHP CodeSniffer according to the instructions in the article above. 

PHP CodeSniffer includes a tool called phpcbf (PHP Code Beautifier and Fixer) that can automatically fix a lot of the issues that PHPCS discovers.

If you have installed PHP CodeSniffer by composer, check that phpcbf works correct:

~/.composer/vendor/bin/phpcbf -h

As result you will get phpcbf usage instructions.

For fixing PHPCS issues from terminal you can run this command:

~/.composer/vendor/bin/phpcbf --standard=Drupal path_to_file/example.module

PhpStorm does not work with phpcbf tool out of the box, so we will use it as an external tool.

Go to Settings → Tools → External Tools and add new tool.

Name PhpCbf
Description Fix PHP code sniffer warnings
Program /home/USER_NAME/.composer/vendor/bin/phpcbf
Parameters --standard=Drupal $FileDir$/$FileName$
Working directory $ProjectFileDir$

You can run this not only for an individual file, you can use it for directories. Check "Insert macro" button and use PhpStorm macros as program parameters.

Now try to open any drupal file with coding standards errors and run  phpcbf from menu Tools → External ToolsPhpCbf

As a result, you will see something like this:


For more detailed configuration you can set additional phpcbf parameters, for example excluded files and etc.

Also I have created hotkey to run this tool, for better usability:

  • Go to SettingsKeymap 
  • Search here External ToolsPhpCbf
  • Сlick on the green pencil
  • Select here "keyboard shortcut"
  • And add your shortcut, for example, I use "Alt + B"