Skip to content

Commit c370b92

Browse files
committed
fix: move PHPStan to pre-commit to catch errors immediately
PHPStan should run at commit time, not push time, to ensure every commit in git history is type-safe and prevent committing broken code. Changes: - Move PHPStan from pre-push to pre-commit (lint-staged) - Update pre-push hook to only check branch protection - Update DEVELOPER.md and CLAUDE.md documentation Why this is better: - Type errors caught immediately when committing - No broken commits in git history - No need to fix and re-commit after failed push - Clearer workflow: fix → stage → commit → push
1 parent db39b8a commit c370b92

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

.husky/pre-push

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,4 @@ if [ "$current_branch" = "master" ] || [ "$current_branch" = "main" ]; then
1111
exit 1
1212
fi
1313

14-
# Run static analysis before push
15-
echo "Running PHPStan static analysis..."
16-
yarn lint:php:phpstan
14+
echo "✅ Branch check passed"

.lintstagedrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"*.php": [
3-
"composer phpcbf --"
3+
"composer phpcbf --",
4+
"composer phpstan --"
45
],
56
"*.{js,jsx}": [
67
"wp-scripts lint-js --fix"

CLAUDE.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -314,22 +314,21 @@ The project uses automated linting and static analysis tools with git hooks for
314314

315315
### Automated Workflow (Git Hooks)
316316

317-
**Pre-commit Hook** (~0.9s - fast!):
318-
- Runs `lint-staged` to auto-fix only staged files
319-
- PHP: `phpcbf` (auto-fixes WordPress coding standards)
317+
**Pre-commit Hook**:
318+
- Runs `lint-staged` to auto-fix and validate staged files
319+
- PHP: `phpcbf` (auto-fixes code style) + `phpstan` (static analysis)
320320
- JavaScript: `eslint --fix` (auto-fixes linting errors)
321321
- CSS: `stylelint --fix` (auto-fixes style errors)
322-
- **Result**: All fixable issues are automatically corrected before commit
322+
- **Result**: All fixable issues corrected, type errors caught immediately
323323

324324
**Commit-msg Hook** (instant):
325325
- Validates commit message follows conventional commits format
326326
- Types: `feat:`, `fix:`, `docs:`, `refactor:`, `chore:`, etc.
327327
- **Result**: Invalid commit messages are rejected
328328

329-
**Pre-push Hook** (~1s):
329+
**Pre-push Hook** (instant):
330330
- **Branch Protection**: Blocks direct pushes to `master`/`main`
331-
- **PHPStan**: Runs static analysis (moved from pre-commit for speed)
332-
- **Result**: Type errors are caught before pushing to remote
331+
- **Result**: Enforces feature branch workflow
333332

334333
### Running Linters Manually
335334

@@ -359,16 +358,21 @@ yarn lint:php:phpstan # Check PHP static analysis only
359358
2. **Before staging**: Run `yarn lint:fix` to auto-fix all issues
360359
3. **Stage your files**: `git add <files>`
361360
4. **Commit**: `git commit -m "feat: your message"`
362-
- Pre-commit hook auto-fixes staged files (~0.9s)
361+
- Pre-commit hook auto-fixes staged files
362+
- Pre-commit hook runs PHPStan to catch type errors
363363
- Commit-msg hook validates message format
364364
5. **Push**: `git push`
365-
- Pre-push hook runs PHPStan (~1s)
366-
- Branch protection prevents pushing to master
365+
- Pre-push hook checks branch protection
367366

368-
**If pre-push fails (PHPStan errors)**:
367+
**If commit fails (PHPStan errors)**:
369368
- Fix the type errors reported by PHPStan
370-
- Commit the fixes
371-
- Push again
369+
- Stage the fixes: `git add <files>`
370+
- Commit again
371+
372+
**Why this is better**:
373+
- Type errors are caught at commit time, not push time
374+
- Every commit in git history is guaranteed to be type-safe
375+
- No need to fix and re-commit after a failed push
372376

373377
### Configuration Files
374378

@@ -434,9 +438,9 @@ composer phpcbf -- src/Gateways/PaymentMethods/WCGatewayMoneiCC.php
434438
- Create pull requests for code review before merging to master
435439

436440
**Performance**:
437-
- Pre-commit hook is optimized for speed (~0.9s)
438-
- PHPStan runs only on pre-push (~1s) to keep commits fast
439441
- lint-staged only processes staged files, not the entire codebase
442+
- PHPStan analyzes only staged PHP files and their dependencies
443+
- Pre-push hook is instant (only branch check)
440444

441445
### Common PHPStan Errors & Fixes
442446

DEVELOPER.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,18 @@ The project uses automated linting and code quality tools to maintain consistent
171171

172172
### Git Hooks
173173

174-
**Pre-commit Hook** (fast ~0.9s):
174+
**Pre-commit Hook**:
175175
- Auto-fixes staged files with `lint-staged`
176-
- PHP: `phpcbf` (WordPress coding standards auto-fix)
176+
- PHP: `phpcbf` (auto-fix code style) + `phpstan` (static analysis)
177177
- JavaScript: `eslint --fix`
178178
- CSS: `stylelint --fix`
179+
- **Prevents committing broken code** by running PHPStan
179180

180181
**Commit-msg Hook**:
181182
- Validates commit message format (conventional commits)
182183

183-
**Pre-push Hook** (~1s):
184+
**Pre-push Hook**:
184185
- **Branch Protection**: Blocks direct pushes to `master`/`main` branches
185-
- **PHPStan**: Static analysis (type checking, bug detection)
186186

187187
### Linting Commands
188188

@@ -207,9 +207,9 @@ yarn lint:php:phpstan # Check PHP static analysis only
207207
### Workflow Best Practices
208208

209209
1. **Before committing**: Run `yarn lint:fix` to auto-fix all issues
210-
2. **During commit**: Hooks auto-fix staged files and validate commit message
211-
3. **Before push**: PHPStan runs automatically (takes ~1s)
212-
4. **If push fails**: Fix PHPStan errors and push again
210+
2. **During commit**: Hooks auto-fix staged files, run PHPStan, and validate commit message
211+
3. **If commit fails**: Fix PHPStan errors and commit again
212+
4. **Before push**: Branch protection check ensures you're not pushing to master
213213

214214
### Configuration Files
215215

0 commit comments

Comments
 (0)