Skip to content

Instantly share code, notes, and snippets.

@ben182
Created January 17, 2019 23:12
Show Gist options
  • Select an option

  • Save ben182/14d7393224781764dc5e3315896d6161 to your computer and use it in GitHub Desktop.

Select an option

Save ben182/14d7393224781764dc5e3315896d6161 to your computer and use it in GitHub Desktop.
PHP-CS-Fixer pre-commit
#!/usr/bin/env bash
EXECUTABLE_NAME=php-cs-fixer
EXECUTABLE_COMMAND=fix
CONFIG_FILE=.php_cs
CONFIG_FILE_PARAMETER='--config'
ROOT=`pwd`
# possible locations
locations=(
$ROOT/bin/$EXECUTABLE_NAME
$ROOT/vendor/bin/$EXECUTABLE_NAME
`which $EXECUTABLE_NAME`
)
for location in ${locations[*]}
do
if [[ -x $location ]]; then
EXECUTABLE=$location
break
fi
done
if [[ ! -x $EXECUTABLE ]]; then
echo "executable $EXECUTABLE_NAME not found, exiting..."
echo "if you're sure this is incorrect, make sure they're executable (chmod +x)"
exit
fi
echo "using \"$EXECUTABLE_NAME\" located at $EXECUTABLE"
$EXECUTABLE --version
if [[ -f $ROOT/$CONFIG_FILE ]]; then
CONFIG=$ROOT/$CONFIG_FILE
echo "config file located at $CONFIG loaded"
fi
git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | while read line; do
if [[ -f $CONFIG ]]; then
$EXECUTABLE $EXECUTABLE_COMMAND $CONFIG_FILE_PARAMETER=$CONFIG $line;
else
$EXECUTABLE $EXECUTABLE_COMMAND $line;
fi
git add $line;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment