From 5baa28bb27ff28a0cd544325de89e84bce09c699 Mon Sep 17 00:00:00 2001 From: Tyler Christensen Date: Mon, 24 Apr 2023 13:10:37 -0600 Subject: [PATCH] php8: resolve attributes in NamespaceResolver --- pkg/visitor/nsresolver/namespace_resolver.go | 4 +++ .../nsresolver/namespace_resolver_test.go | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pkg/visitor/nsresolver/namespace_resolver.go b/pkg/visitor/nsresolver/namespace_resolver.go index 5784916..6fd667c 100644 --- a/pkg/visitor/nsresolver/namespace_resolver.go +++ b/pkg/visitor/nsresolver/namespace_resolver.go @@ -222,6 +222,10 @@ func (nsr *NamespaceResolver) StmtTraitUse(n *ast.StmtTraitUse) { } } +func (nsr *NamespaceResolver) Attribute(n *ast.Attribute) { + nsr.ResolveName(n.Name, "") +} + // LeaveNode is invoked after node process func (nsr *NamespaceResolver) LeaveNode(n ast.Vertex) { switch nn := n.(type) { diff --git a/pkg/visitor/nsresolver/namespace_resolver_test.go b/pkg/visitor/nsresolver/namespace_resolver_test.go index b4d07cb..da58a79 100644 --- a/pkg/visitor/nsresolver/namespace_resolver_test.go +++ b/pkg/visitor/nsresolver/namespace_resolver_test.go @@ -385,6 +385,7 @@ func TestResolveTraitUse(t *testing.T) { func TestResolveClassName(t *testing.T) { nameAB := &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("A")}, &ast.NamePart{Value: []byte("B")}}} nameBC := &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("B")}, &ast.NamePart{Value: []byte("C")}}} + nameCD := &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("C")}, &ast.NamePart{Value: []byte("D")}}} class := &ast.StmtClass{ Name: &ast.Identifier{Value: []byte("A")}, @@ -392,6 +393,13 @@ func TestResolveClassName(t *testing.T) { Implements: []ast.Vertex{ nameBC, }, + AttrGroups: []ast.Vertex{ + &ast.AttributeGroup{ + Attrs: []ast.Vertex{ + &ast.Attribute{Name: nameCD}, + }, + }, + }, } stxTree := &ast.StmtStmtList{ @@ -404,6 +412,7 @@ func TestResolveClassName(t *testing.T) { class: "A", nameAB: "A\\B", nameBC: "B\\C", + nameCD: "C\\D", } nsResolver := nsresolver.NewNamespaceResolver() @@ -629,6 +638,19 @@ func TestResolveNamespaces(t *testing.T) { nameFG := &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("F")}, &ast.NamePart{Value: []byte("G")}}} relativeNameCE := &ast.NameRelative{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("C")}, &ast.NamePart{Value: []byte("E")}}} + relativeNameCA := &ast.NameRelative{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("C")}, &ast.NamePart{Value: []byte("A")}}} + + classA := &ast.StmtClass{ + Name: &ast.Identifier{Value: []byte("A")}, + AttrGroups: []ast.Vertex{ + &ast.AttributeGroup{ + Attrs: []ast.Vertex{ + &ast.Attribute{Name: relativeNameCA}, + }, + }, + }, + } + constantB := &ast.StmtConstant{ Name: &ast.Identifier{Value: []byte("B")}, Expr: &ast.ScalarLnumber{Value: []byte("1")}, @@ -643,6 +665,9 @@ func TestResolveNamespaces(t *testing.T) { &ast.StmtNamespace{ Name: namespaceAB, }, + + classA, + &ast.StmtConstList{ Consts: []ast.Vertex{ constantB, @@ -653,6 +678,7 @@ func TestResolveNamespaces(t *testing.T) { Class: nameFG, Call: &ast.Identifier{Value: []byte("foo")}, }, + &ast.StmtNamespace{ Stmts: []ast.Vertex{}, }, @@ -680,6 +706,8 @@ func TestResolveNamespaces(t *testing.T) { } expected := map[ast.Vertex]string{ + classA: "A\\B\\A", + relativeNameCA: "A\\B\\C\\A", constantB: "A\\B\\B", constantC: "A\\B\\C", nameFG: "A\\B\\F\\G",