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
3 changes: 2 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ The run in `main.go` is a three-stage pipeline over the project's PHP files:
reprint the file, leaving untouched code byte-for-byte.

Skip rules: parameters that already have a type; parameters with conflicting
types (ambiguous); magic methods except `__construct`; methods that may override
types (ambiguous); parameters passed into a type check like `is_array($param)`
or `gettype($param)` in the body, as they accept more than one type; magic methods except `__construct`; methods that may override
a parent or interface (including vendor), unless private or a constructor.

## Packages (`internal/`)
Expand Down
51 changes: 50 additions & 1 deletion internal/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ func (a *applier) applyParams(node ast.Vertex, params []ast.Vertex, lookup func(
added := map[string]string{}
docs := map[string]string{}
var order []string
checked := typeCheckedVariables(node)
for position, paramNode := range params {
param, ok := paramNode.(*ast.Parameter)
if !ok || !typeable(param) {
if !ok || !typeable(param) || checked[phpast.VariableName(param.Var)] {
continue
}
if resolved, ok := lookup(position); ok {
Expand All @@ -156,6 +157,54 @@ func (a *applier) applyParams(node ast.Vertex, params []ast.Vertex, lookup func(
phpast.AddDocParams(node, docs, order)
}

// typeCheckFunctions inspect the type of a value at runtime. A parameter passed
// into one of them is expected to take more than one type, even when every
// resolved call site passes the same one.
var typeCheckFunctions = map[string]bool{
"is_array": true,
"is_bool": true,
"is_callable": true,
"is_countable": true,
"is_double": true,
"is_float": true,
"is_int": true,
"is_integer": true,
"is_iterable": true,
"is_long": true,
"is_numeric": true,
"is_object": true,
"is_scalar": true,
"is_string": true,
"gettype": true,
"get_debug_type": true,
}

// typeCheckedVariables returns the names of variables passed directly into a
// type check function anywhere in the node, e.g. `is_array($sendTo)`.
func typeCheckedVariables(node ast.Vertex) map[string]bool {
checked := map[string]bool{}
var walk func(node ast.Vertex)
walk = func(node ast.Vertex) {
if node == nil {
return
}
if call, ok := node.(*ast.ExprFunctionCall); ok && typeCheckFunctions[strings.ToLower(phpast.ShortName(call.Function))] {
for _, argNode := range call.Args {
if arg, ok := argNode.(*ast.Argument); ok {
if name := phpast.VariableName(arg.Expr); name != "" {
checked[name] = true
}
}
}
}
for _, child := range phpast.Children(node) {
walk(child)
}
}
walk(node)
return checked
}

// defaultType infers a parameter type from its literal default value, so
// `$page = 1` becomes `int $page = 1` even with no call site. A `null` default
// carries no type of its own; enclosingFQCN resolves `self::` constants and
Expand Down
38 changes: 38 additions & 0 deletions internal/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,44 @@ func TestApply(t *testing.T) {
},
want: "<?php\nfunction run($cb) {}",
},
{
name: "leaves a param checked with is_array untyped",
target: "<?php\nfinal class SmsModel {\n public function send($sendTo) { $sendTo = is_array($sendTo) ? $sendTo : [$sendTo]; }\n}",
callers: []string{
"<?php\nfinal class Caller {\n public function __construct(private SmsModel $model) {}\n public function go(Lead $lead) { $this->model->send($lead); }\n}",
"<?php\n$model = getModel();\n$model->send([$lead1, $lead2]);",
},
want: "<?php\nfinal class SmsModel {\n public function send($sendTo) { $sendTo = is_array($sendTo) ? $sendTo : [$sendTo]; }\n}",
},
{
name: "leaves a param checked with is_string untyped in a function",
target: "<?php\nfunction run($value) { if (is_string($value)) { return; } }",
callers: []string{
"<?php\nrun(5);",
},
want: "<?php\nfunction run($value) { if (is_string($value)) { return; } }",
},
{
name: "leaves a param with a literal default checked with gettype untyped",
target: "<?php\nfunction run($value = 1) { return gettype($value); }",
want: "<?php\nfunction run($value = 1) { return gettype($value); }",
},
{
name: "types a param when another variable is type checked",
target: "<?php\nfunction run($value) { if (is_array($other)) { return; } }",
callers: []string{
"<?php\nrun(5);",
},
want: "<?php\nfunction run(int $value) { if (is_array($other)) { return; } }",
},
{
name: "types a param checked with is_null",
target: "<?php\nfunction run($value) { if (is_null($value)) { return; } }",
callers: []string{
"<?php\nrun(5);",
},
want: "<?php\nfunction run(int $value) { if (is_null($value)) { return; } }",
},
{
name: "leaves an arrow function argument untyped",
target: "<?php\nfunction run($cb) {}",
Expand Down
Loading