Skip to content

Instantly share code, notes, and snippets.

@mariovalney
Last active November 11, 2024 12:13
Show Gist options
  • Save mariovalney/174760f2e4387bdeb4ade32986294cfb to your computer and use it in GitHub Desktop.
Save mariovalney/174760f2e4387bdeb4ade32986294cfb to your computer and use it in GitHub Desktop.
Pre-commit hook to check code with PHPCS, fix with PHPCBF and check mess with PHPMD
#!/bin/sh
#########################
# #
# Initializing #
# #
#########################
##
# You should add all tools as composer dependencies or change this path
#
# composer require phpmd/phpmd
# composer require squizlabs/php_codesniffer
##
PHPCS_BIN=./vendor/bin/phpcs
PHPCBF_BIN=./vendor/bin/phpcbf
# Check for PHPCS / PHPCBF
if [ ! -x $PHPCS_BIN ]; then
echo "[PRE-COMMIT] PHP CodeSniffer is not installed locally."
echo "[PRE-COMMIT] Please run 'composer install' or check the path: $PHPCS_BIN"
exit 1
fi
if [ ! -x $PHPCBF_BIN ]; then
echo "[PRE-COMMIT] PHP Code Beautifier and Fixer is not installed locally."
echo "[PRE-COMMIT] Please run 'composer install' or check the path: $PHPCBF_BIN"
exit 1
fi
PHPMD_BIN=./vendor/bin/phpmd
# Check for PHPMD
if [ ! -x $PHPMD_BIN ]; then
echo "[PRE-COMMIT] PHP Mess Detect is not installed locally."
echo "[PRE-COMMIT] Please run 'composer install' or check the path: $PHPMD_BIN"
exit 1
fi
#########################
# #
# Starting #
# #
#########################
# All files in staging area (Added | Created | Modified | Renamed)
PROJECT=$(git rev-parse --show-toplevel)
FILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep .php)
if [ "$FILES" != "" ]
then
# Coding Standards
echo "[PRE-COMMIT] Checking PHPCS..."
# You can change your PHPCS command here
$PHPCS_BIN -n $FILES
if [ $? != 0 ]
then
echo "[PRE-COMMIT] Coding standards errors have been detected."
echo "[PRE-COMMIT] Running PHP Code Beautifier and Fixer..."
# You can change your PHPCBF command here
$PHPCBF_BIN -n $FILES
echo "[PRE-COMMIT] Checking PHPCS again..."
# You can change your PHPCS command here
$PHPCS_BIN -n $FILES
if [ $? != 0 ]
then
echo "[PRE-COMMIT] PHP Code Beautifier and Fixer wasn't able to solve all problems."
echo "[PRE-COMMIT] Run PHPCS manually to check and fix all errors."
exit 1
fi
echo "[PRE-COMMIT] All errors are fixed automatically."
git add $FILES
else
echo "[PRE-COMMIT] No errors found."
fi
# Mess Detector
echo "\n[PRE-COMMIT] Checking PHPMD...\n"
for FILE in $FILES
do
# You can change your PHPMD command here
$PHPMD_BIN $PROJECT/$FILE text phpmd.xml
if [ $? != 0 ]
then
echo "\n[PRE-COMMIT] Fix errors before commit."
exit 1
fi
done
fi
exit $?
@mariovalney
Copy link
Author

You can check explanation here.

This hook is like a default in my projects and when I published this I copied from a old project when all code were placeded in "core" directory. My mistake. I should copied from a version without this standard.

FILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep .php)

@MikecMarko
Copy link

Great! Thank you for explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment