@@ -5148,6 +5148,173 @@ TEST(parallel_php_vendor_types_never_bind_by_name_issue1186) {
51485148 PASS ();
51495149}
51505150
5151+ /* ── PHP LSP types receivers of imported project classes (#1186) ─── */
5152+
5153+ /* Foo.php calls methods on receivers typed with classes imported from OTHER
5154+ * project files: a promoted property (`$this->svc->go()`) and an aliased
5155+ * import (`R $r` -> `$r->find()`). The import map handed to the PHP LSP held
5156+ * the IMPORTS edge target -- the imported file's File node QN
5157+ * (`...Svc.php.__file__`) -- as the class, so no receiver ever typed and the
5158+ * calls fell to name-only strategies (the #1186 "useful calls at 0.38"). The
5159+ * decoy App\Other\Svc shares the short name and the method, so a name-only
5160+ * match cannot tell the two apart. Controls: the vendor-typed receiver
5161+ * (`Client $c`) must stay unresolved, and the same-file calls keep their LSP
5162+ * strategies. */
5163+ static const struct {
5164+ const char * rel ;
5165+ const char * src ;
5166+ } k_php_xfile_files [] = {
5167+ {"lib/Services/Svc.php" , "<?php\nnamespace App\\Services;\n\nclass Svc {\n"
5168+ " public function go() { return 1; }\n}\n" },
5169+ {"lib/Other/Svc.php" , "<?php\nnamespace App\\Other;\n\nclass Svc {\n"
5170+ " public function go() { return 2; }\n}\n" },
5171+ {"lib/Repo/Repo.php" , "<?php\nnamespace App\\Repo;\n\nclass Repo {\n"
5172+ " public function find() { return 3; }\n}\n" },
5173+ {"lib/Http/Foo.php" ,
5174+ "<?php\nnamespace App\\Http;\n\n"
5175+ "use App\\Services\\Svc;\nuse App\\Repo\\Repo as R;\nuse Vendor\\Pkg\\Client;\n\n"
5176+ "class Foo {\n"
5177+ " public function __construct(private Svc $svc) {}\n"
5178+ " public function run(R $r, Client $c) {\n"
5179+ " $this->svc->go();\n"
5180+ " $r->find();\n"
5181+ " $c->go();\n"
5182+ " $this->local();\n"
5183+ " (new Local())->ping();\n"
5184+ " }\n"
5185+ " private function local() { return 4; }\n}\n\n"
5186+ "class Local {\n"
5187+ " public function ping() { return 5; }\n}\n" },
5188+ };
5189+ enum { PHP_XFILE_FILE_COUNT = (int )(sizeof (k_php_xfile_files ) / sizeof (k_php_xfile_files [0 ])) };
5190+
5191+ typedef struct {
5192+ const cbm_gbuf_t * gbuf ;
5193+ const char * src_rel ;
5194+ const char * tgt_rel ;
5195+ const char * tgt_suffix ;
5196+ const char * strategy_prefix ;
5197+ int total ;
5198+ int with_strategy ;
5199+ } php_xfile_edge_count_t ;
5200+
5201+ static void php_xfile_count_edge (const cbm_gbuf_edge_t * edge , void * ud ) {
5202+ php_xfile_edge_count_t * c = ud ;
5203+ if (!edge -> type || strcmp (edge -> type , "CALLS" ) != 0 ) {
5204+ return ;
5205+ }
5206+ const cbm_gbuf_node_t * s = cbm_gbuf_find_by_id (c -> gbuf , edge -> source_id );
5207+ const cbm_gbuf_node_t * t = cbm_gbuf_find_by_id (c -> gbuf , edge -> target_id );
5208+ if (!s || !t || !s -> file_path || !t -> file_path || !t -> qualified_name ||
5209+ strcmp (s -> file_path , c -> src_rel ) != 0 || strcmp (t -> file_path , c -> tgt_rel ) != 0 ) {
5210+ return ;
5211+ }
5212+ size_t qlen = strlen (t -> qualified_name );
5213+ size_t slen = strlen (c -> tgt_suffix );
5214+ if (qlen < slen || strcmp (t -> qualified_name + qlen - slen , c -> tgt_suffix ) != 0 ) {
5215+ return ;
5216+ }
5217+ c -> total ++ ;
5218+ char needle [128 ];
5219+ snprintf (needle , sizeof (needle ), "\"strategy\":\"%s" , c -> strategy_prefix );
5220+ if (edge -> properties_json && strstr (edge -> properties_json , needle )) {
5221+ c -> with_strategy ++ ;
5222+ }
5223+ }
5224+
5225+ /* 1 when every cross-file receiver resolved through the PHP LSP, the decoy and
5226+ * the vendor receiver drew nothing, and the same-file controls held. */
5227+ static int php_xfile_graph_ok (const cbm_gbuf_t * gbuf , const char * driver ) {
5228+ static const char * const foo = "lib/Http/Foo.php" ;
5229+ struct {
5230+ const char * tgt ;
5231+ const char * suffix ;
5232+ int want_total ;
5233+ int want_lsp ;
5234+ } checks [] = {
5235+ {"lib/Services/Svc.php" , ".Svc.go" , 1 , 1 }, /* $this->svc->go(), not $c->go() */
5236+ {"lib/Other/Svc.php" , ".Svc.go" , 0 , 0 }, /* decoy: same short name + method */
5237+ {"lib/Repo/Repo.php" , ".Repo.find" , 1 , 1 }, /* aliased import */
5238+ {foo , ".Foo.local" , 1 , 1 }, /* same-file control */
5239+ {foo , ".Local.ping" , 1 , 1 }, /* same-file control */
5240+ };
5241+ int ok = 1 ;
5242+ for (size_t i = 0 ; i < sizeof (checks ) / sizeof (checks [0 ]); i ++ ) {
5243+ php_xfile_edge_count_t c = {gbuf , foo , checks [i ].tgt , checks [i ].suffix , "php_" , 0 , 0 };
5244+ cbm_gbuf_foreach_edge (gbuf , php_xfile_count_edge , & c );
5245+ if (c .total != checks [i ].want_total || c .with_strategy != checks [i ].want_lsp ) {
5246+ printf (" [%s] CALLS Foo.php -> %s%s: %d edge(s), %d via php_*; want %d, %d\n" , driver ,
5247+ checks [i ].tgt , checks [i ].suffix , c .total , c .with_strategy , checks [i ].want_total ,
5248+ checks [i ].want_lsp );
5249+ ok = 0 ;
5250+ }
5251+ }
5252+ return ok ;
5253+ }
5254+
5255+ /* Run the fixture on both venues. extra_use (nullable) is one more `use` line
5256+ * added to Foo.php. Returns 1 when both graphs pass, 0 when not, -1 on setup
5257+ * failure. */
5258+ static int php_xfile_run (const char * extra_use ) {
5259+ char tmpdir [256 ];
5260+ snprintf (tmpdir , sizeof (tmpdir ), "/tmp/cbm_par_phpxfile_XXXXXX" );
5261+ if (!cbm_mkdtemp (tmpdir )) {
5262+ return -1 ;
5263+ }
5264+ char paths [PHP_XFILE_FILE_COUNT ][512 ];
5265+ cbm_file_info_t files [PHP_XFILE_FILE_COUNT ];
5266+ memset (files , 0 , sizeof (files ));
5267+ static const char foo_use [] = "use Vendor\\Pkg\\Client;\n" ;
5268+ for (int i = 0 ; i < PHP_XFILE_FILE_COUNT ; i ++ ) {
5269+ snprintf (paths [i ], sizeof (paths [i ]), "%s/%s" , tmpdir , k_php_xfile_files [i ].rel );
5270+ char dir [512 ];
5271+ snprintf (dir , sizeof (dir ), "%s" , paths [i ]);
5272+ * strrchr (dir , '/' ) = '\0' ;
5273+ cbm_mkdir_p (dir , 0755 );
5274+ char src [2048 ];
5275+ const char * s = k_php_xfile_files [i ].src ;
5276+ const char * at = extra_use ? strstr (s , foo_use ) : NULL ;
5277+ if (at ) {
5278+ size_t head = (size_t )(at - s ) + sizeof (foo_use ) - 1 ;
5279+ snprintf (src , sizeof (src ), "%.*s%s%s" , (int )head , s , extra_use , s + head );
5280+ s = src ;
5281+ }
5282+ if (th_write_file (paths [i ], s ) != 0 ) {
5283+ th_rmtree (tmpdir );
5284+ return -1 ;
5285+ }
5286+ files [i ].path = paths [i ];
5287+ files [i ].rel_path = (char * )k_php_xfile_files [i ].rel ;
5288+ files [i ].language = CBM_LANG_PHP ;
5289+ }
5290+
5291+ cbm_gbuf_t * seq = run_sequential_with_lsp_cross_and_mutator (
5292+ "cbm_par_phpxfile" , tmpdir , files , PHP_XFILE_FILE_COUNT , NULL , NULL , true);
5293+ cbm_gbuf_t * par = run_parallel_with_extract_opts_and_mutator (
5294+ "cbm_par_phpxfile" , tmpdir , files , PHP_XFILE_FILE_COUNT , 2 , NULL , NULL , NULL , true);
5295+ int seq_ok = seq ? php_xfile_graph_ok (seq , "sequential" ) : 0 ;
5296+ int par_ok = par ? php_xfile_graph_ok (par , "parallel" ) : 0 ;
5297+ cbm_gbuf_free (seq );
5298+ cbm_gbuf_free (par );
5299+ th_rmtree (tmpdir );
5300+ return seq_ok && par_ok ;
5301+ }
5302+
5303+ TEST (parallel_php_lsp_types_imported_project_receivers_issue1186 ) {
5304+ ASSERT_EQ (php_xfile_run (NULL ), 1 );
5305+ PASS ();
5306+ }
5307+
5308+ /* The same file with one more import that no file in the project declares,
5309+ * the global class `use Exception;` written in most namespaced PHP files.
5310+ * The typed import evidence leaves such a file "unknown" as a whole (#2520),
5311+ * so the class-valued import mapping must not depend on that evidence. */
5312+ TEST (parallel_php_lsp_types_imported_project_receivers_unknown_bindings_issue1186 ) {
5313+ ASSERT_EQ (php_xfile_run ("use Exception;\n" ), 1 );
5314+ PASS ();
5315+ }
5316+
5317+
51515318/* A fresh PHP external row replaces stale evidence for its exact occurrence,
51525319 * including an older external row, without clearing a real project invocation
51535320 * of the same method at the next occurrence. Test both old-row orders. */
@@ -5261,6 +5428,8 @@ TEST(parallel_php_external_row_replaces_only_its_exact_occurrence) {
52615428SUITE (parallel ) {
52625429 RUN_TEST (parallel_php_external_row_replaces_only_its_exact_occurrence );
52635430 RUN_TEST (parallel_php_vendor_types_never_bind_by_name_issue1186 );
5431+ RUN_TEST (parallel_php_lsp_types_imported_project_receivers_issue1186 );
5432+ RUN_TEST (parallel_php_lsp_types_imported_project_receivers_unknown_bindings_issue1186 );
52645433 RUN_TEST (usage_semantic_reference_candidate_trusts_marked_producer );
52655434 RUN_TEST (lsp_bare_segment_skips_preprocessor_spacing );
52665435 RUN_TEST (lsp_resolve_qualified_static_call_normalizes_colons );
0 commit comments