1#!/bin/sh
2# codesniffer pre-commit hook based on https://gist.github.com/fdemiramon/0423b4308218d417fbf3
3
4PROJECT=`pwd`
5STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`
6
7# Determine if a file list is passed
8if [ "$#" -eq 1 ]
9then
10    oIFS=$IFS
11    IFS='
12    '
13    SFILES="$1"
14    IFS=$oIFS
15fi
16SFILES=${SFILES:-$STAGED_FILES_CMD}
17
18echo "Checking PHP Lint..."
19for FILE in $SFILES
20do
21    php -l -d display_errors=0 $PROJECT/$FILE
22    if [ $? != 0 ]
23    then
24        echo "Fix the error before commit."
25        exit 1
26    fi
27    FILES="$FILES $PROJECT/$FILE"
28done
29
30if [ "$FILES" != "" ]
31then
32    echo "Running Code Sniffer..."
33    phpcs -p $FILES
34    if [ $? != 0 ]
35    then
36        echo "Coding standards errors have been detected. Running phpcbf..."
37        phpcbf -p $FILES
38        git add $FILES
39        echo "Running Code Sniffer again..."
40        phpcs -p $FILES
41        if [ $? != 0 ]
42        then
43            echo "Errors found not fixable automatically"
44            exit 1
45        fi
46    fi
47fi
48
49exit $?
50