diff --git a/internal/aggregate/aggregate.go b/internal/aggregate/aggregate.go index 00d8a9b6..b487d354 100644 --- a/internal/aggregate/aggregate.go +++ b/internal/aggregate/aggregate.go @@ -6,6 +6,7 @@ import ( "slices" "sort" "strconv" + "strings" "github.com/rectorphp/argtyper/internal/collect" "github.com/rectorphp/argtyper/internal/phpast" @@ -158,12 +159,13 @@ func dedupeClasses(types []valuetype.Type) []valuetype.Type { return kept } +// PHP method and function names are case-insensitive, so keys lowercase them. func methodKey(class, method string, position int) string { - return phpast.ClassKey(class) + "\x00" + method + "\x00" + strconv.Itoa(position) + return phpast.ClassKey(class) + "\x00" + strings.ToLower(method) + "\x00" + strconv.Itoa(position) } func functionKey(name string, position int) string { - return name + "\x00" + strconv.Itoa(position) + return strings.ToLower(name) + "\x00" + strconv.Itoa(position) } func contains(values []valuetype.Type, needle valuetype.Type) bool { diff --git a/internal/aggregate/aggregate_test.go b/internal/aggregate/aggregate_test.go index 91d99458..46acb650 100644 --- a/internal/aggregate/aggregate_test.go +++ b/internal/aggregate/aggregate_test.go @@ -119,6 +119,23 @@ func TestResolveFunctionAndPositionIsolation(t *testing.T) { } } +func TestResolveMatchesNamesCaseInsensitively(t *testing.T) { + records := []collect.Record{ + {Class: "A", Name: "Room", Position: 0, Type: "int"}, + {IsFunction: true, Name: "Greet", Position: 0, Type: "string"}, + } + types := aggregate.Resolve(records) + + method, ok := types.Method("A", "room", 0) + if !ok || join(method.Types) != "int" { + t.Errorf("method: got %+v ok=%v", method, ok) + } + function, ok := types.Function("greet", 0) + if !ok || join(function.Types) != "string" { + t.Errorf("function: got %+v ok=%v", function, ok) + } +} + func TestResolveLiterals(t *testing.T) { literal := func(value string) collect.Record { return collect.Record{Class: "A", Name: "m", Type: "string", IsLiteral: true, Literal: value} diff --git a/internal/apply/apply.go b/internal/apply/apply.go index 0edb95a9..b0aea583 100644 --- a/internal/apply/apply.go +++ b/internal/apply/apply.go @@ -285,7 +285,7 @@ func typeable(param *ast.Parameter) bool { // never saw) is treated as unsafe. Other trait methods are skipped, since the // class using the trait may override a parent method with them. func (a *applier) methodTypeable(method *ast.StmtClassMethod, class ast.Vertex) bool { - if phpast.ShortName(method.Name) == "__construct" { + if strings.EqualFold(phpast.ShortName(method.Name), "__construct") { return true } for _, modifierNode := range method.Modifiers { @@ -320,7 +320,7 @@ func (a *applier) methodTypeable(method *ast.StmtClassMethod, class ast.Vertex) } func isMagicExceptConstructor(name string) bool { - return name != "__construct" && strings.HasPrefix(name, "__") + return !strings.EqualFold(name, "__construct") && strings.HasPrefix(name, "__") } func hasNullDefault(param *ast.Parameter) bool { diff --git a/internal/apply/apply_test.go b/internal/apply/apply_test.go index 82bea843..f7f8caae 100644 --- a/internal/apply/apply_test.go +++ b/internal/apply/apply_test.go @@ -39,6 +39,11 @@ func TestApply(t *testing.T) { target: "room(5); }\n}", want: "room(5); }\n}", }, + { + name: "adds int to method called in different case", + target: "Room(5); }\n}", + want: "Room(5); }\n}", + }, { name: "adds string to function", target: "repo->find(1); }\n}", want: []collect.Record{{Class: "Repo", Name: "find", Position: 0, Type: "int"}}, }, + { + name: "promoted property of constructor in different case resolves this-property call", + src: "repo->find(1); }\n}", + want: []collect.Record{{Class: "Repo", Name: "find", Position: 0, Type: "int"}}, + }, { name: "nullsafe property call resolves target class", src: "repo?->find(1); }\n}",