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 😉

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

Configuration

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

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 /private.

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

docker-compose up

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

docker run

.

PhpStorm

Shell Script

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 😉

scripts/phpcs.sh

#!/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.

Configuration

We can proceed with setting up PhpStorm. First go to Languages & Frameworks -> PHP -> Code Sniffer

phpstorm code sniffer

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

phpstorm code sniffer validate

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

phpstorm code sniffer editor inspections

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.

Verification

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.

phpstorm code sniffer verify

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

5 comments on “Code Sniffer + Docker + PhpStorm

  1. Hi
    Thanks for your article
    I took your files an create a new PhpStorm project
    Here is the error I get (in PhpStorm event log) :
    12:48 PHP Code Sniffer
    phpcs: ERROR: The file “/tmp/phpcs_temp.tmp/test.php” does not exist.
    Run “phpcs –help” for usage information
    Exclude test.php from PHP Code Sniffer analysis.

    The volume is well shared in docker-compose.yml
    Could be due to a permission problem?
    Have you ever encountered this problem and how to fix it?
    Thanks

    Configuration :
    – VM VirtualBox with xubuntu 18.04
    – Docker version 18.06.1-ce, build e68fc7a
    – PhpStorm 2018.3

    1. Same issue here, have you ever found a solution ? maybe because phpstorm creates the tmp file in host /tmp and not shared with docker ?

      1. Hey! We had a conversation with Flo by email. His issue was that he was using Docker Snap. If you do not use it, there can be few reasons, as I wrote in my post the “/tmp” folder on host has to be shared with docker container I did it by typing
        volumes:
        - /tmp:/tmp

        in docker-compose.yml file. Please check those two things, if you still have problems you can email me with the details and I will try to help 😉

    1. Hi, as we discussed in email conversation, unfortunately I don’t have access to a windows machine so I can’t help with that. Mr V resolved that by installing xamp and there installing phpcs globally.

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.