Skip to content

Instantly share code, notes, and snippets.

@iredmedia
Created March 31, 2020 23:10
Show Gist options
  • Save iredmedia/f452e5462008700afe274ff032393a3f to your computer and use it in GitHub Desktop.
Save iredmedia/f452e5462008700afe274ff032393a3f to your computer and use it in GitHub Desktop.
Git - pre-commit - php-cs-fixer
#!/usr/bin/env bash
PHP_CS_FIXER="vendor/bin/php-cs-fixer"
PHP_CS_CONFIG=".php_cs"
HAS_PHP_CS_FIXER=false
checkDefaultFixer() {
CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.php')
if [[ -n "$CHANGED_FILES" ]]; then
${PHP_CS_FIXER} fix --verbose --config ${PHP_CS_CONFIG} ${CHANGED_FILES};
git add ${CHANGED_FILES};
fi
#
# git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | while read line; do
# $PHP_CS_FIXER fix --verbose "$line";
# git add "$line";
# done
}
checkPSR4() {
CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.php')
if [[ -n "$CHANGED_FILES" ]]; then
${PHP_CS_FIXER} fix --verbose --dry-run --rules=psr4 --allow-risky=yes ${CHANGED_FILES};
if [[ $? -eq 0 ]]
then
echo ""
echo -e "\033[1;32mSuccess!\033[1;39"
echo ""
echo " Commit completed!"
else
echo ""
echo -e "\033[1;31mFailure \033[1;39mCould not commit!"
echo ""
echo " You likely have PSR4 issues!"
echo ""
exit 1
fi
git add ${CHANGED_FILES};
fi
}
echo "php-cs-fixer pre commit hook start"
# Check for php-cs-fixer
if [[ -x vendor/bin/php-cs-fixer ]]; then
HAS_PHP_CS_FIXER=true
fi
# Run the checks
if ${HAS_PHP_CS_FIXER}; then
checkDefaultFixer
else
echo ""
echo "Please install php-cs-fixer, e.g.:"
echo ""
echo " composer require --dev fabpot/php-cs-fixer:dev-master"
echo ""
fi
echo "php-cs-fixer pre commit hook finish"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment