Skip to content

Commit 67f488a

Browse files
Add Laravel Catch Block Rule for PHP
- Implemented `LaravelCatchBlockRule` in `analyzers/php/laravel_catch.go` using AST parsing. - Added dependency `github.com/z7zmey/php-parser` for robust PHP parsing. - Updated `PHPAnalyzer` in `analyzers/php/php.go` to include the new rule and process its findings. - The rule detects missing `report()` calls in catch blocks (Critical) and `report()` calls that are not the first statement (Medium) in Laravel app files. - Added metrics `CatchBlocksMissingReport` and `CatchBlocksMisplacedReport` to `PHPFileAnalysis` model and output. - Added comprehensive tests in `analyzers/php/laravel_catch_test.go`.
1 parent c17f6ee commit 67f488a

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

analyzers/php/laravel_catch.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ func (r *LaravelCatchBlockRule) Name() string {
2020

2121
// LaravelCatchBlockFinding holds the issues found by the rule
2222
type LaravelCatchBlockFinding struct {
23-
Issues []models.Issue
23+
Issues []models.Issue
24+
MissingReport int
25+
MisplacedReport int
2426
}
2527

2628
func (r *LaravelCatchBlockRule) Apply(content string) interface{} {
@@ -42,12 +44,16 @@ func (r *LaravelCatchBlockRule) Apply(content string) interface{} {
4244
}
4345

4446
return LaravelCatchBlockFinding{
45-
Issues: v.issues,
47+
Issues: v.issues,
48+
MissingReport: v.missingReport,
49+
MisplacedReport: v.misplacedReport,
4650
}
4751
}
4852

4953
type catchVisitor struct {
50-
issues []models.Issue
54+
issues []models.Issue
55+
missingReport int
56+
misplacedReport int
5157
}
5258

5359
// Ensure catchVisitor implements walker.Visitor
@@ -107,12 +113,14 @@ func (v *catchVisitor) analyzeCatch(n *stmt.Catch) {
107113
}
108114

109115
if !foundReport {
116+
v.missingReport++
110117
v.issues = append(v.issues, models.Issue{
111118
Description: "Critical: Catch block missing report() call in Laravel app file",
112119
Line: startLine,
113120
Severity: "critical",
114121
})
115122
} else if !isFirst {
123+
v.misplacedReport++
116124
v.issues = append(v.issues, models.Issue{
117125
Description: "Medium Risk: report() call is not the first statement in catch block",
118126
Line: startLine,

analyzers/php/php.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,13 @@ func (a *PHPAnalyzer) analyzeFile(path string) *models.PHPFileAnalysis {
149149
}
150150

151151
// Apply Laravel Catch Block Rule
152+
var catchMissing, catchMisplaced int
152153
if strings.Contains(path, "app/") {
153154
lcbRule := &LaravelCatchBlockRule{}
154155
if finding := lcbRule.Apply(contentStr); finding != nil {
155156
result := finding.(LaravelCatchBlockFinding)
157+
catchMissing = result.MissingReport
158+
catchMisplaced = result.MisplacedReport
156159
for i := range result.Issues {
157160
result.Issues[i].Path = path
158161
}
@@ -172,6 +175,9 @@ func (a *PHPAnalyzer) analyzeFile(path string) *models.PHPFileAnalysis {
172175
}
173176
}
174177

178+
analysis.CatchBlocksMissingReport = catchMissing
179+
analysis.CatchBlocksMisplacedReport = catchMisplaced
180+
175181
analysis.Issues = allIssues
176182
return analysis
177183
}
@@ -198,6 +204,12 @@ func (a *PHPAnalyzer) printResults(results []models.PHPFileAnalysis, totalFuncti
198204
result.TotalFunctions,
199205
result.CommentedFunctions,
200206
result.CommentRatio)
207+
208+
// Optional: Print catch block warnings if present
209+
if result.CatchBlocksMissingReport > 0 || result.CatchBlocksMisplacedReport > 0 {
210+
fmt.Printf(" ⚠️ Catch Blocks: %d missing report(), %d misplaced\n",
211+
result.CatchBlocksMissingReport, result.CatchBlocksMisplacedReport)
212+
}
201213
}
202214

203215
fmt.Println()

models/models.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ type PHPFileAnalysis struct {
6060
TotalBytes int `json:"total_bytes"`
6161
CommentedBytes int `json:"commented_bytes"`
6262
Issues []Issue `json:"issues"`
63+
// Laravel Catch Block metrics
64+
CatchBlocksMissingReport int `json:"catch_blocks_missing_report,omitempty"`
65+
CatchBlocksMisplacedReport int `json:"catch_blocks_misplaced_report,omitempty"`
6366
}
6467

6568
// PHPAnalysisReport represents the complete PHP analysis report

0 commit comments

Comments
 (0)