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:
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.
|Description||Fix PHP code sniffer warnings|
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 Tools→PhpCbf
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 Settings → Keymap
- Search here External Tools → PhpCbf
- Сlick on the green pencil
- Select here "keyboard shortcut"
- And add your shortcut, for example, I use "Alt + B"