From 5a9c67bbf91923cf4117e5952159880eacbfd725 Mon Sep 17 00:00:00 2001 From: David Roe Date: Tue, 10 Oct 2023 11:30:53 +0100 Subject: [PATCH] fix(php): lookup variables in subscript indices --- internal/languages/php/analyzer/analyzer.go | 13 +++++++++++++ .../php/detectors/.snapshots/TestPHPString-string | 5 ++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/internal/languages/php/analyzer/analyzer.go b/internal/languages/php/analyzer/analyzer.go index 6669c5056..803adaddb 100644 --- a/internal/languages/php/analyzer/analyzer.go +++ b/internal/languages/php/analyzer/analyzer.go @@ -47,6 +47,8 @@ func (analyzer *analyzer) Analyze(node *sitter.Node, visitChildren func() error) return visitChildren() case "dynamic_variable_name": return analyzer.analyzeDynamicVariableName(node, visitChildren) + case "subscript_expression": + return analyzer.analyzeSubscript(node, visitChildren) case "binary_expression", "unary_op_expression", "argument", @@ -178,6 +180,17 @@ func (analyzer *analyzer) analyzeDynamicVariableName(node *sitter.Node, visitChi return visitChildren() } +// foo["bar"] +func (analyzer *analyzer) analyzeSubscript(node *sitter.Node, visitChildren func() error) error { + object := node.NamedChild(0) + analyzer.builder.Dataflow(node, object) + analyzer.lookupVariable(object) + + analyzer.lookupVariable(node.NamedChild(1)) + + return visitChildren() +} + // default analysis, where the children are assumed to be aliases func (analyzer *analyzer) analyzeGenericConstruct(node *sitter.Node, visitChildren func() error) error { analyzer.builder.Alias(node, analyzer.builder.ChildrenFor(node)...) diff --git a/internal/languages/php/detectors/.snapshots/TestPHPString-string b/internal/languages/php/detectors/.snapshots/TestPHPString-string index 720960508..4e645c5aa 100644 --- a/internal/languages/php/detectors/.snapshots/TestPHPString-string +++ b/internal/languages/php/detectors/.snapshots/TestPHPString-string @@ -353,15 +353,14 @@ children: range: 11:16 - 11:24 dataflow_sources: - 80 - - 83 - - 84 - - 85 queries: - 4 children: - type: variable_name id: 80 range: 11:16 - 11:21 + alias_of: + - 27 children: - type: '"$"' id: 81