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.
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.
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
Running the container
Ok, now we need to launch the docker container. I am using docker-compose so in my case
is the way, but you can just run single container with
After this, we can proceed to PhpStorm.
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 🙂
#!/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.
We can go with setting up PhpStorm. First jump to Languages & Frameworks -> PHP -> Mess Detector
and click on … button. By default we should have a Local config with empty path.
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
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.
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.
And that is all 🙂 if you have any questions feel free to message me or just leave a comment. Thanks for reading!