Skip to content

Commit 4cbac50

Browse files
author
razvan
committed
fix: address PR #46 review comments (6 fixes)
- Fix misleading 'Enrich DONE' log emitted before Blade analysis (enricher.go) - Fix wc_hook signature to use real WP functions (add_action/add_filter/etc.) - Populate WooCommerce APICalls via woocommerceAnalyzer.Analyze() in AST walk - Add missing AST cases to extractExprValue (ScalarDnumber, ExprConstFetch, ExprClassConstFetch) - Fix Blade reSection regex to capture inline sections like @section('title', 'Dashboard') - Set EndLine on blade_template chunks using TotalLines from BladeTemplate struct Signed-off-by: doITmagic <razvan@doitmagic.com>
1 parent 059ffee commit 4cbac50

6 files changed

Lines changed: 68 additions & 12 deletions

File tree

pkg/parser/php/laravel/adapter.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,18 @@ func (a *Adapter) convertBladeToChunks(templates []BladeTemplate) []php.CodeChun
297297
})
298298
}
299299

300+
endLine := tpl.TotalLines
301+
if endLine < 1 {
302+
endLine = 1
303+
}
304+
300305
chunk := php.CodeChunk{
301306
Name: tpl.Name,
302307
Type: "blade_template",
303308
Language: "php",
304309
FilePath: tpl.FilePath,
305310
StartLine: 1,
311+
EndLine: endLine,
306312
Signature: sig,
307313
Docstring: docstring,
308314
Metadata: map[string]any{

pkg/parser/php/laravel/blade.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// Compiled regex patterns for Blade directives
1414
var (
1515
reExtends = regexp.MustCompile(`@extends\(\s*['"](.+?)['"]\s*\)`)
16-
reSection = regexp.MustCompile(`@section\(\s*['"](.+?)['"]\s*\)`)
16+
reSection = regexp.MustCompile(`@section\(\s*['"](.+?)['"]\s*(?:,.*?)?\)`)
1717
reYield = regexp.MustCompile(`@yield\(\s*['"](.+?)['"]\s*\)`)
1818
reInclude = regexp.MustCompile(`@include\(\s*['"](.+?)['"]\s*\)`)
1919
reComponent = regexp.MustCompile(`@component\(\s*['"](.+?)['"]\s*\)`)
@@ -128,6 +128,8 @@ func (ba *BladeAnalyzer) analyzeFile(filePath string) (BladeTemplate, error) {
128128
}
129129
}
130130

131+
tpl.TotalLines = lineNum
132+
131133
return tpl, scanner.Err()
132134
}
133135

pkg/parser/php/laravel/enricher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (e *Enricher) Enrich(ca *php.CodeAnalyzer, packages []*php.PackageInfo, pat
123123
}
124124
}
125125

126-
logger.Instance.Debug("[LARAVEL] Enrich DONE: returning %d total chunks (before blade)", len(chunks))
126+
logger.Instance.Debug("[LARAVEL] Enrich: %d chunks after routes, before blade analysis", len(chunks))
127127

128128
// Analyze Blade Templates
129129
bladeFiles := e.adapter.findBladeFiles(paths)

