Skip to content

Commit 9a2d1d6

Browse files
authored
Merge pull request DeusData#2710 from wazum/fix/606-php-unindexed-receiver
fix(php): drop the name guess when the receiver's class is not indexed
2 parents 3fa7548 + f460813 commit 9a2d1d6

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
@@ -1158,7 +1158,10 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
11581158
* paths are module chains) — MUST match pass_parallel.c exactly. */
11591159
(lang == CBM_LANG_PYTHON && cbm_suppress_weak_import_bound_call(true, true, res.strategy) &&
11601160
cbm_python_import_binding_contradicts(imports, call->callee_name, res.qualified_name,
1161-
ctx->gbuf, ctx->project_name, rel));
1161+
ctx->gbuf, ctx->project_name, rel)) ||
1162+
/* MUST match pass_parallel.c exactly. */
1163+
(lang == CBM_LANG_PHP && cbm_suppress_weak_member_match(true, true, res.strategy) &&
1164+
cbm_pipeline_php_receiver_type_unindexed(lsp_calls, call, ctx->registry));
11621165

11631166
/* Service-pattern HTTP/ASYNC calls to an EXTERNAL client library (e.g.
11641167
* `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
@@ -3421,7 +3421,10 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
34213421
cbm_suppress_weak_import_bound_call(true, true, res.strategy) &&
34223422
cbm_python_import_binding_contradicts(&result->imports, call->callee_name,
34233423
res.qualified_name, rc->main_gbuf,
3424-
rc->project_name, rel));
3424+
rc->project_name, rel)) ||
3425+
/* MUST match pass_calls.c exactly. */
3426+
(lang == CBM_LANG_PHP && cbm_suppress_weak_member_match(true, true, res.strategy) &&
3427+
cbm_pipeline_php_receiver_type_unindexed(&result->resolved_calls, call, rc->registry));
34253428

34263429
/* Service-pattern HTTP/ASYNC client call (`requests.get(url)`): the
34273430
* 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
@@ -505,6 +505,8 @@ const char *cbm_registry_value_target(const cbm_registry_t *r, const char *qn);
505505
int cbm_registry_find_by_name(const cbm_registry_t *r, const char *name, const char ***out,
506506
int *count);
507507

508+
bool cbm_registry_has_type_named(const cbm_registry_t *r, const char *name);
509+
508510
/* Return total number of entries. */
509511
int cbm_registry_size(const cbm_registry_t *r);
510512

‎src/pipeline/registry.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,21 @@ const char *cbm_registry_unique_field_qn(const cbm_registry_t *r, const char *me
14961496
return only;
14971497
}
14981498

1499+
bool cbm_registry_has_type_named(const cbm_registry_t *r, const char *name) {
1500+
if (!r || !name || !name[0]) {
1501+
return false;
1502+
}
1503+
const char **qns = NULL;
1504+
int count = 0;
1505+
cbm_registry_find_by_name(r, name, &qns, &count);
1506+
for (int i = 0; i < count; i++) {
1507+
if (cbm_label_is_type_like(cbm_registry_label_of(r, qns[i]))) {
1508+
return true;
1509+
}
1510+
}
1511+
return false;
1512+
}
1513+
14991514
int cbm_registry_size(const cbm_registry_t *r) {
15001515
return r ? (int)cbm_ht_count(r->exact) : 0;
15011516
}

‎tests/test_pipeline.c‎

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9659,6 +9659,163 @@ TEST(pipeline_python_receiver_suppresses_weak_method_edge) {
96599659
PASS();
96609660
}
96619661

9662+
/* #606: a receiver typed as an unindexed (vendor/) class must not weakly bind a
9663+
* same-named project method. */
9664+
static void write_php_receiver_fixture(const char *tmp) {
9665+
write_temp_file(tmp, "src/Item.php",
9666+
"<?php\n"
9667+
"namespace App;\n"
9668+
"\n"
9669+
"class Item\n"
9670+
"{\n"
9671+
" public function getUid(): int\n"
9672+
" {\n"
9673+
" return 1;\n"
9674+
" }\n"
9675+
"}\n");
9676+
write_temp_file(tmp, "src/Domain/Repository.php",
9677+
"<?php\n"
9678+
"namespace App\\Domain;\n"
9679+
"\n"
9680+
"use App\\Item;\n"
9681+
"use Vendor\\Storage\\File;\n"
9682+
"\n"
9683+
"class Repository\n"
9684+
"{\n"
9685+
" public function findItem(): Item\n"
9686+
" {\n"
9687+
" return new Item();\n"
9688+
" }\n"
9689+
"\n"
9690+
" public function findFile(): File\n"
9691+
" {\n"
9692+
" return new File();\n"
9693+
" }\n"
9694+
"}\n");
9695+
write_temp_file(tmp, "src/Listener.php",
9696+
"<?php\n"
9697+
"namespace App;\n"
9698+
"\n"
9699+
"use Vendor\\Storage\\File;\n"
9700+
"use function sprintf;\n"
9701+
"\n"
9702+
"class Listener\n"
9703+
"{\n"
9704+
" public function vendorCall(File $file): int\n"
9705+
" {\n"
9706+
" return $file->getUid();\n"
9707+
" }\n"
9708+
"\n"
9709+
" public function selfCall(): int\n"
9710+
" {\n"
9711+
" return $this->localHelper();\n"
9712+
" }\n"
9713+
"\n"
9714+
" private function localHelper(): int\n"
9715+
" {\n"
9716+
" return 1;\n"
9717+
" }\n"
9718+
"}\n");
9719+
write_temp_file(tmp, "src/ChainListener.php",
9720+
"<?php\n"
9721+
"namespace App;\n"
9722+
"\n"
9723+
"use App\\Domain\\Repository;\n"
9724+
"\n"
9725+
"class ChainListener\n"
9726+
"{\n"
9727+
" public function chainedVendorCall(Repository $repository): int\n"
9728+
" {\n"
9729+
" return $repository->findFile()->getUid();\n"
9730+
" }\n"
9731+
"\n"
9732+
" public function projectCall(Repository $repository): Item\n"
9733+
" {\n"
9734+
" return $repository->findItem();\n"
9735+
" }\n"
9736+
"}\n");
9737+
}
9738+
9739+
/* Fewer than 50 files exercises pass_calls.c. */
9740+
TEST(pipeline_php_receiver_suppresses_weak_method_edge) {
9741+
char tmp[256];
9742+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_php_recv_XXXXXX");
9743+
if (!cbm_mkdtemp(tmp)) {
9744+
FAIL("tmpdir");
9745+
}
9746+
write_php_receiver_fixture(tmp);
9747+
9748+
char db_path[512];
9749+
snprintf(db_path, sizeof(db_path), "%s/php_recv.db", tmp);
9750+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
9751+
ASSERT_NOT_NULL(p);
9752+
ASSERT_EQ(cbm_pipeline_run(p), 0);
9753+
const char *project = cbm_pipeline_project_name(p);
9754+
9755+
cbm_store_t *s = cbm_store_open_path(db_path);
9756+
ASSERT_NOT_NULL(s);
9757+
9758+
ASSERT_GTE(fixture_node_count(s, project, "src/Item.php", "getUid", "Method"), 1);
9759+
ASSERT_FALSE(cross_file_call_exists(s, project, "vendorCall", "getUid"));
9760+
ASSERT_FALSE(cross_file_call_exists(s, project, "chainedVendorCall", "getUid"));
9761+
ASSERT_TRUE(cross_file_call_exists(s, project, "projectCall", "findItem"));
9762+
ASSERT_GTE(named_edge_count(s, project, "CALLS", "selfCall", "localHelper"), 1);
9763+
9764+
cbm_store_close(s);
9765+
cbm_pipeline_free(p);
9766+
th_rmtree(tmp);
9767+
PASS();
9768+
}
9769+
9770+
/* >= 50 files forces pass_parallel.c. */
9771+
TEST(pipeline_php_receiver_parallel_suppresses_weak_method_edge) {
9772+
char tmp[256];
9773+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_php_par_XXXXXX");
9774+
if (!cbm_mkdtemp(tmp)) {
9775+
FAIL("tmpdir");
9776+
}
9777+
write_php_receiver_fixture(tmp);
9778+
for (int i = 0; i < 52; i++) {
9779+
char name[64];
9780+
char body[128];
9781+
snprintf(name, sizeof(name), "src/Filler%d.php", i);
9782+
snprintf(body, sizeof(body), "<?php\nfunction filler%d(): int\n{\n return %d;\n}\n", i,
9783+
i);
9784+
write_temp_file(tmp, name, body);
9785+
}
9786+
9787+
char *old_workers = getenv("CBM_WORKERS");
9788+
char *saved = old_workers ? strdup(old_workers) : NULL;
9789+
cbm_setenv("CBM_WORKERS", "4", 1);
9790+
9791+
char db_path[512];
9792+
snprintf(db_path, sizeof(db_path), "%s/php_par.db", tmp);
9793+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
9794+
ASSERT_NOT_NULL(p);
9795+
ASSERT_EQ(cbm_pipeline_run(p), 0);
9796+
const char *project = cbm_pipeline_project_name(p);
9797+
9798+
cbm_store_t *s = cbm_store_open_path(db_path);
9799+
ASSERT_NOT_NULL(s);
9800+
9801+
ASSERT_GTE(fixture_node_count(s, project, "src/Item.php", "getUid", "Method"), 1);
9802+
ASSERT_FALSE(cross_file_call_exists(s, project, "vendorCall", "getUid"));
9803+
ASSERT_FALSE(cross_file_call_exists(s, project, "chainedVendorCall", "getUid"));
9804+
ASSERT_TRUE(cross_file_call_exists(s, project, "projectCall", "findItem"));
9805+
ASSERT_GTE(named_edge_count(s, project, "CALLS", "selfCall", "localHelper"), 1);
9806+
9807+
cbm_store_close(s);
9808+
cbm_pipeline_free(p);
9809+
if (saved) {
9810+
cbm_setenv("CBM_WORKERS", saved, 1);
9811+
free(saved);
9812+
} else {
9813+
cbm_unsetenv("CBM_WORKERS");
9814+
}
9815+
th_rmtree(tmp);
9816+
PASS();
9817+
}
9818+
96629819
/* The member guard's one exemption (2026-09-16, v0.10.8 → v0.11.0 A/B on
96639820
* django/django): a member call whose receiver is an attribute chain rooted at
96649821
* self/cls, whose callee has exactly ONE definition in the project and is not a
@@ -27522,6 +27679,8 @@ SUITE(pipeline) {
2752227679
RUN_TEST(pipeline_ts_crossfile_new_instance_method_call_issue1354);
2752327680
RUN_TEST(pipeline_ts_param_property_injection_issue514);
2752427681
RUN_TEST(pipeline_python_receiver_suppresses_weak_method_edge);
27682+
RUN_TEST(pipeline_php_receiver_suppresses_weak_method_edge);
27683+
RUN_TEST(pipeline_php_receiver_parallel_suppresses_weak_method_edge);
2752527684
RUN_TEST(pipeline_python_receiver_keeps_specific_unique_name_member_call);
2752627685
RUN_TEST(pipeline_html_embedded_member_call_stays_unbound);
2752727686
RUN_TEST(pipeline_go_method_caller_keeps_lsp_join);

0 commit comments

Comments
 (0)