Skip to content

Instantly share code, notes, and snippets.

@kburton-dev
Last active April 2, 2024 12:47
Show Gist options
  • Save kburton-dev/2bd27fee60e22124f0038247bba62b7f to your computer and use it in GitHub Desktop.
Save kburton-dev/2bd27fee60e22124f0038247bba62b7f to your computer and use it in GitHub Desktop.
pre-commit: run pint, and phpstan on diffed files
#!/bin/bash
# Get the root directory of the Git repository
root_directory=$(git rev-parse --show-toplevel)
cd "$root_directory"
modified_files=()
while IFS= read -r line; do
if [[ "$line" =~ \.php$ && "$line" =~ ^(app|bootstrap|config|database|routes|tests) ]]; then
modified_files+=("$line")
fi
done < <(git diff --diff-filter=d --name-only --cached)
echo "Running Pint on files: ${modified_files[@]}"
if [ "${#modified_files[@]}" -gt 0 ]; then
vendor/bin/pint --test "${modified_files[@]}"
exit_code=$?
if [[ $exit_code -ne 0 ]]; then
echo "Pint dry-run failed. Please fix the issues before committing."
exit 1
fi
fi
modified_files=()
while IFS= read -r line; do
if [[ "$line" =~ \.php$ && "$line" =~ ^(app|bootstrap|database/migrations) ]]; then
modified_files+=("$line")
fi
done < <(git diff --diff-filter=d --name-only --cached)
echo "Type checking files: ${modified_files[@]}"
if [ "${#modified_files[@]}" -gt 0 ]; then
vendor/bin/phpstan analyse "${modified_files[@]}"
exit_code=$?
if [[ $exit_code -ne 0 ]]; then
echo "PHPStan analysis failed. Please fix the issues before committing."
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment