Skip to main content

How to configure CodeSniffer for Drupal in PhpStorm

Dec 9, 2021
code-like

In this article, I will give you a step-by-step guide on how to configure CodeSniffer for PhpStorm + Docksal. For Docker it should not be a big difference in settings.

“I'd like to show you how to set up tools to help make your code presentable.

no-more-s
CodeSniffer in realtime

CodeSniffer will continuously check your code for standards inconsistencies and mark it as warnings in the IDE. You should pay attention to the fact that this is a fairly resource-intensive process, and you should have a pretty powerful computer.
First, you need to install the PHP_CodeSniffer and Drupal coder libraries: `fin composer require squizlabs/php_codesniffer drupal/coder`.

img-6

Now we can go directly to the "CodeSniffer settings" in PhpStorm.
Go to settings `Ctrl+Alt+S` > PHP and add a PHP interpreter.

phpcs_1-min

Add an interpreter following the instructions below.

phpcs_2-min
phpcs_4-min

You have to select the cli-image of the project in "Image name". If you are not sure, or you don’t know what to choose here, then you can find out it using this command: `fin config | grep "image: docksal/cli"`. If you get nothing, then you need to check the file `.docksal/docksal.env`, also `.docksal/docksal-local.env` and `.docksal/services/cli/Dockerfile`, if they exist.
If everything is done correctly, you will see the following screen. Press OK.

phpcs_4-min

Note that the PhpStorm has set the project path as `/opt/project`, but this doesn’t work for Docksal. You need to edit that path and set `/var/www`.

phpcs_5-min
phpcs_6-min
phpcs_7-min
phpcs_8-min

If done correctly, the path should change here.

phpcs_9-min

After setting up the PHP interpreter, it will appear on the select list here: PHP > Quality Tools. Select the interpreter in the select list you just added and click "Apply".

phpcs_10-min

Now you need to add paths for CodeSniffer. To do this, in the same modal dialog, you should go to the configuration.

phpcs_11-min
phpcs_12-min
phpcs_13-min

PhpStorm has already prescribed all the paths, so click "Validate" and after a few seconds you will see a message below saying which version of CodeSniffer is installed. Click "Apply".

phpcs_14-min

Next, you need to go to PHP_CodeSniffer Inspection on the same modal window:

phpcs_15-min

Now you need to:

  1. enable the checkbox for CodeSniffer;
  2. check the box for "Installed standards path";
  3. enter the path to the standards in the library drupal/coder in the vendor folder: `/var/www/vendor/drupal/coder/coder_sniffer`.
  4. Please note that the project home directory for Docksal is `/var/www`, so the path to the "vendor" directory should also begin with `/var/www`;
  5. Click OK.
phpcs_16-min

Going in "PHP_CodeSniffer inspection" again.

phpcs_17-min
“And look, its working!”

Now the standards have been added to the select list, and we choose the Drupal standard.

phpcs_18-min

Also note that more file types can be added here. I added "module" and "theme".

phpcs_19-min

Click OK on all modal windows.

You have set up CodeSniffer in PhpStorm! ???????????? You can now open any class/file of your module and press `Alt+6`. You will be shown all the problems that PhpStorm + CodeSniffer found. The code will be underlined as well.

Disable CodeSniffer checking in realtime

To disable CodeSniffer, just uncheck one checkbox here.

phpcs_20-min
please
Enjoying the article?

Consider subscribing to our social media.

We much appreciate it.

CodeSniffer in manual mode

If you have Docksal, but you don’t use PhpStorm or don’t want to load the processor with continuously checking for standards, then there is an option of using CodeSniffer in manual mode.

For convenience, put the addon: `fin addon install phpcs`.

A script to manually run the Drupal standards check with CodeSniffer will be placed in the ".docksal" directory of your project.

phpcs_21-min

To run the script, enter the command: `fin phpcs cs {FILE/DIR PATH}`.
The console will display the non-compliance with the standards code, which CodeSniffer will find in the file or directory (recursively).
 

phpcs_22-min

If there is no error in the file, nothing will be displayed in the console.
Another tool worth mentioning is the PHP Code Beautifier and Fixer. It can fix some minor inaccuracies in the code such as wrong indents, extra spaces, etc.
It can be launched with the following command: `fin phpcs cbf {FILE/DIR PATH}`.

phpcs_23-min

Actually, if you work in PhpStorm, this tool will not be very useful, because PhpStorm has a built-in code formatting function called by the `Ctrl+Alt+L` hotkey (additional function settings are called by `Ctrl+Alt+Shift+L`).

Conclusion

So, I hope this article helped you and your code will always be clean and well documented! :)

Clean code everywhere, codesniffer