Skip to content

Instantly share code, notes, and snippets.

@ianlintner-wf
Last active February 23, 2017 21:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianlintner-wf/fcdfd342c5733577cf197cb92efad16d to your computer and use it in GitHub Desktop.
Save ianlintner-wf/fcdfd342c5733577cf197cb92efad16d to your computer and use it in GitHub Desktop.
PHP Code Sniffer / CI / Bamboo / Jenkins / Drupal / Check Style script
#!/bin/bash
# PHP CodeSniffer CI Task
#
# Based off of git pre-commit hook
# https://github.com/s0enke/git-hooks/tree/master/phpcs-pre-commit
#
# Export Variables to be set by CI Agent / Bot
# PHPCS_BIN=/usr/bin/phpcs
# PHPCS_CODING_STANDARD=PEAR
# PHPCS_IGNORE=
# REMOTE_BRANCH=
# CONFIG_FILE=
# PHPCS_IGNORE=
# PHPCS_SNIFFS=
# PHPCS_ENCODING=
# PHPCS_IGNORE_WARNINGS=
# PHPCS_EXTENSIONS=
# PHPCS_REPORT_CHECKSTYLE=
# PHPCS_MEMORY_LIMIT=
# FILE_PATH_FILTER
# grep pattern to match e.g. \/docroot\/sites\/all\/contrib
##
#PHPCS_BIN=/usr/bin/phpcs
#PHPCS_CODING_STANDARD=PEAR
#PHPCS_IGNORE=
# Values passed to git diff
currentBranch=$(git rev-parse --abbrev-ref HEAD)
compareBranch=$REMOTE_BRANCH
# parse config
CONFIG_FILE=$(dirname $0)/config
if [ -e $CONFIG_FILE ]; then
. $CONFIG_FILE
fi
# simple check if code sniffer is set up correctly
if [ ! -x $PHPCS_BIN ]; then
echo "PHP CodeSniffer bin not found or executable -> $PHPCS_BIN"
exit 1
fi
# this is the magic:
# retrieve all files in staging area that are added, modified or renamed
# but no deletions etc
FILES=$(git diff --diff-filter=AM --name-only $compareBranch $currentBranch)
if [ "$FILES" == "" ]; then
exit 0
fi
# match files against whitelist
FILES_TO_CHECK=""
for FILE in $FILES
do
echo "$FILE" | egrep -q "$PHPCS_FILE_PATTERN"
RETVAL=$?
if [ "$RETVAL" -eq "0" ]
then
FILES_TO_CHECK="$FILES_TO_CHECK $FILE"
fi
done
if [ "$FILE_PATH_FILTER" != "" ]; then
FILES_TO_CHECK==$(echo $FILES_TO_CHECK | grep "$FILE_PATH_FILTER")
fi
#echo $FILES_TO_CHECK
if [ "$FILES_TO_CHECK" == "" ]; then
exit 0
fi
# execute the code sniffer
if [ "$PHPCS_IGNORE" != "" ]; then
IGNORE="--ignore=$PHPCS_IGNORE"
else
IGNORE=""
fi
if [ "$PHPCS_SNIFFS" != "" ]; then
SNIFFS="--sniffs=$PHPCS_SNIFFS"
else
SNIFFS=""
fi
if [ "$PHPCS_ENCODING" != "" ]; then
ENCODING="--encoding=$PHPCS_ENCODING"
else
ENCODING=""
fi
if [ "$PHPCS_IGNORE_WARNINGS" == "1" ]; then
IGNORE_WARNINGS="-n"
else
IGNORE_WARNINGS=""
fi
if [ "$PHPCS_MEMORY_LIMIT" != "" ]; then
MEMORY_LIMIT="-d memory_limit=-1"
else
MEMORY_LIMIT=""
fi
if [ "$PHPCS_REPORT_CHECKSTYLE" != "" ]; then
REPORT_CHECKSTYLE="--report-checkstyle=$PHPCS_REPORT_CHECKSTYLE"
else
REPORT_CHECKSTYLE=""
fi
if [ "$PHPCS_EXTENSIONS" != "" ]; then
EXTENSIONS="--extensions=$PHPCS_EXTENSIONS"
else
EXTENSIONS="--extensions=php,module,inc,install,test,profile,theme,css,info"
fi
OUTPUT=$($PHPCS_BIN -v -s $IGNORE_WARNINGS $MEMORY_LIMIT --standard=$PHPCS_CODING_STANDARD $REPORT_CHECKSTYLE $EXTENSIONS $ENCODING $IGNORE $SNIFFS $FILES_TO_CHECK)
echo "$OUTPUT"
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo "$OUTPUT" | less
fi
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment