Skip to content

Instantly share code, notes, and snippets.

@yuki777
Last active August 29, 2015 14:16
Show Gist options
  • Save yuki777/771b48c50aeaf9db237a to your computer and use it in GitHub Desktop.
Save yuki777/771b48c50aeaf9db237a to your computer and use it in GitHub Desktop.
pre-commit, check-syntax, php-cs-fixer

pre-commit

#!/usr/bin/env bash
dir=`dirname realpath $0`

# Check php syntax
$dir/check-php-syntax
if [ $? -ne 0 ]; then
    message="Aborting commit due to files with syntax errors."
    echo -e "\033[0;31m$message\033[0;39m" >&2
    exit 1
fi

# Run php-cs-fixer
$dir/run-php-cs-fixer
if [ $? -ne 0 ]; then
    exit 1
fi

check-php-syntax

#!/usr/bin/env bash

root=$(git rev-parse --show-toplevel)
files=($(git diff --name-only --cached | egrep '\.php$' | sed -e "s|^|$root/|"))

for file in "${files[@]}"; do
    if [ -f "$file" ]; then
        /usr/bin/php -l "$file" > /dev/null
        if [ $? -ne 0 ]; then
            exit 1
        fi
    fi
done

run-php-cs-fixer

#!/usr/bin/env bash

root=($(git rev-parse --show-toplevel |sed -e "s|/usr/home|/home|"))
files=($(git diff --name-only --cached | egrep "\.php$" | sed -e "s|^|$root/|"))

for file in "${files[@]}"; do
    if [ -f "$file" ]; then
        /usr/bin/php ~/local/bin/php-cs-fixer fix $file --diff --verbose --dry-run
        if [ $? -ne 0 ]; then
            echo -------------------------------------------------------------------
            echo "# Run this command if you want to fix php-cs(PHP-Coding-Standards)."
            echo "/usr/bin/php ~/local/bin/php-cs-fixer fix $file --diff --verbose"
            echo "# Or add -n(--no-verify) option on commit, if you do NOT want to fix it."
            echo "git commit -v -n $file"
            echo -------------------------------------------------------------------
            exit 1
        fi
    fi
done

chmod

chmod 755 pre-commit
chmod 755 check-syntax
chmod 755 php-cs-fixer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment