Using Git Hooks for code style fixes

  • Reading time: 1 min
  • Published 3 years ago

Pretty much every modern language has an established Coding Style Guide. For PHP, this is PSR-2. As with other style guides of this sort, there is a tool for automatically applying the appropriate fixes to one's code

I only recently finally switched to using PSR-2 and to make sure I wouldn't accidentally miss running the style fixer, I added this little snipped to my .git/hooks/pre-commit-Hook:

modified_php=$(git ls-files --modified | grep "\.php$")

for f in $modified
do
    php-cs-fixer fix --level psr2 $f
done