Skip to content

Instantly share code, notes, and snippets.

@pespantelis
Last active January 11, 2019 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pespantelis/d68eac31ca675ac97481e3e0720abfc5 to your computer and use it in GitHub Desktop.
Save pespantelis/d68eac31ca675ac97481e3e0720abfc5 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}"
composer lint -d $(git rev-parse --show-toplevel) -- --dry-run >& /dev/null
#
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