Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions internal/aggregate/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"

"github.com/rectorphp/argtyper/internal/collect"
"github.com/rectorphp/argtyper/internal/phpast"
"github.com/rectorphp/argtyper/internal/valuetype"
)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions internal/aggregate/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
Expand Down
33 changes: 33 additions & 0 deletions internal/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,39 @@ func TestApply(t *testing.T) {
},
want: "<?php\nnamespace App\\Y;\nclass Foo { public function __construct(string $a) {} }\nnew Foo(\"s\");",
},
{
name: "adds type from constructor called with different class case",
target: "<?php\nclass Money {\n public function __construct($amount) {}\n}",
callers: []string{
"<?php\nnew money(100);",
},
want: "<?php\nclass Money {\n public function __construct(int $amount) {}\n}",
},
{
name: "adds type from call on class imported in different case",
target: "<?php\nnamespace App\\Entity;\n\nfinal class Lead {\n public function rename($name) {}\n}",
callers: []string{
"<?php\nnamespace App;\n\nuse App\\entity\\lead;\n\nfunction go(Lead $lead) { $lead->rename('x'); }",
},
want: "<?php\nnamespace App\\Entity;\n\nfinal class Lead {\n public function rename(string $name) {}\n}",
},
{
name: "keeps written case of object type from different-case callers",
target: "<?php\nfunction handle($item) {}",
callers: []string{
"<?php\nhandle(new Lead());",
"<?php\nhandle(new lead());",
},
want: "<?php\nfunction handle(\\Lead $item) {}",
},
{
name: "types method whose parent in different class case does not declare it",
target: "<?php\nclass A extends base {\n public function set($v) {}\n public function go() { $this->set(1); }\n}",
callers: []string{
"<?php\nclass Base {\n public function other() {}\n}",
},
want: "<?php\nclass A extends base {\n public function set(int $v) {}\n public function go() { $this->set(1); }\n}",
},
{
name: "adds type from call on imported class",
target: "<?php\nnamespace App\\Entity;\n\nfinal class Lead {\n public function rename($name) {}\n}",
Expand Down
16 changes: 8 additions & 8 deletions internal/inherit/inherit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (

// class holds the ancestors and declared methods of one class or interface.
type class struct {
parent string // fully qualified parent class, "" if none
interfaces []string // fully qualified implemented/extended interfaces
parent string // lowercased fully qualified parent class, "" if none
interfaces []string // lowercased fully qualified implemented/extended interfaces
methods map[string]bool
}

// Table maps a fully qualified class name to its inheritance data.
// Table maps a lowercased fully qualified class name to its inheritance data.
type Table struct {
classes map[string]*class
}
Expand Down Expand Up @@ -75,10 +75,10 @@ func (t *Table) add(fqcn, parent string, interfaces []ast.Vertex, body ast.Verte
return
}

entry := &class{parent: parent, methods: map[string]bool{}}
entry := &class{parent: phpast.ClassKey(parent), methods: map[string]bool{}}
for _, interfaceNode := range interfaces {
if name := names[interfaceNode]; name != "" {
entry.interfaces = append(entry.interfaces, name)
entry.interfaces = append(entry.interfaces, phpast.ClassKey(name))
}
}
for _, member := range phpast.Children(body) {
Expand All @@ -89,7 +89,7 @@ func (t *Table) add(fqcn, parent string, interfaces []ast.Vertex, body ast.Verte
}
}

t.classes[fqcn] = entry
t.classes[phpast.ClassKey(fqcn)] = entry
}

// Overrides reports whether a method of the given class is also declared by an
Expand All @@ -98,11 +98,11 @@ func (t *Table) add(fqcn, parent string, interfaces []ast.Vertex, body ast.Verte
// class the scan never saw, typically in /vendor - may still declare it, so the
// caller should treat the method as unsafe to change.
func (t *Table) Overrides(fqcn, method string) (overrides bool, resolved bool) {
entry, ok := t.classes[fqcn]
// PHP class and method names are case-insensitive
entry, ok := t.classes[phpast.ClassKey(fqcn)]
if !ok {
return false, false
}
// PHP method names are case-insensitive
method = strings.ToLower(method)

resolved = true
Expand Down
3 changes: 3 additions & 0 deletions internal/inherit/inherit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestOverrides(t *testing.T) {
table.CollectSource([]byte("<?php\nnamespace App;\n\ninterface Contract {\n public function handle($v);\n}"))
table.CollectSource([]byte("<?php\nnamespace App;\n\nclass Child extends Base implements Contract {\n public function save($v) {}\n public function handle($v) {}\n public function own($v) {}\n}"))
table.CollectSource([]byte("<?php\nnamespace App;\n\nclass Loud {\n public function Shout($v) {}\n}\n\nclass Quiet extends Loud {}"))
table.CollectSource([]byte("<?php\nnamespace App;\n\nclass Lower extends base {}"))
table.CollectSource([]byte("<?php\nnamespace App;\n\nclass Orphan extends \\Vendor\\Base {\n public function run($v) {}\n}"))

tests := []struct {
Expand All @@ -26,6 +27,8 @@ func TestOverrides(t *testing.T) {
{"inherited with different case", "App\\Child", "SAVE", true, true},
{"looked up in different case on interface", "App\\Child", "Handle", true, true},
{"declared with different case on ancestor", "App\\Quiet", "shout", true, true},
{"class looked up in different case", "app\\CHILD", "save", true, true},
{"parent written in different case", "App\\Lower", "save", true, true},
{"parent chain reaches vendor", "App\\Orphan", "run", false, false},
{"unknown class", "App\\Missing", "x", false, false},
}
Expand Down
6 changes: 6 additions & 0 deletions internal/phpast/phpast.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ func ResolvedName(names map[ast.Vertex]string, node ast.Vertex) string {
return ShortName(node)
}

// ClassKey returns a class name as a lookup key. PHP class names are
// case-insensitive, so keys are lowercased; written type text keeps its case.
func ClassKey(name string) string {
return strings.ToLower(name)
}

// ObjectClassNode returns the class-name node of a value that produces an
// object - `new X()` or a `X::CASE` enum case - or nil for anything else.
func ObjectClassNode(expr ast.Vertex) ast.Vertex {
Expand Down
11 changes: 6 additions & 5 deletions internal/symbols/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

// Table holds the project symbols gathered across every file.
type Table struct {
enums map[string]bool // fully qualified enum name
enums map[string]bool // lowercased fully qualified enum name
consts map[string]valuetype.Type // CONST name -> 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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)]
Expand All @@ -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
}
3 changes: 3 additions & 0 deletions internal/symbols/symbols_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "", ""},
Expand Down
Loading