Skip to content

Instantly share code, notes, and snippets.

@hujuice
Last active March 9, 2021 00:57
Show Gist options
  • Save hujuice/9ff2fd4221e2135f946931dbb512bfc6 to your computer and use it in GitHub Desktop.
Save hujuice/9ff2fd4221e2135f946931dbb512bfc6 to your computer and use it in GitHub Desktop.
Devs Git hooks for a PHP project

Here are suggested hooks for a developer repository, in a composer based PHP project.

The pre-commit avoids direct commits in masterbranch and performs quality checks (qa script) on changed files, blocking the poor quality code.

The post-merge install new versions of packages on composer.lock changes and perform a new QA check on incoming code.

The quality checks are based on PHP Mess Detector and PHP CodeSniffer

#!/usr/bin/env bash
# -------------------------------------------------------------#
# Install new packages on composer.lock updates #
# -------------------------------------------------------------#
# Sergio Vaccaro <sergio.vaccaro@istat.it>
#
# Preferences
# --------------------------------------------------------------
COMPOSER_INSTALL_ON_LOCK_CHANGES=1
QA=1
# --------------------------------------------------------------
# Install the updated version of composer packages
# ================================================
if [[ 0 -ne "$COMPOSER_INSTALL_ON_LOCK_CHANGES" ]]; then
# Inspired by https://gist.github.com/sindresorhus/7996717
COMPOSER_FILE="composer.lock"
if git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD | grep --quiet "$COMPOSER_FILE"; then
echo "${COMPOSER_FILE} changes detected. Running updates."
composer install
fi
fi
# Run QA checks
# =============
if [[ 0 -ne "$QA" ]]; then
source .git/hooks/qa
#run checks
echo "Running QA checks"
if ! qa $(git diff HEAD --name-only --diff-filter=dr | grep \.php$); then
echo "Quality errors detected. Please fix."
fi
fi
#!/usr/bin/env bash
# -------------------------------------------------------------#
# Run many checks before commit #
# -------------------------------------------------------------#
# Sergio Vaccaro <sergio.vaccaro@istat.it>
#
# Preferences
# --------------------------------------------------------------
PREVENT_COMMIT_IN_MASTER_BRANCH=1
QA=1
# --------------------------------------------------------------
# Redirect output to stderr.
exec 1>&2
# Prevent commits in master branch
# ================================
if [[ 0 -ne "$PREVENT_COMMIT_IN_MASTER_BRANCH" ]]; then
# See https://stackoverflow.com/questions/40462111/git-prevent-commits-in-master-branch
branch="$(git rev-parse --abbrev-ref HEAD)"
if [ "$branch" = "master" ]; then
echo
echo "[BEST PRATICE] You can't commit directly to master branch."
echo
echo " You can quickly change to a new branch with the command"
echo " git stash && git stash branch newBranch"
echo " or to an existing branch with command"
echo " git stash && git checkout existingBranch && git stash pop."
echo
exit 1
fi
fi
# Quality assurance
# =================
if [[ 0 -ne "$QA" ]]; then
source .git/hooks/qa
#run checks
echo "Running QA checks"
qa $(git diff HEAD --name-only --diff-filter=dr | grep \.php$)
EXIT=$?
if [ "$EXIT" -ne 0 ]; then
echo 'Commit unsuccessful because quality errors. Please fix them.'
exit $EXIT
fi
fi
exit 0
#!/usr/bin/env bash
# -------------------------------------------------------------#
# Quality assurance checks #
# #
# for a composer based PHP project #
# -------------------------------------------------------------#
# Sergio Vaccaro <sergio.vaccaro@istat.it>
# Note: add the following to your project
# composer require --dev "phpmd/phpmd": "2.6.0"
# composer require --dev "squizlabs/php_codesniffer": "2.9.1"
# Create your own phpmd ruleset: https://phpmd.org/documentation/creating-a-ruleset.html
# PHPMD rules file
PHPMDRULES="phpmd.xml"
function qa {
# Set the exit status
local EXIT=0
# PHPMD rules file
PHPMDRULES="phpmd.xml"
# Failures
local NOPHPMD=""
local NOPHPCS=""
for i in $@; do
echo "File ${i}"
# phpmd, https://phpmd.org/
# -------------------------
# To run phpmd manually, type:
# vendor/bin/phpmd . text phpmd.xml --suffixes php --exclude vendor,local
echo -n 'PHP Mess Detector... '
if vendor/bin/phpmd $i text "$PHPMDRULES"; then
echo 'ok'
else
echo 'failed'
if [ -z "$NOPHPMD" ]; then
NOPHPMD="$i"
else
NOPHPMD="${NOPHPMD},${i}"
fi
fi
# phpcs https://github.com/squizlabs/PHP_CodeSniffer/wiki
# -------------------------------------------------------
# To run phpcs manually, type:
# vendor/bin/phpcs -s --ignore=vendor --ignore=web --ignore=local --report=summary .
echo -n 'PHP CodeSniffer... '
if vendor/bin/phpcs -s "${i}"; then
echo 'ok'
else
echo 'failed'
if [ -z "$NOPHPCS" ]; then
NOPHPCS="$i"
else
NOPHPCS="${NOPHPCS},${i}"
fi
fi
done
if [ -n "$NOPHPMD" ]; then
echo
echo '[QA] Error(s) found in code quality'
echo ' Check the quality rules at https://phpmd.org/rules/index.html'
echo " The following file(s) have error(s): ${NOPHPMD}"
echo " Please run 'vendor/bin/phpmd _filename_ text ${PHPMDRULES}' for each file"
echo
EXIT=1
fi
if [ -n "$NOPHPCS" ]; then
echo
echo '[QA] Error(s) found in conding style'
echo " The following file(s) have error(s): ${NOPHPCS}"
echo " Please run 'vendor/bin/phpcs -s --report=diff _filename_' for each file"
echo
if [ $EXIT -eq 0 ]; then
EXIT=2
fi
fi
if [ "$EXIT" -ne 0 ]; then
return "$EXIT"
fi
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment