Skip to content

Instantly share code, notes, and snippets.

@MPJHorner
Last active August 14, 2025 08:01
Show Gist options
  • Select an option

  • Save MPJHorner/d0e064d0949df5f7273381c2168921ba to your computer and use it in GitHub Desktop.

Select an option

Save MPJHorner/d0e064d0949df5f7273381c2168921ba to your computer and use it in GitHub Desktop.
Claude Code PHP Hooks

Claude Code Laravel PHP Hooks

Add these to the repo, update the claude.md to make them aware they are thier. No longer wastes tokens on running these since they will autorun in code and provide feedback when file changes are made. They only run on the edited file, so a githook precommmit is still required since editing/moving/deleting a file could mean another file no longer lints correctly.

File Locations

  • settings.json --> .claude/settings.json
  • hook-pint.sh --> scripts/hook-pint.sh
  • hook-phpstan.sh --> scripts/hook-phpstan.sh

CLAUDE.md

Add the following to claude.md

Instructions

This project includes Claude Code hooks that automatically run PHP code quality checks when files are modified:

Automatic Quality Checks:

  • PHP Pint formatting validation runs on only the edited PHP file
  • PHPStan static analysis runs on only the edited PHP file
  • Non-PHP files are automatically skipped

The hooks will show:

  • ✅ PASSED when code meets quality standards
  • ❌ FAILED when issues are found, with guidance on how to fix them
#!/bin/bash
# PHPStan Hook Script - Check only edited files
# Read JSON input from stdin
if [ -t 0 ]; then
echo "⚠️ No input provided - run from hooks only"
exit 0
fi
FILE_PATH=$(cat | jq -r '.tool_input.file_path // empty' 2>/dev/null)
if [[ ! "$FILE_PATH" == *.php ]]; then
exit 0
fi
if [ ! -d "./vendor" ] || [ ! -f "./vendor/bin/phpstan" ]; then
echo "⚠️ PHPStan not available - run composer install"
exit 0
fi
if [ -n "$FILE_PATH" ] && [ -f "$FILE_PATH" ]; then
if ./vendor/bin/phpstan analyse "$FILE_PATH" --no-progress --quiet >/dev/null 2>&1; then
echo "✅ PHPStan analysis: PASSED ($(basename "$FILE_PATH"))"
else
echo "❌ PHPStan analysis: FAILED ($(basename "$FILE_PATH")) - run ./vendor/bin/phpstan analyse \"$FILE_PATH\""
fi
else
echo "⚠️ No file path found in hook input"
fi
#!/bin/bash
# PHP Pint Hook Script - Check only edited files
# Read JSON input from stdin
if [ -t 0 ]; then
echo "⚠️ No input provided - run from hooks only"
exit 0
fi
FILE_PATH=$(cat | jq -r '.tool_input.file_path // empty' 2>/dev/null)
if [[ ! "$FILE_PATH" == *.php ]]; then
exit 0
fi
if [ ! -d "./vendor" ] || [ ! -f "./vendor/bin/pint" ]; then
echo "⚠️ Pint not available - run composer install"
exit 0
fi
if [ -n "$FILE_PATH" ] && [ -f "$FILE_PATH" ]; then
if ./vendor/bin/pint --test "$FILE_PATH" --quiet >/dev/null 2>&1; then
echo "✅ Pint formatting: PASSED ($(basename "$FILE_PATH"))"
else
echo "❌ Pint formatting: FAILED ($(basename "$FILE_PATH")) - run ./vendor/bin/pint \"$FILE_PATH\""
fi
else
echo "⚠️ No file path found in hook input"
fi
{
"hooks": {
"UserPromptSubmit": [
{
"hooks": [
{
"type": "command",
"command": "echo 'PHP development session active - code quality checks enabled'"
}
]
}
],
"PostToolUse": [
{
"matcher": "Edit|MultiEdit|Write",
"hooks": [
{
"type": "command",
"command": "./scripts/hook-pint.sh"
},
{
"type": "command",
"command": "./scripts/hook-phpstan.sh"
}
]
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment