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: 6 additions & 0 deletions internal/aggregate/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func resolveGroups(groups map[string]map[string]struct{}) map[string]Resolved {
}
sort.Strings(types)

// a callable argument (a closure) leaves the parameter untyped, since a
// callable can take many shapes that must not be narrowed
if contains(types, "callable") {
continue
}

nullable := contains(types, "null")
members := without(types, "null")
if len(members) == 0 {
Expand Down
16 changes: 12 additions & 4 deletions internal/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,28 @@ func TestApply(t *testing.T) {
want: "<?php\nfunction take(array $items = []) {}",
},
{
name: "types closure argument",
name: "leaves a closure argument untyped",
target: "<?php\nfunction run($cb) {}",
callers: []string{
"<?php\nrun(function () {});",
},
want: "<?php\nfunction run(\\Closure $cb) {}",
want: "<?php\nfunction run($cb) {}",
},
{
name: "types arrow function argument",
name: "leaves an arrow function argument untyped",
target: "<?php\nfunction run($cb) {}",
callers: []string{
"<?php\nrun(fn () => 1);",
},
want: "<?php\nfunction run(\\Closure $cb) {}",
want: "<?php\nfunction run($cb) {}",
},
{
name: "a closure poisons the parameter even with another type",
target: "<?php\nfunction run($cb) {}",
callers: []string{
"<?php\nrun(function () {});\nrun(1);",
},
want: "<?php\nfunction run($cb) {}",
},
{
name: "types from builtin return value",
Expand Down
6 changes: 5 additions & 1 deletion internal/valuetype/valuetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ func Of(expr ast.Vertex) string {
case *ast.ExprArray:
return "array"
case *ast.ExprClosure, *ast.ExprArrowFunction:
return "object:Closure"
// a closure argument means the parameter takes a callable, which can be
// many things (a closure, a "func" string, a [$obj, method] array, a
// first-class callable). "callable" is a poison type: it is never
// written, and it stops the parameter from being typed at all.
return "callable"
case *ast.ExprUnaryMinus:
return numericType(typed.Expr)
case *ast.ExprUnaryPlus:
Expand Down
4 changes: 2 additions & 2 deletions internal/valuetype/valuetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func TestOf(t *testing.T) {
{"true", "true", "bool"},
{"null", "null", "null"},
{"negative int", "-3", "int"},
{"closure", "function () {}", "object:Closure"},
{"arrow function", "fn () => 1", "object:Closure"},
{"closure", "function () {}", "callable"},
{"arrow function", "fn () => 1", "callable"},
{"class name fetch", "Foo::class", "string"},
{"new object", "new Money()", "object:Money"},
{"builtin int return", "strlen($x)", "int"},
Expand Down
Loading