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
15 changes: 15 additions & 0 deletions internal/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ func TestApply(t *testing.T) {
},
want: "<?php\nfinal class Factory {\n public static function make(string $label) {}\n}",
},
{
name: "adds type to constructor from new self",
target: "<?php\nnamespace App;\n\nfinal class Foo {\n public function __construct($a) {}\n public static function make() { return new self(5); }\n}",
want: "<?php\nnamespace App;\n\nfinal class Foo {\n public function __construct(int $a) {}\n public static function make() { return new self(5); }\n}",
},
{
name: "adds type to constructor from new static",
target: "<?php\nclass Foo {\n public function __construct($a) {}\n public static function make() { return new static('x'); }\n}",
want: "<?php\nclass Foo {\n public function __construct(string $a) {}\n public static function make() { return new static('x'); }\n}",
},
{
name: "adds enclosing class type from new self argument",
target: "<?php\nnamespace App;\n\nfinal class Node {\n public function link($other) {}\n public function go() { $this->link(new self()); }\n}",
want: "<?php\nnamespace App;\n\nfinal class Node {\n public function link(\\App\\Node $other) {}\n public function go() { $this->link(new self()); }\n}",
},
{
name: "nullable when null also passed",
target: "<?php\nfinal class A {\n public function set($v) {}\n public function go() { $this->set(1); $this->set(null); }\n}",
Expand Down
49 changes: 23 additions & 26 deletions internal/collect/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ func (c *collector) walk(node ast.Vertex, sc scope) {
sc = c.classScope(typed)
case *ast.StmtFunction:
sc.params = c.paramClasses(typed.Params, sc.classFQCN)
sc.locals = c.localClasses(typed.Stmts, typed.Params)
sc.locals = c.localClasses(typed.Stmts, typed.Params, sc.classFQCN)
case *ast.StmtClassMethod:
sc.params = c.paramClasses(typed.Params, sc.classFQCN)
sc.locals = c.localClasses(phpast.Children(typed.Stmt), typed.Params)
sc.locals = c.localClasses(phpast.Children(typed.Stmt), typed.Params, sc.classFQCN)
case *ast.ExprClosure:
sc.params = c.paramClasses(typed.Params, sc.classFQCN)
sc.locals = c.localClasses(typed.Stmts, typed.Params)
sc.locals = c.localClasses(typed.Stmts, typed.Params, sc.classFQCN)
case *ast.ExprArrowFunction:
// arrow functions capture outer variables, so keep the inherited params
// and locals and overlay the arrow's own typed parameters.
Expand All @@ -97,7 +97,7 @@ func (c *collector) visit(node ast.Vertex, sc scope) {
c.record(typed.Args, Record{IsFunction: true, Name: name}, sc)

case *ast.ExprNew:
name := phpast.ResolvedName(c.names, typed.Class)
name := c.staticClassName(typed.Class, sc.classFQCN)
if name == "" {
return
}
Expand Down Expand Up @@ -146,9 +146,9 @@ func (c *collector) classScope(class ast.Vertex) scope {
}
}

// staticClassName resolves the class of a static call: self/static map to the
// enclosing class, parent is unresolvable, anything else is resolved from the
// file's namespace and imports.
// staticClassName resolves the class of a static call or `new`: self/static map
// to the enclosing class, parent is unresolvable, anything else is resolved from
// the file's namespace and imports.
func (c *collector) staticClassName(classNode ast.Vertex, enclosing string) string {
switch strings.ToLower(phpast.ShortName(classNode)) {
case "self", "static":
Expand Down Expand Up @@ -322,7 +322,7 @@ func (c *collector) argType(expr ast.Vertex, sc scope) valuetype.Type {

switch expr.(type) {
case *ast.ExprMethodCall, *ast.ExprNullsafeMethodCall:
if class := c.newChainClass(expr); class != "" {
if class := c.newChainClass(expr, sc.classFQCN); class != "" {
return valuetype.Object(class)
}
}
Expand All @@ -334,29 +334,27 @@ func (c *collector) argType(expr ast.Vertex, sc scope) valuetype.Type {
if fqcn := phpast.ObjectFQCN(expr, c.names, sc.classFQCN); fqcn != "" {
return valuetype.Object(fqcn)
}
// `new parent()`, or `new self()` outside a class, has no known class
if newExpr, ok := expr.(*ast.ExprNew); ok && c.staticClassName(newExpr.Class, sc.classFQCN) == "" {
return valuetype.Unknown
}
return typeName
}

// 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 {
// return their receiver, so `new X()->modify(...)` has type X, and self/static
// map to the enclosing class. Empty when the chain is not rooted at a new, or
// the class is parent.
func (c *collector) newChainClass(expr ast.Vertex, enclosing string) 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)
return c.staticClassName(typed.Class, enclosing)
default:
return ""
}
Expand Down Expand Up @@ -398,32 +396,31 @@ func thisPropertyClass(varNode, propNode ast.Vertex, sc scope) string {
// qualified name, so those variables resolve as arguments and call targets.
// Parameters are excluded: a reassignment like `$param = new X()` must not
// retype the parameter's earlier uses, since the tracking is not flow sensitive.
func (c *collector) localClasses(stmts, params []ast.Vertex) map[string]string {
// enclosing is the class that `new self()`/`new static()` resolve to.
func (c *collector) localClasses(stmts, params []ast.Vertex, enclosing string) map[string]string {
excluded := paramNames(params)
locals := map[string]string{}
for _, stmt := range stmts {
c.collectLocals(stmt, locals, excluded)
c.collectLocals(stmt, locals, excluded, enclosing)
}
return locals
}

func (c *collector) collectLocals(node ast.Vertex, locals map[string]string, excluded map[string]bool) {
func (c *collector) collectLocals(node ast.Vertex, locals map[string]string, excluded map[string]bool, enclosing string) {
if node == nil {
return
}
if assign, ok := node.(*ast.ExprAssign); ok {
if name := phpast.VariableName(assign.Var); name != "" && !excluded[name] {
if newExpr, ok := assign.Expr.(*ast.ExprNew); ok {
if fqcn := c.names[newExpr.Class]; fqcn != "" {
locals[name] = fqcn
} else if class := phpast.ShortName(newExpr.Class); class != "" {
if class := c.staticClassName(newExpr.Class, enclosing); class != "" {
locals[name] = class
}
}
}
}
for _, child := range phpast.Children(node) {
c.collectLocals(child, locals, excluded)
c.collectLocals(child, locals, excluded, enclosing)
}
}

Expand Down
38 changes: 38 additions & 0 deletions internal/collect/collect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,44 @@ func TestFromSource(t *testing.T) {
src: "<?php\nclass A {\n function go() { parent::set(1); }\n}",
want: nil,
},
{
name: "new self and new static use fully qualified enclosing class",
src: "<?php\nnamespace App;\nclass A {\n static function make() { new self(5); new static('s'); }\n}",
want: []collect.Record{
{Class: "App\\A", Name: "__construct", Position: 0, Type: "int"},
{Class: "App\\A", Name: "__construct", Position: 0, Type: "string", IsLiteral: true, Literal: "s"},
},
},
{
name: "skips new parent",
src: "<?php\nclass A extends B {\n function go() { new parent(1); }\n}",
want: nil,
},
{
name: "skips new self and self call outside a class",
src: "<?php\nnew self(1);\nself::make(1);\nfunction f() { new static(1); }",
want: nil,
},
{
name: "new self as argument is enclosing class object",
src: "<?php\nnamespace App;\nclass A {\n function go() { $this->set(new self()); }\n}",
want: []collect.Record{{Class: "App\\A", Name: "set", Position: 0, Type: valuetype.Object("App\\A")}},
},
{
name: "new parent as argument is unresolved",
src: "<?php\nclass A extends B {\n function go() { $this->set(new parent()); }\n}",
want: []collect.Record{{Class: "A", Name: "set", Position: 0}},
},
{
name: "method chain rooted at new static resolves to enclosing class",
src: "<?php\nclass A {\n function go() { $this->set(new static()->with()); }\n}",
want: []collect.Record{{Class: "A", Name: "set", Position: 0, Type: valuetype.Object("A")}},
},
{
name: "local assigned new self resolves call target",
src: "<?php\nclass A {\n function go() { $copy = new self(); $copy->set(1); }\n}",
want: []collect.Record{{Class: "A", Name: "set", Position: 0, Type: "int"}},
},
}

for _, test := range tests {
Expand Down
Loading