Search code examples
phpdockerdocker-composedockerfilephpcodesniffer

Docker CodeSniffer - phpcbf doesn't change files


Hello i have a problem with phpcbf. Here is my docker configuration:

docker-compose.yml

version: "2"
services:    
    data:
        build: docker/application
        volumes:
            - .:/var/www/html
    fpm:
        build: docker/php-fpm
        volumes_from:
            - data
    nginx:
        build: docker/nginx
        ports:
            - "80:80"
        links:
            - fpm
        volumes_from:
            - data

docker/application/Dockerfile

FROM debian:jessie

ADD ./ /var/www/html

docker/php-fpm/Dockerfile

FROM php:fpm

#Install PHP_CodeSniffer
RUN apt-get update && apt-get install -y wget && apt-get install -y vim && \
    wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar && \
    wget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar

RUN chmod u+x *.phar

RUN mv phpcbf.phar /usr/local/bin/phpcbf
RUN mv phpcs.phar /usr/local/bin/phpcs

RUN phpcs --config-set default_standard PSR2

RUN apt-get remove -y wget

docker/nginx/Dockerfile

FROM nginx:latest

ADD default.conf /etc/nginx/conf.d/

docker/nginx/default.conf

server {
    listen 80;
    root /var/www/html;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/.+\.php(/|$) {
        fastcgi_pass fpm:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}

phpcs index.php - test file

root@6ad6c7f34641:/var/www/html# phpcs index.php                                                                                                                                                        

FILE: /var/www/html/index.php
----------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 3 LINES
----------------------------------------------------------------------
 3 | ERROR | [ ] Each class must be in a namespace of at least one
   |       |     level (a top-level vendor name)
 3 | ERROR | [x] Opening brace of a class must be on the line after
   |       |     the definition
 6 | ERROR | [x] Opening brace should be on a new line
 9 | ERROR | [x] Expected 1 newline at end of file; 0 found
----------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 48ms; Memory: 4Mb

And auto fix test which will do nothing. File is unchanged.

root@6ad6c7f34641:/var/www/html# phpcbf index.php                                                                                                                                                          
Changing into directory /var/www/html
Processing index.php [PHP => 47 tokens in 9 lines]... DONE in 8ms (3 fixable violations)
        => Fixing file: 0/3 violations remaining [made 3 passes]... DONE in 4ms 
sh: 1: patch: not found
Array
(
)
Returned: 127
Time: 103ms; Memory: 4Mb

Can you help me where and what i miss? Thank you


Solution

  • Problem solved: The error indicates that I don't have installed the patch command, which PHPCBF uses to apply changes in one action.

    docker/php-fpm/Dockerfile

    FROM php:fpm
    
    RUN apt-get update && \
    apt-get install -y wget && \
    apt-get install -y vim && \
    apt-get install -y patch && \
    wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar && \
    wget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
    
    RUN chmod u+x *.phar
    
    RUN mv phpcbf.phar /usr/local/bin/phpcbf
    RUN mv phpcs.phar /usr/local/bin/phpcs
    
    RUN phpcs --config-set default_standard PSR2
    
    RUN apt-get remove -y wget