Last active
March 17, 2018 17:41
-
-
Save brunoric/36082088751817ec7cfc0d0bf6596ba0 to your computer and use it in GitHub Desktop.
PHPCS check on diff and pre-commit hook
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 | |
RED='\033[0;31m' # red color | |
YELLOW='\033[1;33m' # red color | |
NC='\033[0m' # no color | |
FILE=$1; | |
RELATIVE_FILE_PATH=$(realpath --relative-to=${PWD} ${FILE}); | |
STANDARD_LIST='SmartboxMagento'; | |
PHPCS_REPORT_JSON=; | |
PHPCS_DEFAULT='phpcs' | |
PHPCS_LOCATION=$2; | |
ERRORS_COUNT=0; | |
function diff_staged_file() | |
{ | |
git diff --unified=0 --cached $FILE; | |
} | |
function get_lines_changed() | |
{ | |
local LINES=; | |
local FILE_PATH=; | |
local ESC=$'\033'; | |
while read; do | |
if [[ $REPLY =~ ---\ (a/)?.* ]]; then | |
continue; | |
elif [[ $REPLY =~ \+\+\+\ (b/)?([^[:blank:]$ESC]+).* ]]; then | |
FILE_PATH=${BASH_REMATCH[2]}; | |
elif [[ $REPLY =~ @@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+)(,[0-9]+)?\ @@.* ]]; then | |
LINES=${BASH_REMATCH[2]}; | |
elif [[ $REPLY =~ ^($esc\[[0-9;]+m)*([\ +-]) ]]; then | |
# printf "$path:$LINES:$REPLY" | |
echo "$LINES"; | |
if [[ ${BASH_REMATCH[2]} != - ]]; then | |
((LINES++)); | |
fi | |
fi | |
done | |
} | |
function phpcs_get_report() | |
{ | |
local phpcsLocation=$PHPCS_DEFAULT; | |
if [[ ! -z $PHPCS_LOCATION ]]; then | |
phpcsLocation=$PHPCS_LOCATION; | |
fi | |
PHPCS_REPORT_JSON=$(git show :${RELATIVE_FILE_PATH} | ${phpcsLocation} -n --report=json --standard=$STANDARD_LIST --stdin-path=${FILE}); | |
} | |
function phpcs_parse_report() | |
{ | |
local LINE=$1; | |
local PARSER=" | |
\$report = <<<'REPORT' | |
$PHPCS_REPORT_JSON | |
REPORT; | |
\$line = '$LINE'; | |
\$json = json_decode(utf8_encode(\$report), true); | |
if (\$json['totals']['errors'] == 0) | |
{ | |
return; | |
} | |
\$file = current(\$json['files']); | |
if (\$file['errors'] == 0) | |
{ | |
return; | |
} | |
foreach (\$file['messages'] as \$message) | |
{ | |
if (\$message['line'] == \$line) | |
{ | |
print \$message['message'] . PHP_EOL; | |
break; | |
} | |
if (empty(\$line)) | |
{ | |
print \$message['line'] . PHP_EOL; | |
} | |
} | |
"; | |
php -r "$PARSER"; | |
} | |
function get_lines_changed_with_error() | |
{ | |
local LINE=; | |
local RESULT=; | |
diff_staged_file | get_lines_changed | sort -u -g | | |
while IFS= read -r LINE | |
do | |
RESULT=$(phpcs_parse_report "$LINE" 2>&1); | |
if [[ -z $RESULT ]]; then | |
continue; | |
fi | |
if [ "$ERRORS_COUNT" -eq "0" ]; then | |
printf "${YELLOW}File: $FILE${NC}\n"; | |
fi | |
ERRORS_COUNT=$(expr $ERRORS_COUNT + 1); | |
printf "${RED}STANDARD ERROR at line: $LINE${NC}\n"; | |
printf "$RESULT\n"; | |
done | |
} | |
function run() | |
{ | |
phpcs_get_report; | |
get_lines_changed_with_error; | |
} | |
run; |
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
#!/bin/bash | |
ROOT_DIR=$(git rev-parse --show-toplevel) | |
#5mb FILESIZE | |
FILESIZELIMIT=5242880 | |
LIST=$(git diff-index --cached --name-only --diff-filter=ACMR HEAD) | |
ERRORS_BUFFER="" | |
echo "Checking PHP syntax ..." | |
for file in $LIST | |
do | |
EXTENSION=$(echo "$file" | grep ".php$") | |
if [ "$EXTENSION" != "" ] && [ $file != ".phpstorm.meta.php" ]; then | |
ERRORS_LINTER=$(php -l ${ROOT_DIR}/$file 2>&1 | grep "Parse error") | |
if [ "$ERRORS_LINTER" != "" ]; then | |
if [ "$ERRORS_BUFFER" != "" ]; then | |
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS_LINTER" | |
else | |
ERRORS_BUFFER="$ERRORS_LINTER" | |
fi | |
fi | |
ERRORS_PHPCS=$(${ROOT_DIR}/bin/phpcs_check_errors.sh ${ROOT_DIR}/$file 2>&1 ${ROOT_DIR}/bin/phpcs) | |
if [ "$ERRORS_PHPCS" != "" ]; then | |
if [ "$ERRORS_BUFFER" != "" ]; then | |
ERRORS_BUFFER="$file\n$ERRORS_BUFFER\n$ERRORS_PHPCS" | |
else | |
ERRORS_BUFFER="$file\n$ERRORS_PHPCS" | |
fi | |
fi | |
fi | |
done | |
if [ "$ERRORS_BUFFER" != "" ]; then | |
echo "------------------------------------------------" | |
echo "These errors were found in try-to-commit files: " | |
echo -e "$ERRORS_BUFFER" | |
echo | |
echo "Can't commit, fix errors first." | |
echo "------------------------------------------------" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment