PHPSTORM Configuration of 'Quality Tools' in a Docker Image

Here's a question I recently pondered. Usually, I install QA tools directly on my host or in the Docker image. If tools are installed on my host, there's no problem configuring PhpStorm.

If the tools are directly configured in the Docker image, there's also no problem using them within the image. However, how can one combine the power of PhpStorm and Docker to link the two?

So, here’s how to proceed with the installation of php_codesniffer, Mess Detector, CS Fixer, PSalm, PhpStan.

image

For the purpose of our article, I'll give you some tips to add to your Dockerfile or to create one specially for your development workflow. Personally, I have a Dockerfile to manage all PHP versions which includes all the tools I need for setting up a development stack.

FROM php:7.4.31-apache-buster

ARG COMPOSER_HOME=/composer

RUN composer global require squizlabs/php_codesniffer
RUN composer global require phpmd/phpmd
RUN composer global require friendsofphp/php-cs-fixer  --with-all-dependencies
RUN composer global require vimeo/psalm
RUN composer global require phpstan/phpstan

You can use a special image for the CLI. Personally, I use an image with Apache. So, I mount my 'qa' image when I need it by overriding the default entrypoint with an infinite command.

version: "3.8"
services:
    qa:
        image: 'devpartitech/php:7.4-apache'
        tty: true
        environment:
            COMPOSER_HOME: '/composer'
        volumes:
          - "./:/var/www/"
        entrypoint: ["/bin/bash", "-c"]
        command: "tail -f /dev/null"

So you'll need to configure interpreters. For this, go to Settings > Quality Tools and we will choose the first one on the list, PHP_Code_Sniffer.

image-2

Once in the Interpreters configuration interface, click the “+” and select “From Docker

image-3

This is just an example that you can adapt as you wish. I'm going to use my Docker Compose directly.

image-4
image-5

Once created, you will have access to the configuration page. Before confirming, remember to execute the reload of your configuration so that it recognizes your PHP executable.

image-6
image-7

If, like me, you've chosen “/composer” as your COMPOSER_HOME, then you will find all the installed tools in /composer/vendor/bin. Therefore, you can specify (adjust if necessary) the following values:
PHP_Code_Sniffer path: /composer/vendor/bin/phpcs

Path to phpcbf: /composer/vendor/bin/phpcbf

Confirm to make sure everything is in order.

image-8

Let's continue with the next one on the list: Mess Detector. Just select the interpreter that's already configured.

image-9

Enter the interpreter and click on the "…".

image-11

Specify the path mapping: /composer/vendor/bin/phpmd and you're all set!

image-10

We continue with Mess detector. We always choose our configuration on qa-php-7.4 and click “...”. Then click on the "+".

image-13

Specify the PHP CS Fixer path: /composer/vendor/bin/php-cs-fixer and confirm.

image-14

Let's proceed with Psalm. We keep using our interpreter.

image-16
image-17

Enter the psalm path: /composer/vendor/bin/psalm and confirm.

image-18

We finish with the last one on the list, PHPStan. We activate it and click on “...”

image-19
image-20

Specify the PHPSTAN path: /composer/vendor/bin/phpstan, confirm, and you're good to go.

image-21

And there you have it! We have linked all of our quality tools properly to our Docker container.

You can now test that everything is working well before fine-tuning your settings.

image-22
image-23

In the list, unfold PHP > Quality tools and remember to check all of them:

image-24
image-25

Click on Analyze:

image-26

All the remarks will appear directly in the "Problems" tab of PhpStorm. Be careful if you've included your entire project. You may prefer to include only the src or exclude the vendor directory unless you want to be overwhelmed with notifications!

And that's it! Happy coding 😊