Skip to content

Instantly share code, notes, and snippets.

@johndodev
Last active December 21, 2023 09:45
Show Gist options
  • Save johndodev/cc54a6fc85c995e26d8dc722468b9151 to your computer and use it in GitHub Desktop.
Save johndodev/cc54a6fc85c995e26d8dc722468b9151 to your computer and use it in GitHub Desktop.
Exemple de pre-commit qui lance un lint, valide le mapping des entities, phpcs et phpstan
#!/bin/sh
# No PHP ? Exit
if [[ -z $(which php) ]]; then
exit 0
fi
PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
SFILES=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`
printf "Running PHP Lint... "
for FILE in $SFILES
do
php -l -d display_errors=0 $PROJECT/$FILE 1 > /dev/null
if [ $? != 0 ]
then
echo "Fix the error before commit."
exit 1
fi
FILES="$FILES $PROJECT/$FILE"
done
echo "passed"
if [ "$FILES" != "" ]
then
# Remove that test if no Symfony !
printf "Validate entities mapping "
php bin/console doctrine:schema:validate -q
if [ $? != 0 ]
then
php bin/console doctrine:schema:validate
echo "Fix the error before commit."
exit 1
fi
echo "passed"
# End of Symfony test
printf "Running Code Sniffer... "
./vendor/bin/phpcs -n $FILES > /dev/null
if [ $? = 0 ]
then
echo "passed"
else
echo "Errors detected. Running phpcbf..."
./vendor/bin/phpcbf -n -q $FILES > /dev/null
git add $FILES
printf "Running Code Sniffer again... "
output=$(./vendor/bin/phpcs -n -p $FILES)
if [ $? = 0 ]
then
echo "passed"
else
echo "Errors found not fixable automatically:"
echo "$output"
exit 1
fi
fi
printf "Running PhpStan... "
output=$(./vendor/bin/phpstan analyze --no-progress $SFILES)
if [ $? != 0 ]
then
echo ""
echo "$output"
exit 1;
else
echo "passed"
fi
fi
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment