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
57 changes: 50 additions & 7 deletions internal/collect/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,26 @@ func (c *collector) record(args []ast.Vertex, base Record, sc scope) {
continue
}

typeName := c.argType(arg.Expr, sc)
if typeName == "" {
continue
for _, typeName := range c.argTypes(arg.Expr, sc) {
record := base
record.Position = position
record.Type = typeName
c.records = append(c.records, record)
}
}
}

record := base
record.Position = position
record.Type = typeName
c.records = append(c.records, record)
// argTypes returns the type(s) an argument contributes. A coalesce expression
// (`$x ?? null`) contributes the types of both sides, so `... ?? null` makes the
// parameter nullable; every other expression contributes at most one type.
func (c *collector) argTypes(expr ast.Vertex, sc scope) []string {
if coalesce, ok := expr.(*ast.ExprBinaryCoalesce); ok {
return append(c.argTypes(coalesce.Left, sc), c.argTypes(coalesce.Right, sc)...)
}
if typeName := c.argType(expr, sc); typeName != "" {
return []string{typeName}
}
return nil
}

// argType returns the type of an argument value as a fully qualified object
Expand All @@ -346,6 +356,13 @@ func (c *collector) argType(expr ast.Vertex, sc scope) string {
return "object:" + class
}

switch expr.(type) {
case *ast.ExprMethodCall, *ast.ExprNullsafeMethodCall:
if class := c.newChainClass(expr); class != "" {
return "object:" + class
}
}

typeName := c.symbols.TypeOfExpr(expr, sc.class)
if !strings.HasPrefix(typeName, "object:") {
return typeName
Expand All @@ -372,6 +389,32 @@ func (c *collector) objectFQCN(expr ast.Vertex, sc scope) string {
return c.names[classNode]
}

// newChainClass returns the fully qualified class of the object a method chain
// is rooted at, when that root is a `new X()`. Fluent methods are assumed to
// return their receiver, so `new X()->modify(...)` has type X. Empty when the
// chain is not rooted at a new, or the class is self/static/parent.
func (c *collector) newChainClass(expr ast.Vertex) string {
for {
switch typed := expr.(type) {
case *ast.ExprMethodCall:
expr = typed.Var
case *ast.ExprNullsafeMethodCall:
expr = typed.Var
case *ast.ExprNew:
switch strings.ToLower(phpast.ShortName(typed.Class)) {
case "", "self", "static", "parent":
return ""
}
if fqcn := c.names[typed.Class]; fqcn != "" {
return fqcn
}
return phpast.ShortName(typed.Class)
default:
return ""
}
}
}

// argClass resolves the fully qualified class of a variable or `$this->prop`
// argument: `$this`, a typed parameter, a `new X()` local, or a typed property.
func argClass(expr ast.Vertex, sc scope) string {
Expand Down
23 changes: 23 additions & 0 deletions internal/collect/collect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,29 @@ func TestFromSource(t *testing.T) {
src: "<?php\nclass A {\n static function fmt($day): string { return \"\"; }\n function go($d) { $d = new \\DateTime(self::fmt($d)); }\n}",
want: nil,
},
{
name: "method chain rooted at new resolves to receiver class",
src: "<?php\nclass A {\n function go() { $this->set(new \\DateTime()->modify('+2 hours')); }\n}",
want: []collect.Record{{Class: "A", Name: "set", Position: 0, Type: "object:DateTime"}},
},
{
name: "nullsafe method chain rooted at new resolves to receiver class",
src: "<?php\nclass A {\n function go() { $this->set(new \\DateTime()?->modify('+2 hours')); }\n}",
want: []collect.Record{{Class: "A", Name: "set", Position: 0, Type: "object:DateTime"}},
},
{
name: "coalesce contributes both sides",
src: "<?php\nclass A {\n function go($e) { $this->set('x' ?? null); }\n}",
want: []collect.Record{
{Class: "A", Name: "set", Position: 0, Type: "string"},
{Class: "A", Name: "set", Position: 0, Type: "null"},
},
},
{
name: "coalesce with unresolvable left contributes null only",
src: "<?php\nclass A {\n function go($e) { $this->set($e['k'] ?? null); }\n}",
want: []collect.Record{{Class: "A", Name: "set", Position: 0, Type: "null"}},
},
{
name: "skips parent static call",
src: "<?php\nclass A {\n function go() { parent::set(1); }\n}",
Expand Down
Loading