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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/rectorphp/argtyper

go 1.26

require github.com/rectorphp/php-parser-in-go v0.1.1
require github.com/rectorphp/php-parser-in-go v0.1.2-0.20260914171035-ef8c7cbb139d
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rectorphp/php-parser-in-go v0.1.1 h1:9jMbxAtDBjubUlPFKB5lbbvpXn8Otxq9eG77Q7moqwk=
github.com/rectorphp/php-parser-in-go v0.1.1/go.mod h1:6p9QnZnLGc9uruvKuSXlVt4uSIY5Rqe6h2aj9+hEBG8=
github.com/rectorphp/php-parser-in-go v0.1.2-0.20260914171035-ef8c7cbb139d h1:T26rEJvnupxPmDhRGulHue2rj83Ncl6DtjCLbbMFjT4=
github.com/rectorphp/php-parser-in-go v0.1.2-0.20260914171035-ef8c7cbb139d/go.mod h1:6p9QnZnLGc9uruvKuSXlVt4uSIY5Rqe6h2aj9+hEBG8=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
5 changes: 5 additions & 0 deletions internal/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ func TestApply(t *testing.T) {
},
want: "<?php\nnamespace App;\n\nfunction handle(\\App\\Entity\\Lead $item) {}",
},
{
name: "qualifies arrow function typed parameter argument from use import",
target: "<?php\nnamespace App;\n\nuse App\\Entity\\Lead;\n\nfinal class Repo {\n public function save($entity) {}\n public function go() { $run = fn (Lead $lead) => $this->save($lead); }\n}",
want: "<?php\nnamespace App;\n\nuse App\\Entity\\Lead;\n\nfinal class Repo {\n public function save(\\App\\Entity\\Lead $entity) {}\n public function go() { $run = fn (Lead $lead) => $this->save($lead); }\n}",
},
{
name: "qualifies typed parameter argument from use import",
target: "<?php\nnamespace App;\n\nuse App\\Entity\\Lead;\n\nfinal class Repo {\n public function save($entity) {}\n public function go(Lead $lead) { $this->save($lead); }\n}",
Expand Down
19 changes: 19 additions & 0 deletions internal/collect/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func (c *collector) walk(node ast.Vertex, sc scope) {
case *ast.ExprClosure:
sc.params = c.paramClasses(typed.Params, sc.classFQCN)
sc.locals = c.localClasses(typed.Stmts)
case *ast.ExprArrowFunction:
// arrow functions capture outer variables, so keep the inherited params
// and locals and overlay the arrow's own typed parameters.
sc.params = merge(sc.params, c.paramClasses(typed.Params, sc.classFQCN))
}

c.visit(node, sc)
Expand Down Expand Up @@ -291,6 +295,21 @@ func (c *collector) classFromType(typeNode ast.Vertex, enclosing string) string
return name
}

// merge overlays the over map onto a copy of base, with over winning on clashes.
func merge(base, over map[string]string) map[string]string {
if len(over) == 0 {
return base
}
merged := make(map[string]string, len(base)+len(over))
for name, class := range base {
merged[name] = class
}
for name, class := range over {
merged[name] = class
}
return merged
}

// shortName returns the last segment of a fully qualified name, the form used
// as a method lookup key.
func shortName(fqcn string) string {
Expand Down
Loading