Skip to content

Instantly share code, notes, and snippets.

@mariovalney
Last active May 1, 2024 16:12
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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

Download and put into .git/hooks directory.

You can do: cp pre-commit .git/hooks/pre-commit

Then, make it executable:
chmod +x .git/hooks/pre-commit

@OlegApanovich
Copy link

Great hook but I have a difficulties trying to detecting changed files in line
FILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep core/ | grep .php)
I don't know why maybe it's a operation system issue I tryin launch it on CLI interface in ubuntu 21.04
For me it's work fine when I changed line to
FILES=$(git diff --name-only --diff-filter=ACMR HEAD | grep \\.php )

@MikecMarko
Copy link

Like already mentioned it's a great hook. But like Oleg mentioned I also have issues with grep core/ because then it fetches no files.
Removing that works for me as well then.

Why do you @mariovalney use that part in the first place, just curious?

@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