Skip to content

Instantly share code, notes, and snippets.

@linhmtran168
Last active August 29, 2015 14:19
Show Gist options
  • Save linhmtran168/11a7bb7b044f80f2b39e to your computer and use it in GitHub Desktop.
Save linhmtran168/11a7bb7b044f80f2b39e to your computer and use it in GitHub Desktop.
Pre-commit hook to check error and code style for php (using php lint and php code sniffer)
#!/bin/sh
STAGED_FILES=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`
# check for php
which php &> /dev/null
if [[ "$?" == 1 ]]; then
echo "\t\033[41mPlease install PHP.\033[0m"
exit 1
fi
# check for phpcs
./vendor/bin/phpcs &> /dev/null
if [[ ! -e ./vendor/bin/phpcs ]]; then
echo "\t\033[41mPlease install PHPCS.\033[0m"
exit 1
fi
PASS=true
#PHPLint
echo "Checking PHP Lint...\n"
for FILE in $STAGED_FILES
do
php -l -d display_errors=0 "$FILE"
if [[ "$?" != 0 ]]; then
echo "\t\033[41mPHPLint Failed: $FILE\033[0m"
PASS=false
fi
FILES="$FILES $FILE"
done
if ! $PASS; then
echo "\n\033[41mPHPLint Failed. Fix the errors and try again.\033[0m"
exit 1
fi
echo "Checking PHP Lint Completed!\n"
# PHPCS
if [[ "$FILES" != "" ]]; then
echo "Running Code Sniffer...\n"
./vendor/bin/phpcs --standard=PSR1 --encoding=utf-8 -n -p $FILES
echo "Running Code Sniffer Complted!\n"
if [ "$?" != 0 ]
then
echo "\n\033[41mPHPCS Failed. Fix the error before commit.\033[0m"
exit 1
fi
fi
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment