TODO
Created
August 4, 2017 12:30
-
-
Save yvh/a8764b518f56a94059a9c3078362a530 to your computer and use it in GitHub Desktop.
Git pre-commit with PHPLint, PHP-CS-Fixer ans PHP Code Sniffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Author: Yannick Vanhaeren | |
# Github : https://gist.github.com/yvh/a8764b518f56a94059a9c3078362a530 | |
# Declare functions | |
# ========================================================== | |
# PHPLint check | |
function phplint_check() { | |
local command="php -l -d display_errors=0" | |
echo -en "${GREEN}Checking PHPLint...${RESET}" | |
for file in ${files_list[@]}; do | |
script -eq ${TMP_FILE} -c "${command} ${file}" &> /dev/null | |
if [ ${?} -gt 0 ]; then | |
echo && tail -n +2 ${TMP_FILE} | |
echo -e "${RED}Fix the error before commit.${RESET}" | |
exit ${?} | |
fi | |
done | |
echo -e "${GREEN}Done!${RESET}" | |
} | |
# PHP CS Fixer check | |
function php_cs_fixer_check() { | |
local command="./bin/php-cs-fixer fix --config=.php_cs -v --using-cache=no --path-mode=intersection" | |
echo -en "${GREEN}Running PHP CS Fixer...${RESET}" | |
script -eq ${TMP_FILE} -c "${command} --dry-run -- ${files_list[*]}" &> /dev/null | |
if [ ${?} -gt 0 ]; then | |
echo && tail -n +2 ${TMP_FILE} | |
echo -e "${RED}Fix the error before commit.${RESET}" | |
echo -e "${RED}Run${RESET}" | |
echo -e "\t${ITALIC}${RED}${command} -- ${files_list[*]}${RESET}" | |
echo -e "${RED}to try to fix automatically or fix it manually.${RESET}" | |
read -p "Try to fix automatically? [n]" auto_fix | |
auto_fix=${auto_fix:-"n"} | |
case ${auto_fix} in | |
[Yy]*) | |
${command} -- ${files_list[*]} &> /dev/null | |
php_cs_fixer_check | |
;; | |
[Nn]*) | |
exit ${?} | |
;; | |
esac | |
fi | |
echo -e "${GREEN}Done!${RESET}" | |
} | |
# PHP Code Sniffer check | |
function php_code_sniffer_check() { | |
local command="./bin/phpcs -np --standard=Symfony --encoding=utf-8" | |
echo -en "${GREEN}Running Code Sniffer...${RESET}" | |
script -eq $TMP_FILE -c "${command} ${files_list[*]}" &> /dev/null | |
if [ ${?} -gt 0 ]; then | |
echo && tail -n +2 ${TMP_FILE} | |
echo -e "${RED}Fix the error before commit.${RESET}" | |
echo -e "${RED}Run${RESET}" | |
echo -e "\t${ITALIC}${RED}${command} ${files_list[*]}${RESET}" | |
echo -e "${RED}to try to fix automatically or fix it manually.${RESET}" | |
read -p "Try to fix automatically? [n]" auto_fix | |
auto_fix=${auto_fix:-"n"} | |
case ${auto_fix} in | |
[Yy]*) | |
${command} ${files_list[*]} &> /dev/null | |
php_code_sniffer_check | |
;; | |
[Nn]*) | |
exit ${?} | |
;; | |
esac | |
fi | |
echo -e "${GREEN}Done!${RESET}" | |
} | |
# Script | |
# ========================================================== | |
# Init vars | |
tabs 4 | |
declare -r RESET=$(tput sgr0) # Reset color | |
declare -r RED=$(tput setaf 1) # Red color | |
declare -r GREEN=$(tput setaf 2) # Green color | |
declare -r ITALIC=$(tput sitm) | |
declare -r GIT_HOOKS_DIR="$(git rev-parse --git-dir)/hooks" | |
declare -r PROJECT_DIRECTORY="$GIT_HOOKS_DIR/../.." | |
declare -r TMP_FILE="/tmp/tmp$$.out" | |
cd $PROJECT_DIRECTORY | |
# Get staged files | |
declare -r COMMIT_SCA_FILES=($(git diff --cached --name-only --diff-filter=ACMRTUXB HEAD -- . ':!vendor' ':!app/check.php' ':!app/SymfonyRequirements.php' | grep \.php)) | |
# Determine if a file list is passed | |
declare files_list=($@) | |
if [ -z $files_list ]; then | |
files_list=(${COMMIT_SCA_FILES[@]}) | |
fi | |
if [ ${#files_list[@]} -gt 0 ]; then | |
# PHPLint check | |
phplint_check | |
# PHP CS Fixer check | |
php_cs_fixer_check | |
# PHP Code Sniffer check | |
php_code_sniffer_check | |
fi | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment