Skip to content

Instantly share code, notes, and snippets.

@davidromani
Created January 23, 2017 11:00
Show Gist options
  • Save davidromani/9fa1c32fcd805a098403a60995aec595 to your computer and use it in GitHub Desktop.
Save davidromani/9fa1c32fcd805a098403a60995aec595 to your computer and use it in GitHub Desktop.
Apply PHP CS fixer Symfony rules in Githook pre-commit
#!/bin/bash
if [ -x "$(command -v php-cs-fixer)" ]; then
FILES=`git diff --name-only`
printf '%s\n' "$FILES" | while IFS= read -r FILE
do
if [[ $FILE == "src/"* ]]; then
php-cs-fixer fix "$FILE" --rules=@Symfony
# git add "$FILE"
fi
done
FILES=`git diff --name-only --staged`
printf '%s\n' "$FILES" | while IFS= read -r FILE
do
if [[ $FILE == "src/"* ]]; then
php-cs-fixer fix "$FILE" --rules=@Symfony
git add "$FILE"
fi
done
else
echo "Unable to find php-cs-fixer command. Please, check https://github.com/FriendsOfPHP/PHP-CS-Fixer to install it globally (manual) in your system."
fi
@davidromani
Copy link
Author

davidromani commented Jan 23, 2017

This Git hook applies a PHP CS Fixer with Symfony rules only in PHP files under "src" directory. Finally, you must execute* this symlink command under project root dir.

ln -s ../../app/Resources/githooks/pre-commit.sh .git/hooks/pre-commit

*In this case we placed this script under app/Resources/githooks directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment