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
6 changes: 4 additions & 2 deletions internal/aggregate/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"slices"
"sort"
"strconv"
"strings"

"github.com/rectorphp/argtyper/internal/collect"
"github.com/rectorphp/argtyper/internal/phpast"
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions internal/aggregate/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions internal/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions internal/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func TestApply(t *testing.T) {
target: "<?php\nfinal class Hotel {\n public function room($number) {}\n public function go() { $this->room(5); }\n}",
want: "<?php\nfinal class Hotel {\n public function room(int $number) {}\n public function go() { $this->room(5); }\n}",
},
{
name: "adds int to method called in different case",
target: "<?php\nfinal class Hotel {\n public function room($number) {}\n public function go() { $this->Room(5); }\n}",
want: "<?php\nfinal class Hotel {\n public function room(int $number) {}\n public function go() { $this->Room(5); }\n}",
},
{
name: "adds string to function",
target: "<?php\nfunction greet($who) {}",
Expand All @@ -55,6 +60,22 @@ func TestApply(t *testing.T) {
},
want: "<?php\nclass Money {\n public function __construct(int $amount) {}\n}",
},
{
name: "adds string to function called in different case",
target: "<?php\nfunction greet($who) {}",
callers: []string{
"<?php\nGreet(\"hi\");",
},
want: "<?php\nfunction greet(string $who) {}",
},
{
name: "adds type to constructor declared in different case",
target: "<?php\nclass Money extends Base {\n public function __Construct($amount) {}\n}",
callers: []string{
"<?php\nnew Money(100);",
},
want: "<?php\nclass Money extends Base {\n public function __Construct(int $amount) {}\n}",
},
{
name: "adds type from static call",
target: "<?php\nfinal class Factory {\n public static function make($label) {}\n}",
Expand Down
2 changes: 1 addition & 1 deletion internal/collect/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (c *collector) classProperties(class ast.Vertex, enclosing string) map[stri
}
}
case *ast.StmtClassMethod:
if phpast.ShortName(typed.Name) != "__construct" {
if !strings.EqualFold(phpast.ShortName(typed.Name), "__construct") {
continue
}
maps.Copy(properties, c.promotedProperties(typed.Params, enclosing))
Expand Down
5 changes: 5 additions & 0 deletions internal/collect/collect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func TestFromSource(t *testing.T) {
src: "<?php\nclass A {\n function __construct(private Repo $repo) {}\n function go() { $this->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: "<?php\nclass A {\n function __Construct(private Repo $repo) {}\n function go() { $this->repo->find(1); }\n}",
want: []collect.Record{{Class: "Repo", Name: "find", Position: 0, Type: "int"}},
},
{
name: "nullsafe property call resolves target class",
src: "<?php\nclass A {\n private Repo $repo;\n function go() { $this->repo?->find(1); }\n}",
Expand Down
Loading