mess detector docker phpstorm

Are you looking for a tool that will help you maintain your code? PHP Mess Detector is definitely worth trying. It looks in your code for a potential problems such as possible bugs, sub optimal code, over complicated expressions or unused parameters, methods, properties. Setting it up with Docker and PHP Storm is pretty simple and similar to setting up Code Sniffer which I have already described in another post, if you did not read it, check it out -> code sniffer.

Docker & Mess Detector

Let us start with creating a docker container with the libraries that we need.

custom/Dockerfile

FROM alpine:3.5

RUN apk --no-cache add \
        ca-certificates \
        curl \
        php5-cli \
        php5-dom \
        php5-iconv \
        php5-phar \
        php5-xdebug \
        php5-zlib

WORKDIR /tmp

RUN curl -OL http://static.phpmd.org/php/latest/phpmd.phar && \
    cp /tmp/phpmd.phar /usr/local/bin/phpmd && \
    chmod +x /usr/local/bin/phpmd

We will use this container only for Mess Detector so we base it on alpine – a very lightweight linux image. Then we install some required libraries and download the phpmd.phar file with curl.

Configuration of docker-composer.yml

Now we have to add our container configuration to the docker-compose.yml file.
NOTE: If you do not use docker-compose you do not have to do it.

docker-compose.yml

version: "2"

services:
  custom:
    build: ./custom
    image: local_custom:latest
    container_name: local_custom
    tty: true
    volumes:
      - /tmp:/tmp

If you do not have any experience in the docker-copmose here is a short explanation – the build param is pointing to the folder with our custom Dockerfile and volumes is connecting /tmp dir on our drive to /tmp dir in the container. The volumes config is very important, because PhpStorm when checking the file with Mess Detector will create a copy of it and upload it to some directory. Then it will use this path as the param for the phpmd script.

NOTE: I am using Ubuntu 18.04 and the path were PhpStorm copies the files is /tmp. On macOS it will probably be /private.

Running the container

Ok, now we need to launch the docker container. I am using docker-compose so in my case

docker-compose up

is the way, but you can just run single container with

docker run

After this, we can proceed to PhpStorm.

PhpStorm

Shell Script

First we need to create a small shell script. Why? Because PhpStorm needs a path to Mess Detector tool and as we do not have it on our host but in the docker container, so we will make a script that will launch the library in the container 🙂

scripts/phpmd.sh

#!/bin/bash
docker exec -i local_custom phpmd "$@"

Simple as that 🙂 we just execute phpmd command on local_custom container, where $@ are the arguments from PhpStorm.

Configuration

We can go with setting up PhpStorm. First jump to Languages & Frameworks -> PHP -> Mess Detector

phpstorm messdetector

and click on button. By default we should have a Local config with empty path.

phpstorm messdetector validate

Click on the folder icon and select the phpmd.sh script. You can also click on the Validate button to check if everything is ok. Apply the changes.
Last thing is to enable Mess Detector inspections. Go to Editor -> Inspections and filter the list by mess detector

phpstorm messdetector editor inspections

We have to check the checkbox next to PHP Mess Detector validation and either select some of predefined Options or create a custom ruleset. Apply the changes.

Verification

Finally we can check how our Mess Detector works. Open any php file and let it work. If everything went well and you have some errors PhpStorm will underscore the places that require fixing.

phpstorm messdetector verification

And that is all 🙂 if you have any questions feel free to message me or just leave a comment. Thanks for reading!

4 comments on “Mess Detector + Docker + PhpStorm

  1. Nice article, by the way you could only create a
    phpmd.sh with this inside

    #!/bin/bash
    docker run -i –rm phpqa/phpmd “$@”

    1. Thanks @Maxim. Yes, sure, that is also a way. In my solution the container is working all the time and all we do is just to run a command inside it.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.