diff --git a/internal/aggregate/aggregate.go b/internal/aggregate/aggregate.go index 8e38a964..00d8a9b6 100644 --- a/internal/aggregate/aggregate.go +++ b/internal/aggregate/aggregate.go @@ -8,6 +8,7 @@ import ( "strconv" "github.com/rectorphp/argtyper/internal/collect" + "github.com/rectorphp/argtyper/internal/phpast" "github.com/rectorphp/argtyper/internal/valuetype" ) @@ -34,7 +35,7 @@ type group struct { } // Types holds resolved parameter types keyed for fast lookup during apply. -// Method keys use the fully qualified class name. +// Method keys use the lowercased fully qualified class name. type Types struct { methods map[string]Resolved // class \x00 method \x00 position functions map[string]Resolved // function \x00 position @@ -83,6 +84,7 @@ func resolveGroups(groups map[string]*group) map[string]Resolved { types = append(types, typeName) } slices.Sort(types) + types = dedupeClasses(types) // a callable argument (a closure) leaves the parameter untyped, since a // callable can take many shapes that must not be narrowed @@ -137,8 +139,27 @@ func addRecord(groups map[string]*group, key string, record collect.Record) { } } +// dedupeClasses drops object types naming the same class in another case, as +// PHP class names are case-insensitive. Types are sorted, so the kept casing is +// deterministic. +func dedupeClasses(types []valuetype.Type) []valuetype.Type { + seen := map[string]bool{} + kept := make([]valuetype.Type, 0, len(types)) + for _, typeName := range types { + if typeName.IsObject() { + key := phpast.ClassKey(typeName.Class()) + if seen[key] { + continue + } + seen[key] = true + } + kept = append(kept, typeName) + } + return kept +} + func methodKey(class, method string, position int) string { - return class + "\x00" + method + "\x00" + strconv.Itoa(position) + return phpast.ClassKey(class) + "\x00" + method + "\x00" + strconv.Itoa(position) } func functionKey(name string, position int) string { diff --git a/internal/aggregate/aggregate_test.go b/internal/aggregate/aggregate_test.go index 97756838..91d99458 100644 --- a/internal/aggregate/aggregate_test.go +++ b/internal/aggregate/aggregate_test.go @@ -62,6 +62,21 @@ func TestResolveMethod(t *testing.T) { wantType: "int", wantFound: true, }, + { + name: "class name differing in case shares the key", + records: []collect.Record{{Class: "a", Name: "m", Type: "int"}}, + wantType: "int", + wantFound: true, + }, + { + name: "same class in another case is one object type", + records: []collect.Record{ + {Class: "A", Name: "m", Type: valuetype.Object("App\\Foo")}, + {Class: "A", Name: "m", Type: valuetype.Object("App\\foo")}, + }, + wantType: "object:App\\Foo", + wantFound: true, + }, { name: "only unresolved resolves to nothing", records: []collect.Record{{Class: "A", Name: "m"}}, diff --git a/internal/apply/apply_test.go b/internal/apply/apply_test.go index b539a93b..82bea843 100644 --- a/internal/apply/apply_test.go +++ b/internal/apply/apply_test.go @@ -363,6 +363,39 @@ func TestApply(t *testing.T) { }, want: "rename('x'); }", + }, + want: "set(1); }\n}", + callers: []string{ + "set(1); }\n}", + }, { name: "adds type from call on imported class", target: " type - classConsts map[string]valuetype.Type // "Fqcn\x00CONST" -> type + classConsts map[string]valuetype.Type // "lowercased fqcn\x00CONST" -> type } // New returns an empty table ready for Collect. @@ -60,7 +60,7 @@ func (t *Table) walk(node ast.Vertex, enclosing string, names map[ast.Vertex]str enclosing = phpast.ResolvedName(names, typed) case *ast.StmtEnum: enclosing = phpast.ResolvedName(names, typed) - t.enums[enclosing] = true + t.enums[phpast.ClassKey(enclosing)] = true case *ast.StmtConstList: t.recordConsts(typed.Consts, enclosing) case *ast.StmtClassConstList: @@ -156,7 +156,7 @@ func (t *Table) classConst(fetch *ast.ExprClassConstFetch, enclosing string, nam return valuetype.Unknown } - if t.enums[class] { + if t.enums[phpast.ClassKey(class)] { return valuetype.Object(class) } return t.classConsts[classConstKey(class, constName)] @@ -171,5 +171,6 @@ func stringLiteral(expr ast.Vertex) string { } func classConstKey(class, name string) string { - return class + "\x00" + name + // constant names are case-sensitive, only the class part is normalized + return phpast.ClassKey(class) + "\x00" + name } diff --git a/internal/symbols/symbols_test.go b/internal/symbols/symbols_test.go index 692efa96..04781820 100644 --- a/internal/symbols/symbols_test.go +++ b/internal/symbols/symbols_test.go @@ -25,6 +25,9 @@ func TestTypeOfExpr(t *testing.T) { {"self const", "LIMIT", "", ""}, // bare name, not the const fetch form {"self class const", "self::LIMIT", "Config", "float"}, {"enum case", "Status::Active", "", valuetype.Object("Status")}, + {"class const with different class case", "config::LIMIT", "", "float"}, + {"class const name stays case-sensitive", "Config::limit", "", ""}, + {"enum case with different class case", "status::Active", "", valuetype.Object("status")}, {"class name stays string", "Status::class", "", "string"}, {"unknown const", "NOPE", "", ""}, {"unknown class const", "Other::THING", "", ""},