pkg/parser/php/laravel/types.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,14 @@ type Middleware struct {
132132

133133
// BladeTemplate represents a parsed Blade template with its directives
134134
type BladeTemplate struct {
135-
Name string `json:"name"` // dot notation: layouts.app
136-
FilePath string `json:"file_path"`
137-
Extends string `json:"extends,omitempty"` // @extends('...')
138-
Sections []BladeSection `json:"sections,omitempty"`
139-
Includes []BladeInclude `json:"includes,omitempty"`
140-
Stacks []string `json:"stacks,omitempty"` // @push/@stack names
141-
Props []string `json:"props,omitempty"` // @props([...])
135+
Name string `json:"name"` // dot notation: layouts.app
136+
FilePath string `json:"file_path"`
137+
TotalLines int `json:"total_lines"` // total line count of the file
138+
Extends string `json:"extends,omitempty"` // @extends('...')
139+
Sections []BladeSection `json:"sections,omitempty"`
140+
Includes []BladeInclude `json:"includes,omitempty"`
141+
Stacks []string `json:"stacks,omitempty"` // @push/@stack names
142+
Props []string `json:"props,omitempty"` // @props([...])
142143
}
143144

144145
// BladeSection represents a @section or @yield directive

pkg/parser/php/wordpress/analyzer.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (a *Analyzer) AnalyzePaths(paths []string) ([]php.CodeChunk, error) {
8080
// analyzeWordPress performs complete WordPress analysis using both package info and AST
8181
func (a *Analyzer) analyzeWordPress(packages []*php.PackageInfo, paths []string) *WordPressInfo {
8282
info := &WordPressInfo{}
83+
var wcAPICalls []woocommerce.WCAPICall
8384

8485
// Analyze from parsed package info (method calls)
8586
info.Hooks = a.hookAnalyzer.AnalyzeHooks(packages)
@@ -149,6 +150,12 @@ func (a *Analyzer) analyzeWordPress(packages []*php.PackageInfo, paths []string)
149150
}
150151
}
151152

153+
// WooCommerce API calls from AST
154+
wcInfo := a.woocommerceAnalyzer.Analyze(rootNode, path)
155+
if wcInfo != nil && len(wcInfo.APICalls) > 0 {
156+
wcAPICalls = append(wcAPICalls, wcInfo.APICalls...)
157+
}
158+
152159
return nil
153160
})
154161
}
@@ -173,8 +180,11 @@ func (a *Analyzer) analyzeWordPress(packages []*php.PackageInfo, paths []string)
173180
})
174181
}
175182
wcHooks := a.woocommerceAnalyzer.AnalyzeHooksFromWP(wcInputHooks)
176-
if len(wcHooks) > 0 {
177-
wcInfo := &woocommerce.WooCommerceInfo{Hooks: wcHooks}
183+
if len(wcHooks) > 0 || len(wcAPICalls) > 0 {
184+
wcInfo := &woocommerce.WooCommerceInfo{
185+
Hooks: wcHooks,
186+
APICalls: wcAPICalls,
187+
}
178188
info.WooCommerceInfo = wcInfo
179189
}
180190

@@ -428,7 +438,7 @@ func (a *Analyzer) convertToChunks(info *WordPressInfo) []php.CodeChunk {
428438
FilePath: wcHook.FilePath,
429439
StartLine: wcHook.StartLine,
430440
EndLine: wcHook.EndLine,
431-
Signature: fmt.Sprintf("%s('%s', '%s')", wcHook.HookType, wcHook.HookName, wcHook.Callback),
441+
Signature: buildWCHookSignature(wcHook),
432442
Docstring: fmt.Sprintf("WooCommerce %s hook (%s area): %s", wcHook.HookType, wcHook.Area, wcHook.HookName),
433443
Metadata: map[string]any{
434444
"framework": "wordpress",
@@ -504,6 +514,33 @@ func buildHookSignature(hook WPHook) string {
504514
}
505515
}
506516

517+
// buildWCHookSignature creates a readable signature for a WooCommerce hook
518+
// using the real WordPress function names instead of raw hook type values
519+
func buildWCHookSignature(wcHook woocommerce.WCHook) string {
520+
switch wcHook.HookType {
521+
case "action":
522+
if wcHook.Priority > 0 {
523+
return fmt.Sprintf("add_action('%s', '%s', %d)", wcHook.HookName, wcHook.Callback, wcHook.Priority)
524+
}
525+
return fmt.Sprintf("add_action('%s', '%s')", wcHook.HookName, wcHook.Callback)
526+
case "filter":
527+
if wcHook.Priority > 0 {
528+
return fmt.Sprintf("add_filter('%s', '%s', %d)", wcHook.HookName, wcHook.Callback, wcHook.Priority)
529+
}
530+
return fmt.Sprintf("add_filter('%s', '%s')", wcHook.HookName, wcHook.Callback)
531+
case "action_trigger":
532+
return fmt.Sprintf("do_action('%s')", wcHook.HookName)
533+
case "filter_trigger":
534+
return fmt.Sprintf("apply_filters('%s')", wcHook.HookName)
535+
case "action_removal":
536+
return fmt.Sprintf("remove_action('%s', '%s')", wcHook.HookName, wcHook.Callback)
537+
case "filter_removal":
538+
return fmt.Sprintf("remove_filter('%s', '%s')", wcHook.HookName, wcHook.Callback)
539+
default:
540+
return fmt.Sprintf("%s('%s', '%s')", wcHook.HookType, wcHook.HookName, wcHook.Callback)
541+
}
542+
}
543+
507544
// IsWordPressProject detects if the given paths contain a WordPress project.
508545
// It first checks the paths directly (directory-level indicators, plugin headers),
509546
// then walks UP parent directories to find WordPress root indicators.

pkg/parser/php/wordpress/woocommerce/analyzer.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ func extractExprValue(expr ast.Vertex) string {
333333
return val
334334
case *ast.ScalarLnumber:
335335
return string(n.Value)
336+
case *ast.ScalarDnumber:
337+
return string(n.Value)
336338
case *ast.Name:
337339
var parts []string
338340
for _, part := range n.Parts {
@@ -341,6 +343,14 @@ func extractExprValue(expr ast.Vertex) string {
341343
}
342344
}
343345
return strings.Join(parts, "\\")
346+
case *ast.ExprConstFetch:
347+
return extractExprValue(n.Const)
348+
case *ast.ExprClassConstFetch:
349+
if constName, ok := n.Const.(*ast.Identifier); ok {
350+
if string(constName.Value) == "class" {
351+
return extractExprValue(n.Class) + "::class"
352+
}
353+
}
344354
case *ast.ExprVariable:
345355
if nameNode, ok := n.Name.(*ast.Identifier); ok {
346356
name := string(nameNode.Value)

0 commit comments

Comments
 (0)