Skip to content

Instantly share code, notes, and snippets.

@timoschinkel
Last active May 13, 2019 08:25
Show Gist options
  • Save timoschinkel/2a0b3e956c34d6ea891ea83d46624d5b to your computer and use it in GitHub Desktop.
Save timoschinkel/2a0b3e956c34d6ea891ea83d46624d5b to your computer and use it in GitHub Desktop.
Git pre-push hook for php inspections
#!/bin/bash
UPSTREAM="upstream" # Could come from env
CHANGED_FILES=`git diff --name-only --diff-filter=ACM HEAD $UPSTREAM/master --raw`
PHP_SOURCE_FILES=""
PHP_TEST_FILES=""
TWIG_FILES=""
for file in $CHANGED_FILES
do
if [[ -f $file ]]
then
if [[ ${file: -4} == ".php" ]] || [[ ${file: -4} == ".tpl" ]]
then
if [[ $file == *"tests"* ]]
then
PHP_TEST_FILES="$PHP_TEST_FILES $file"
else
PHP_SOURCE_FILES="$PHP_SOURCE_FILES $file"
fi
elif [[ ${file: -5} == ".twig" ]]
then
TWIG_FILES="$TWIG_FILES $file"
fi
fi
done
echo "⏳ Running inspections on changed files"
#
# Any additional inspections can add exit codes to CUMULATIVE_EXIT_CODE. If this is > 0 at the end of the inspection
# the push is prevented.
#
CUMULATIVE_EXIT_CODE=0
#
# PHP inspections
#
if [[ -z "$PHP_SOURCE_FILES" ]] && [[ -z "$PHP_TEST_FILES" ]]
then
echo "ℹ️ No php files in change set"
else
if [[ ! -z "$PHP_SOURCE_FILES" ]]
then
./vendor/bin/psalm $PHP_SOURCE_FILES
PSALM_RESULT=$?
CUMULATIVE_EXIT_CODE=$((CUMULATIVE_EXIT_CODE + PSALM_RESULT))
fi
./vendor/bin/phpcs $PHP_SOURCE_FILES $PHP_TEST_FILES
PHPCS_RESULT=$?
CUMULATIVE_EXIT_CODE=$((CUMULATIVE_EXIT_CODE + PHPCS_RESULT))
fi
if [[ ${CUMULATIVE_EXIT_CODE} -ne 0 ]]; then
echo "⚠️ Some inspections failed"
exit 1
else
echo "✅ Inspections passed, pushing ..."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment