Skip to content

Commit 5fd9e4f

Browse files
committed
fix(php): drop the name guess when the receiver's class is not indexed
When the PHP LSP knows a receiver's class but the class is not in the index, it emits php_method_typed_unindexed at 0.55, below the LSP confidence floor. The resolver skipped that row and the weak short-name fallback bound the call to a same-named project method: `$file->getUid()` on a vendor File became a CALLS edge to App\Item::getUid. DeusData#2520 turns this row into php_external_receiver when a vendor `use` proves the class external, but two cases still reach the fallback. A global function import (`use function sprintf;`) leaves the file's import bindings unproven, since DeusData#2705 proves only class imports absent. And a vendor type that arrives through a return type (`$repository->findFile()->getUid()`) has no `use` in the calling file at all. Both resolvers now drop a unique_name or suffix_match edge when the LSP row names a receiver class that no project class-like symbol shares by simple name. A project class with that name keeps the fallback, because the PHP LSP does not follow every cross-file type. The test pair covers both cases, through pass_calls.c and pass_parallel.c. Each case is RED on main on its own (vendorCall, chainedVendorCall); calls on a project-typed receiver and on $this still resolve. On a 211-file TYPO3 project PHP CALLS edges go from 1083 to 1059 (-18 suffix_match, -6 unique_name; typed php_* edges unchanged): 25 removed, and 1 that a removed edge on the same caller had absorbed. Signed-off-by: Wolfgang Klinger <wolfgang@wazum.com>
1 parent d4980ad commit 5fd9e4f

6 files changed

Lines changed: 221 additions & 2 deletions

File tree

‎src/pipeline/lsp_resolve.h‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,43 @@ static inline bool cbm_pipeline_invocation_leaf_matches(const CBMResolvedCall *r
414414
call_leaf && strcmp(cbm_lsp_bare_segment(resolved->reason), call_leaf) == 0;
415415
}
416416

417+
/* #606: a sub-floor `php_method_typed_unindexed` row still proves the receiver
418+
* is not a project class, so a weak short-name fallback must not bind it. A
419+
* project class with the same simple name keeps the fallback, since the PHP
420+
* LSP does not follow every cross-file type. */
421+
static inline bool cbm_pipeline_php_receiver_type_unindexed(const CBMResolvedCallArray *arr,
422+
const CBMCall *call,
423+
const cbm_registry_t *registry) {
424+
if (!arr || !call || !call->enclosing_func_qn || !call->callee_name) {
425+
return false;
426+
}
427+
for (int i = 0; i < arr->count; i++) {
428+
const CBMResolvedCall *rc = &arr->items[i];
429+
if (rc->kind != CBM_RESOLVED_INVOCATION || !rc->caller_qn || !rc->callee_qn ||
430+
!rc->strategy || strcmp(rc->strategy, "php_method_typed_unindexed") != 0 ||
431+
strcmp(rc->caller_qn, call->enclosing_func_qn) != 0) {
432+
continue;
433+
}
434+
int site_rank = cbm_pipeline_invocation_site_rank(rc, call);
435+
if (site_rank == 0 || !cbm_pipeline_invocation_leaf_matches(rc, call, site_rank)) {
436+
continue;
437+
}
438+
const char *method_dot = strrchr(rc->callee_qn, '.');
439+
if (!method_dot || method_dot == rc->callee_qn) {
440+
continue;
441+
}
442+
char class_qn[CBM_SZ_512];
443+
size_t class_len = (size_t)(method_dot - rc->callee_qn);
444+
if (class_len >= sizeof(class_qn)) {
445+
continue;
446+
}
447+
memcpy(class_qn, rc->callee_qn, class_len);
448+
class_qn[class_len] = '\0';
449+
return !cbm_registry_has_type_named(registry, cbm_lsp_bare_segment(class_qn));
450+
}
451+
return false;
452+
}
453+
417454
/* Look up the highest-confidence LSP-resolved call entry whose caller QN
418455
* matches the textual call's enclosing function and whose callee QN
419456
* short-name matches the textual callee. Returns a pointer into `arr`

‎src/pipeline/pass_calls.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,10 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
11501150
* paths are module chains) — MUST match pass_parallel.c exactly. */
11511151
(lang == CBM_LANG_PYTHON && cbm_suppress_weak_import_bound_call(true, true, res.strategy) &&
11521152
cbm_python_import_binding_contradicts(imports, call->callee_name, res.qualified_name,
1153-
ctx->gbuf, ctx->project_name, rel));
1153+
ctx->gbuf, ctx->project_name, rel)) ||
1154+
/* MUST match pass_parallel.c exactly. */
1155+
(lang == CBM_LANG_PHP && cbm_suppress_weak_member_match(true, true, res.strategy) &&
1156+
cbm_pipeline_php_receiver_type_unindexed(lsp_calls, call, ctx->registry));
11541157

11551158
/* Service-pattern HTTP/ASYNC calls to an EXTERNAL client library (e.g.
11561159
* `requests.get("/api/orders/{id}")`) resolve to a QN containing the library

‎src/pipeline/pass_parallel.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3409,7 +3409,10 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
34093409
cbm_suppress_weak_import_bound_call(true, true, res.strategy) &&
34103410
cbm_python_import_binding_contradicts(&result->imports, call->callee_name,
34113411
res.qualified_name, rc->main_gbuf,
3412-
rc->project_name, rel));
3412+
rc->project_name, rel)) ||
3413+
/* MUST match pass_calls.c exactly. */
3414+
(lang == CBM_LANG_PHP && cbm_suppress_weak_member_match(true, true, res.strategy) &&
3415+
cbm_pipeline_php_receiver_type_unindexed(&result->resolved_calls, call, rc->registry));
34133416

34143417
/* Service-pattern HTTP/ASYNC client call (`requests.get(url)`): the
34153418
* service signal lives in the callee_name. The registry can mis-resolve

‎src/pipeline/pipeline.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ const char *cbm_registry_value_target(const cbm_registry_t *r, const char *qn);
496496
int cbm_registry_find_by_name(const cbm_registry_t *r, const char *name, const char ***out,
497497
int *count);
498498

499+
bool cbm_registry_has_type_named(const cbm_registry_t *r, const char *name);
500+
499501
/* Return total number of entries. */
500502
int cbm_registry_size(const cbm_registry_t *r);
501503

‎src/pipeline/registry.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,21 @@ const char *cbm_registry_unique_field_qn(const cbm_registry_t *r, const char *me
14671467
return only;
14681468
}
14691469

1470+
bool cbm_registry_has_type_named(const cbm_registry_t *r, const char *name) {
1471+
if (!r || !name || !name[0]) {
1472+
return false;
1473+
}
1474+
const char **qns = NULL;
1475+
int count = 0;
1476+
cbm_registry_find_by_name(r, name, &qns, &count);
1477+
for (int i = 0; i < count; i++) {
1478+
if (cbm_label_is_type_like(cbm_registry_label_of(r, qns[i]))) {
1479+
return true;
1480+
}
1481+
}
1482+
return false;
1483+
}
1484+
14701485
int cbm_registry_size(const cbm_registry_t *r) {
14711486
return r ? (int)cbm_ht_count(r->exact) : 0;
14721487
}

‎tests/test_pipeline.c‎

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8644,6 +8644,163 @@ TEST(pipeline_python_receiver_suppresses_weak_method_edge) {
86448644
PASS();
86458645
}
86468646

8647+
/* #606: a receiver typed as an unindexed (vendor/) class must not weakly bind a
8648+
* same-named project method. */
8649+
static void write_php_receiver_fixture(const char *tmp) {
8650+
write_temp_file(tmp, "src/Item.php",
8651+
"<?php\n"
8652+
"namespace App;\n"
8653+
"\n"
8654+
"class Item\n"
8655+
"{\n"
8656+
" public function getUid(): int\n"
8657+
" {\n"
8658+
" return 1;\n"
8659+
" }\n"
8660+
"}\n");
8661+
write_temp_file(tmp, "src/Domain/Repository.php",
8662+
"<?php\n"
8663+
"namespace App\\Domain;\n"
8664+
"\n"
8665+
"use App\\Item;\n"
8666+
"use Vendor\\Storage\\File;\n"
8667+
"\n"
8668+
"class Repository\n"
8669+
"{\n"
8670+
" public function findItem(): Item\n"
8671+
" {\n"
8672+
" return new Item();\n"
8673+
" }\n"
8674+
"\n"
8675+
" public function findFile(): File\n"
8676+
" {\n"
8677+
" return new File();\n"
8678+
" }\n"
8679+
"}\n");
8680+
write_temp_file(tmp, "src/Listener.php",
8681+
"<?php\n"
8682+
"namespace App;\n"
8683+
"\n"
8684+
"use Vendor\\Storage\\File;\n"
8685+
"use function sprintf;\n"
8686+
"\n"
8687+
"class Listener\n"
8688+
"{\n"
8689+
" public function vendorCall(File $file): int\n"
8690+
" {\n"
8691+
" return $file->getUid();\n"
8692+
" }\n"
8693+
"\n"
8694+
" public function selfCall(): int\n"
8695+
" {\n"
8696+
" return $this->localHelper();\n"
8697+
" }\n"
8698+
"\n"
8699+
" private function localHelper(): int\n"
8700+
" {\n"
8701+
" return 1;\n"
8702+
" }\n"
8703+
"}\n");
8704+
write_temp_file(tmp, "src/ChainListener.php",
8705+
"<?php\n"
8706+
"namespace App;\n"
8707+
"\n"
8708+
"use App\\Domain\\Repository;\n"
8709+
"\n"
8710+
"class ChainListener\n"
8711+
"{\n"
8712+
" public function chainedVendorCall(Repository $repository): int\n"
8713+
" {\n"
8714+
" return $repository->findFile()->getUid();\n"
8715+
" }\n"
8716+
"\n"
8717+
" public function projectCall(Repository $repository): Item\n"
8718+
" {\n"
8719+
" return $repository->findItem();\n"
8720+
" }\n"
8721+
"}\n");
8722+
}
8723+
8724+
/* Fewer than 50 files exercises pass_calls.c. */
8725+
TEST(pipeline_php_receiver_suppresses_weak_method_edge) {
8726+
char tmp[256];
8727+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_php_recv_XXXXXX");
8728+
if (!cbm_mkdtemp(tmp)) {
8729+
FAIL("tmpdir");
8730+
}
8731+
write_php_receiver_fixture(tmp);
8732+
8733+
char db_path[512];
8734+
snprintf(db_path, sizeof(db_path), "%s/php_recv.db", tmp);
8735+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
8736+
ASSERT_NOT_NULL(p);
8737+
ASSERT_EQ(cbm_pipeline_run(p), 0);
8738+
const char *project = cbm_pipeline_project_name(p);
8739+
8740+
cbm_store_t *s = cbm_store_open_path(db_path);
8741+
ASSERT_NOT_NULL(s);
8742+
8743+
ASSERT_GTE(fixture_node_count(s, project, "src/Item.php", "getUid", "Method"), 1);
8744+
ASSERT_FALSE(cross_file_call_exists(s, project, "vendorCall", "getUid"));
8745+
ASSERT_FALSE(cross_file_call_exists(s, project, "chainedVendorCall", "getUid"));
8746+
ASSERT_TRUE(cross_file_call_exists(s, project, "projectCall", "findItem"));
8747+
ASSERT_GTE(named_edge_count(s, project, "CALLS", "selfCall", "localHelper"), 1);
8748+
8749+
cbm_store_close(s);
8750+
cbm_pipeline_free(p);
8751+
th_rmtree(tmp);
8752+
PASS();
8753+
}
8754+
8755+
/* >= 50 files forces pass_parallel.c. */
8756+
TEST(pipeline_php_receiver_parallel_suppresses_weak_method_edge) {
8757+
char tmp[256];
8758+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_php_par_XXXXXX");
8759+
if (!cbm_mkdtemp(tmp)) {
8760+
FAIL("tmpdir");
8761+
}
8762+
write_php_receiver_fixture(tmp);
8763+
for (int i = 0; i < 52; i++) {
8764+
char name[64];
8765+
char body[128];
8766+
snprintf(name, sizeof(name), "src/Filler%d.php", i);
8767+
snprintf(body, sizeof(body), "<?php\nfunction filler%d(): int\n{\n return %d;\n}\n", i,
8768+
i);
8769+
write_temp_file(tmp, name, body);
8770+
}
8771+
8772+
char *old_workers = getenv("CBM_WORKERS");
8773+
char *saved = old_workers ? strdup(old_workers) : NULL;
8774+
cbm_setenv("CBM_WORKERS", "4", 1);
8775+
8776+
char db_path[512];
8777+
snprintf(db_path, sizeof(db_path), "%s/php_par.db", tmp);
8778+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
8779+
ASSERT_NOT_NULL(p);
8780+
ASSERT_EQ(cbm_pipeline_run(p), 0);
8781+
const char *project = cbm_pipeline_project_name(p);
8782+
8783+
cbm_store_t *s = cbm_store_open_path(db_path);
8784+
ASSERT_NOT_NULL(s);
8785+
8786+
ASSERT_GTE(fixture_node_count(s, project, "src/Item.php", "getUid", "Method"), 1);
8787+
ASSERT_FALSE(cross_file_call_exists(s, project, "vendorCall", "getUid"));
8788+
ASSERT_FALSE(cross_file_call_exists(s, project, "chainedVendorCall", "getUid"));
8789+
ASSERT_TRUE(cross_file_call_exists(s, project, "projectCall", "findItem"));
8790+
ASSERT_GTE(named_edge_count(s, project, "CALLS", "selfCall", "localHelper"), 1);
8791+
8792+
cbm_store_close(s);
8793+
cbm_pipeline_free(p);
8794+
if (saved) {
8795+
cbm_setenv("CBM_WORKERS", saved, 1);
8796+
free(saved);
8797+
} else {
8798+
cbm_unsetenv("CBM_WORKERS");
8799+
}
8800+
th_rmtree(tmp);
8801+
PASS();
8802+
}
8803+
86478804
/* The member guard's one exemption (2026-09-16, v0.10.8 → v0.11.0 A/B on
86488805
* django/django): a member call whose receiver is an attribute chain rooted at
86498806
* self/cls, whose callee has exactly ONE definition in the project and is not a
@@ -26177,6 +26334,8 @@ SUITE(pipeline) {
2617726334
RUN_TEST(pipeline_ts_crossfile_new_instance_method_call_issue1354);
2617826335
RUN_TEST(pipeline_ts_param_property_injection_issue514);
2617926336
RUN_TEST(pipeline_python_receiver_suppresses_weak_method_edge);
26337+
RUN_TEST(pipeline_php_receiver_suppresses_weak_method_edge);
26338+
RUN_TEST(pipeline_php_receiver_parallel_suppresses_weak_method_edge);
2618026339
RUN_TEST(pipeline_python_receiver_keeps_specific_unique_name_member_call);
2618126340
RUN_TEST(pipeline_html_embedded_member_call_stays_unbound);
2618226341
RUN_TEST(pipeline_go_multi_return_receiver_is_typed);

0 commit comments

Comments
 (0)