Skip to content

Commit

Permalink
fix(php): support pattern variables for parameter types
Browse files Browse the repository at this point in the history
  • Loading branch information
didroe committed Oct 10, 2023
1 parent 115e5b2 commit 0265ba2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(*builder.Result)({
Query: (string) (len=210) "([(class_declaration . (_) . [(declaration_list [(method_declaration [ (visibility_modifier )] (_) . [(formal_parameters . [(simple_parameter . (_) (_) @match)] . )] [ (compound_statement )])] )] .)] @root)",
VariableNames: ([]string) (len=1) {
(string) (len=1) "_"
},
ParamToVariable: (map[string]string) {
},
EqualParams: ([][]string) <nil>,
ParamToContent: (map[string]map[string]string) {
},
RootVariable: (*language.PatternVariable)(<nil>)
})
28 changes: 25 additions & 3 deletions internal/languages/php/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ var (
patternMatchNodeContainerTypes = []string{"formal_parameters", "simple_parameter", "argument"}

allowedPatternQueryTypes = []string{"_"}

functionRegex = regexp.MustCompile(`\bfunction\b`)
parameterTypeRegex = regexp.MustCompile(`[,(]\s*(public|private|protected|var)?\s*\z`)
)

type Pattern struct {
Expand All @@ -39,13 +42,26 @@ func (*Pattern) FixupMissing(node *tree.Node) string {
}

func (*Pattern) FixupVariableDummyValue(input []byte, node *tree.Node, dummyValue string) string {
addDollar := false

if parent := node.Parent(); parent != nil {
if parent.Type() == "named_type" ||
(parent.Type() == "ERROR" && parent.Parent() != nil && parent.Parent().Type() == "declaration_list") {
return "$" + dummyValue
if parent.Type() == "named_type" {
addDollar = true
}

if parent.Type() == "ERROR" && parent.Parent() != nil && parent.Parent().Type() == "declaration_list" {
parentContent := []byte(parent.Content())
parentPrefix := string(parentContent[:node.ContentStart.Byte-parent.ContentStart.Byte])

isFunctionName := functionRegex.MatchString(parentPrefix) && !strings.Contains(parentPrefix, "(")
addDollar = !isFunctionName && !parameterTypeRegex.MatchString(parentPrefix)
}
}

if addDollar {
return "$" + dummyValue
}

return dummyValue
}

Expand Down Expand Up @@ -143,6 +159,12 @@ func (*Pattern) IsAnchored(node *tree.Node) (bool, bool) {
return true, true
}

// optional type on parameters
if slices.Contains([]string{"property_promotion_parameter", "simple_parameter"}, parent.Type()) &&
node == parent.ChildByFieldName("name") {
return false, true
}

if parent.Type() == "method_declaration" {
// visibility
if node == parent.ChildByFieldName("name") {
Expand Down
5 changes: 5 additions & 0 deletions internal/languages/php/php_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func TestPattern(t *testing.T) {
public $<!>$<_>;
}
`},
{"parameter names are unanchored", `
class $<_> {
public function $<_>($<_> $<!>$<_>) {}
}
`},
} {
t.Run(test.name, func(tt *testing.T) {
result, err := patternquerybuilder.Build(php.Get(), test.pattern, "")
Expand Down

0 comments on commit 0265ba2

Please sign in to comment.