Skip to content

Instantly share code, notes, and snippets.

@angeloskath
Created December 4, 2014 21:27
Show Gist options
  • Save angeloskath/624c32b897118069438b to your computer and use it in GitHub Desktop.
Save angeloskath/624c32b897118069438b to your computer and use it in GitHub Desktop.
PHP style checking pre-commit hook
#!/bin/bash
CHANGED_FILES=`git diff-index --name-only --cached HEAD`
SUCCESS=1
FILES=""
for f in $CHANGED_FILES
do
# check file f for style errors
TMP=`php-cs-fixer fix --level=psr2 --dry-run $f | head -n -1 | sed "s/ \+/ /" | cut -d " " -f 3`
# check if we have an error
TMPC=`echo "$TMP" | wc -c`
if [ "$TMPC" -ne "1" ]
then
SUCCESS=0
FILES="$FILES - $TMP\n"
fi
done
if [ "$SUCCESS" -eq "1" ]
then
exit 0
else
echo -e ""
echo -e "\e[41m \e[49m"
echo -e "\e[41m \e[97mThere are style errors\e[39m \e[49m"
echo -e "\e[41m \e[49m"
echo ""
echo -e "Run \"\e[32mphp-cs-fixer fix --config=config.php_cs\e[39m\""
echo ""
echo "Incorrect files:"
echo -e "$FILES"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment