Guide to PhpStorm code quality checks via PhpCS


Let’s configure PHP Code Sniffer to investigate our code instantly and have error messages shown directly in PhpStorm IDE just like this:

Configure php interpreter

We will configure PhpStorm to use dockerized php-cli. To do so, we need its image. Just pull it using:

$ docker pull php:cli

Next, add a new interpreter in PhpStorm. Open configuration window: Preferences -> Languages & Frameworks -> PHP. You should see something similar to:

Click dotted button […] to add a new interpreter. In a new window click plus [+] sign and choose From docker, vagrant[..] :

In new popup choose Docker as remote PHP interpreter and use our previously downloaded image php:cli

Click ok. It should look similar to this:

Just proceed with OK. Now, cli interpreter should be configured properly:

Please note docker container volume with shared project files. By default it is {our_project_path} -> /opt/project. We not change it.

Quality tools: PhpCS

We can proceed to a sub-section Quality Tools of PhpStorm configuration:

When on it, click dotted button to open configuration window. Click plus [+] sign and select code sniffer by previously configured remote interpreter:

When selected, press OK. You should see config:

Please take into account that codesniffer path is a location inside container.

Apply all preferences windows with OK. It is time to configure inspections.

PhpStorm inspections

Open inspections window from right bottom corner of PhpStorm:

Search (1) for codesniffer. I suggest to change (2) and (4) to Error to get all phpcs violations marked seriously. Change checked file extensions to php (3). From coding standard (5) choose the one you prefer or (better) select custom if you have your own ruleset (6).

Example custom ruleset location:

Keep in mind it is location inside docker container

Close all windows with OK. PhpStorm editor should now display PhpCs violations!

Looking for some example PhpCS rulesets? Check out example using Slevomat coding standard in my Querabilis project.

Thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *