31

Guide to PhpStorm code quality checks via PhpCS | Damian Mąsior

 4 years ago
source link: https://dmasior.com/guide-to-phpstorm-code-quality-checks-via-phpcs/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Share

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

phpcs.gif

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:

Screen-Shot-2019-12-29-at-14.36.08-1024x770.png

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

Screen-Shot-2019-12-29-at-14.45.45-1024x317.png

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

Screen-Shot-2019-12-29-at-14.49.15-1024x350.png

Click ok. It should look similar to this:

Screen-Shot-2019-12-29-at-14.57.34-1024x771.png

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

Screen-Shot-2019-12-29-at-16.10.39-1024x249.png

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: Screen-Shot-2019-12-29-at-16.03.14.png

Screen-Shot-2019-12-29-at-15.53.04-1024x295.png

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

Screen-Shot-2019-12-29-at-15.56.15-1024x716.png

When selected, press OK. You should see config:

Screen-Shot-2019-12-29-at-15.59.51-1024x753.png

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:

Screen-Shot-2019-12-29-at-16.35.06-1024x511.png

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).

Screen-Shot-2019-12-29-at-16.37.49-1024x877.png

Example custom ruleset location:

Screen-Shot-2019-12-29-at-16.47.59-1-1024x324.png

Keep in mind it is location inside docker container

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

Screen-Shot-2019-12-29-at-16.53.49-1024x176.png

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

Thanks for reading!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK