Skip to content

Instantly share code, notes, and snippets.

@jnduli
Last active June 10, 2019 05:33
Show Gist options
  • Select an option

  • Save jnduli/9888c08083d82a5b371ef2b12cc5cc6f to your computer and use it in GitHub Desktop.

Select an option

Save jnduli/9888c08083d82a5b371ef2b12cc5cc6f to your computer and use it in GitHub Desktop.
php-cs-fixer pre-commit hook for git
#!/bin/sh
# php-cs-fixer should be installed in the folder ./vendor/bin/
# Get installation instructions here: https://github.com/FriendsOfPHP/PHP-CS-Fixer
STAGED_FILES=$(git diff --cached --name-only --diff-filter=d | grep ".*\.php$")
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
echo "\nValidating PHP:\n"
# Checking for php-cs-fixer
if [ ! -f ./vendor/bin/php-cs-fixer ]; then
echo "\t\033[41mphp-cs-fixer not found\033[0m"
exit 1
fi
for FILE in $STAGED_FILES
do
php vendor/bin/php-cs-fixer fix $FILE
CORRECTION_CODE=$?
if [[ ${CORRECTION_CODE} -eq 0 ]]; then
git add $FILE
echo "\t\033[32mLint Passed: $FILE\033[0m"
else
echo "\t\033[41mLint Failed: $FILE\033[0m"
PASS=false
fi
done
echo "\nPHP validation completed!\n"
if ! $PASS; then
echo "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that coult not be fixed with php-cs-fixer.\n"
exit 1
else
echo "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment