| 1640 | /* ── call resolution ────────────────────────────────────────────── */ |
| 1641 | |
| 1642 | static void resolve_function_call(PHPLSPContext *ctx, TSNode call, CBMResolvedKind kind) { |
| 1643 | TSNode fn = ts_node_child_by_field_name(call, "function", 8); |
| 1644 | if (ts_node_is_null(fn)) |
| 1645 | return; |
| 1646 | char *name = php_node_text(ctx, fn); |
| 1647 | if (!name) |
| 1648 | return; |
| 1649 | |
| 1650 | /* In PHP, `$handler(...)` is syntactically a function-call expression but |
| 1651 | * a typed object value dispatches to its materialized `__invoke` method. |
| 1652 | * This is precise only for a first-class callable and a receiver type whose |
| 1653 | * exact method exists in the registry; otherwise leave it unresolved so |
| 1654 | * the usage pipeline can retain ordinary value semantics. */ |
| 1655 | if (kind == CBM_RESOLVED_CALL_REFERENCE && strcmp(ts_node_type(fn), "variable_name") == 0) { |
| 1656 | const CBMType *receiver = php_eval_expr_type(ctx, fn); |
| 1657 | const char *class_qn = NULL; |
| 1658 | if (receiver && receiver->kind == CBM_TYPE_NAMED) { |
| 1659 | class_qn = receiver->data.named.qualified_name; |
| 1660 | } else if (receiver && receiver->kind == CBM_TYPE_TEMPLATE) { |
| 1661 | class_qn = receiver->data.template_type.template_name; |
| 1662 | } |
| 1663 | const CBMRegisteredFunc *invoke = |
| 1664 | class_qn ? php_lookup_method(ctx, class_qn, "__invoke") : NULL; |
| 1665 | if (invoke && invoke->qualified_name) { |
| 1666 | emit_resolved_reason_kind(ctx, invoke->qualified_name, "php_invokable_reference", 0.95f, |
| 1667 | name, kind); |
| 1668 | } |
| 1669 | return; |
| 1670 | } |
| 1671 | |
| 1672 | /* `use function Foo\\bar;` mapping. */ |
| 1673 | const char *target = find_use(ctx, name, CBM_PHP_USE_FUNCTION); |
| 1674 | if (target) { |
| 1675 | const CBMRegisteredFunc *f = cbm_registry_lookup_func(ctx->registry, target); |
| 1676 | if (f) { |
| 1677 | emit_resolved_kind(ctx, f->qualified_name, "php_function_namespaced", 0.95f, kind); |
| 1678 | return; |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | /* Current namespace, then global fallback. */ |
| 1683 | if (ctx->current_namespace_qn && ctx->current_namespace_qn[0]) { |
| 1684 | const char *ns_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->current_namespace_qn, name); |
| 1685 | const CBMRegisteredFunc *f = cbm_registry_lookup_func(ctx->registry, ns_qn); |
| 1686 | if (f) { |
| 1687 | emit_resolved_kind(ctx, f->qualified_name, "php_function_namespaced", 0.95f, kind); |
| 1688 | return; |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | /* Look for any free function with this short_name. Prefer the one in the |
| 1693 | * file's project (i.e. with the longest module-prefix overlap). */ |
| 1694 | const CBMRegisteredFunc *best = NULL; |
| 1695 | int best_score = -1; |
| 1696 | for (int i = 0; ctx->registry && i < ctx->registry->func_count; i++) { |
| 1697 | const CBMRegisteredFunc *cand = &ctx->registry->funcs[i]; |
| 1698 | if (cand->receiver_type) |
| 1699 | continue; |
no test coverage detected