brightnucleus/contracts

View on GitHub
.pre-commit

Summary

Maintainability
Test Coverage
#!/bin/bash

# PHP to use.
php=`which php`

# Get name of the project (probably topmost directory name).
projectname="${PWD##*/}"

# The code standard to use.
codestandard='PSR2'

# create empty errors array
declare -a errors

# Check if we're on a semi-secret empty tree
if git rev-parse --verify HEAD
then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# fetch all changed php files and validate them
files=$(git diff-index --name-only --diff-filter=ACMR $against | grep '\.php$')
if [ -n "$files" ]; then

    for file in $files; do

        echo
        echo -e "  + Checking the PHP files of \033[1;33m${projectname}\033[1;0m for syntax errors..."

        # first check if they are valid php files
        output=`$php -l $file | grep 'Errors parsing'`

        # if it did contain errors, we have output
        if [ -n "$output" ]; then
            echo -e "\033[1;31m$file contains php syntax errors\033[1;0m"
            echo
            errors=("${errors[@]}" "$output")
        fi

        echo
        echo -e "  + Validating the PHP files of \033[1;33m${projectname}\033[1;0m for adherence to code standards..."

        # checks if the phpcs output contains '| ERROR |'
        output=`vendor/bin/phpcs --standard=$codestandard --extensions=php --encoding=utf8 --report=full $file | grep '| ERROR |'`

        # if it did contain errors, we have output
        if [ -n "$output" ]; then
            echo -e "\033[1;31m$file fails coding standards\033[1;0m"
            echo
            phpcs --standard=PSR2 --extensions=php --encoding=utf8 --report=full $file
            errors=("${errors[@]}" "$output")
        fi
    done
fi

# if we have errors, exit with 1
if [ -n "$errors" ]; then
    echo -e "  + \033[1;31mABORTING COMMIT\033[1;0m"
    echo
    exit 1
fi

echo -e '  + \033[1;32mNo errors found!\033[1;0m'
echo
exit 0