Skip to content

Instantly share code, notes, and snippets.

@popovserhii
Created September 30, 2021 11:13
Show Gist options
  • Save popovserhii/80aa7844d4b714ef3c6ab611ac89b01c to your computer and use it in GitHub Desktop.
Save popovserhii/80aa7844d4b714ef3c6ab611ac89b01c to your computer and use it in GitHub Desktop.
PHP ECS (Easy Coding standard) git pre commit hook
#!/usr/bin/env bash
echo "PHP-ECS pre commit hook start"
PASS=true
PHP_ECS="bin/ecs"
HAS_PHP_ECS=false
if [ -x $PHP_ECS ]; then
HAS_PHP_ECS=true
fi
if $HAS_PHP_ECS; then
# Get a list of files in the staging area
FILES=` git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | tr '\n' ' '`
if [ -z "$FILES" ]; then
echo "No PHP file exists in commit"
else
$PHP_ECS check ${FILES} --fix
if [[ "$?" == 0 ]]; then
# Add the fixed files back to the staging area
git add ${FILES}
else
# Different code than 0 means that there were unresolved fixes
PASS=false
fi
fi
else
echo ""
echo "Please install easy-coding-standard, e.g.:"
echo ""
echo " composer require --dev symplify/easy-coding-standard"
echo ""
fi
echo -e "\nPHP-ECS validation completed!\n"
if ! $PASS; then
echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass PHP-ECS but do not. Please fix the PHP-ECS errors and try again.\n"
exit 1
else
echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment