Skip to content

Instantly share code, notes, and snippets.

@AlekseyKorzun
Last active February 2, 2021 11:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlekseyKorzun/6099219 to your computer and use it in GitHub Desktop.
Save AlekseyKorzun/6099219 to your computer and use it in GitHub Desktop.
Pre-commit hook that supports PHPCS and PHPDOC
#!/bin/bash
################################################################
# Pre commit hook for git
#
# @package: utilities
# @subpackage: utilities\git
# @author: Aleksey Korzun <al.ko@webfoundation.net>
################################################################
# Specify full path to your PHP executable
PHP="php"
# Specify full path to your PHP Code Sniffer installation
PHP_CS="utilities/phpcs.php"
# Specify full path to your PHP Documentator installation
PHP_DOC="utilities/phpdoc.php"
# Extension of your PHP files
EXTENSION="\.php";
# PHP Code Sniffer
for FILE in `git diff --cached --name-only | grep -i $EXTENSION`; do
# Make sure file exists
if [ ! -f $FILE ]; then
continue
fi
echo ""
echo "Verifying $FILE against internal styling guide...";
$PHP $PHP_CS $FILE
if [ $? -eq 1 ]; then
exit 1
fi
echo "Done";
done
# PHP Documentator
for FILE in `git diff --cached --name-only | grep -i $EXTENSION`; do
# Make sure file exists
if [ ! -f $FILE ]; then
continue
fi
echo ""
echo "Verifying $FILE against PHP Documentor...";
IFS=$'\n' DATA=( $(PHP $PHP_DOC parse --skip-save -f $FILE) )
if [[ "${#DATA[@]}" -gt 3 ]]; then
unset DATA[0]
unset DATA[1]
unset DATA[2]
if [[ "${DATA[3]}" == *"No page-level DocBlock"* ]]; then
unset DATA[3]
fi
if [[ "${#DATA[@]}" -ge 1 ]]; then
for line in "${DATA[@]}"
do
echo $line
done
exit 1
fi
fi
echo "Done";
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment