Skip to content

Instantly share code, notes, and snippets.

@gpapadopg
Forked from pespantelis/php-cs-fixer-hook
Last active January 11, 2019 14:55
Show Gist options
  • Select an option

  • Save gpapadopg/2245397539cbec7103af35b833cd40cc to your computer and use it in GitHub Desktop.

Select an option

Save gpapadopg/2245397539cbec7103af35b833cd40cc to your computer and use it in GitHub Desktop.
PHP-CS-Fixer Git pre commit/push hook
#!/bin/sh
# if the script is running from a terminal, show colors
if [ -t 1 ]; then
STYLE_END="\033[m"
BLUE="\033[34m"
GREEN="\033[32m"
RED="\033[41m"
YELLOW="\033[43m\033[34m"
fi
# if php-cs-fixer is not installed or the execute permission is not granted, exit
if ! [ -x ~/.composer/vendor/bin/php-cs-fixer ]; then
echo ""
echo "${YELLOW} ${STYLE_END}"
echo "${YELLOW} Please install the PHP-CS-Fixer: ${STYLE_END}"
echo "${YELLOW} ${STYLE_END}"
echo "${YELLOW} composer global require friendsofphp/php-cs-fixer ${STYLE_END}"
echo "${YELLOW} ${STYLE_END}"
echo ""
exit 1
fi
echo "${BLUE}PHP-CS-Fixer hook triggered.${STYLE_END}"
DIR=$(git rev-parse --show-toplevel)
CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.php')
if [ -n "$CHANGED_FILES" ]; then
composer lint -d "$DIR" $CHANGED_FILES 2>&1 > /dev/null
git add $CHANGED_FILES
fi
if ! [ $? -eq 0 ]; then
echo ""
echo "${RED} ${STYLE_END}"
echo "${RED} PHP-CS-Fixer failed. Please run: ${STYLE_END}"
echo "${RED} ${STYLE_END}"
echo "${RED} composer lint ${STYLE_END}"
echo "${RED} ${STYLE_END}"
echo ""
exit 1
fi
echo "${GREEN}PHP-CS-Fixer finished successfully.${STYLE_END}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment