I am sure that your code is clean and well maintained, but are you sure that you are following the standards all the time? If not, I have a solution. PHP Code Sniffer is a useful tool that will help you keep your code clean and standards compliant.
Last week I’ve published a post about combining Xdebug, Docker and PhpStorm (if you are interested in this topic check it out here -> xdebug), this time we will do it with PHP Code Sniffer.
Docker & Code Sniffer
Let us start with creating a custom docker container. Why a custom container instead of putting Code Sniffer into existing container, for example with php? I like to keep everything separated, that is the point of docker containers 😉
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 https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar && \ cp /tmp/phpcs.phar /usr/local/bin/phpcs && \ chmod +x /usr/local/bin/phpcs
As we will use this container only for Code Sniffer we base it on alpine – a very lightweight linux image. Next, we install some required libraries and download the phpcs.phar file with curl.
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
The only thing you have to know about this configuration is that 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 Code Sniffer will create a copy of it and upload it to some directory. Then it will use this path as the param for the phpcs script. If we do not do this then code sniffer will not see the file inside container.
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
Before we proceed to setting up PhpStorm, we first 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
First we need to create a small shell script. Why? Because PhpStorm needs a path to Code Sniffer library and as we do not have it on our host, but in the docker container, we will make a little hack 😉
#!/bin/bash docker exec -i local_custom phpcs "$@"
It is very simple, we just execute phpcs command on local_custom container, where $@ are the arguments from PhpStorm.
We can proceed with setting up PhpStorm. First go to Languages & Frameworks -> PHP -> Code Sniffer
and click on … button. By default we should have a Local config with empty path.
Click on the folder icon and select the phpcs.sh script. You can also click on the Validate button to check if everything is ok. Apply the changes.
Last thing is to enable Code Sniffer inspections. Go to Editor -> Inspections and filter the list by code sniffer
We have to check the checkbox next to PHP Code Sniffer validation and select the Coding standard. If you do not have any options in the Coding standard checkbox, click the refresh button. Apply the changes.
Finally we can check how our Code Sniffer 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!