@@ -440,9 +440,12 @@ static void parse_pyproject_toml(const char *source, int source_len, const char
440440 }
441441}
442442
443- /* Extract PSR-4 autoload entries from composer.json root. */
444- static void extract_psr4 (yyjson_val * root , const char * dir , cbm_pkg_entries_t * entries ) {
445- yyjson_val * autoload = yyjson_obj_get (root , "autoload" );
443+ /* Extract PSR-4 entries from one composer.json autoload section ("autoload"
444+ * or "autoload-dev": composer registers both, so test namespaces such as
445+ * Tests\ map to their directory as well). */
446+ static void extract_psr4 (yyjson_val * root , const char * section , const char * dir ,
447+ cbm_pkg_entries_t * entries ) {
448+ yyjson_val * autoload = yyjson_obj_get (root , section );
446449 if (!yyjson_is_obj (autoload )) {
447450 return ;
448451 }
@@ -459,6 +462,12 @@ static void extract_psr4(yyjson_val *root, const char *dir, cbm_pkg_entries_t *e
459462 }
460463 const char * ns_prefix = yyjson_get_str (key );
461464 const char * ns_dir = yyjson_get_str (val );
465+ while (ns_dir [0 ] == '.' && ns_dir [SKIP_ONE ] == '/' ) {
466+ ns_dir += PAIR_LEN ; /* "./src/" names src/ */
467+ }
468+ if (strcmp (ns_dir , "." ) == 0 ) {
469+ ns_dir = "" ; /* "." names the manifest's own directory */
470+ }
462471 char ns_entry [PKGMAP_PATH_BUF ];
463472 if (dir [0 ]) {
464473 snprintf (ns_entry , sizeof (ns_entry ), "%s/%s" , dir , ns_dir );
@@ -497,7 +506,8 @@ static void parse_composer_json(const char *source, int source_len, const char *
497506 }
498507 }
499508
500- extract_psr4 (root , dir , entries );
509+ extract_psr4 (root , "autoload" , dir , entries );
510+ extract_psr4 (root , "autoload-dev" , dir , entries );
501511
502512 free (dir );
503513 yyjson_doc_free (doc );
@@ -987,6 +997,12 @@ bool cbm_pkgmap_try_parse(const char *basename, const char *rel_path, const char
987997
988998/* ── Merge: per-worker entries → hash table ────────────────────── */
989999
1000+ /* A composer.json `autoload.psr-4` key: a namespace prefix ending in '\\'. */
1001+ static bool pkgmap_is_psr4_prefix (const char * pkg_name ) {
1002+ size_t n = pkg_name ? strlen (pkg_name ) : 0 ;
1003+ return n > 0 && pkg_name [n - SKIP_ONE ] == '\\' ;
1004+ }
1005+
9901006CBMHashTable * cbm_pkgmap_build (cbm_pkg_entries_t * worker_entries , int worker_count ,
9911007 const char * project_name ) {
9921008 /* Count total entries */
@@ -1004,8 +1020,14 @@ CBMHashTable *cbm_pkgmap_build(cbm_pkg_entries_t *worker_entries, int worker_cou
10041020 for (int w = 0 ; w < worker_count ; w ++ ) {
10051021 cbm_pkg_entries_t * we = & worker_entries [w ];
10061022 for (int i = 0 ; i < we -> count ; i ++ ) {
1007- /* Convert entry_rel to QN: project.dir.parts */
1008- char * qn = cbm_pipeline_fqn_module (project_name , we -> items [i ].entry_rel );
1023+ /* Convert entry_rel to QN: project.dir.parts. A PSR-4 namespace
1024+ * prefix (key ends in '\\') keeps its repo-relative DIRECTORY
1025+ * instead: its readers map `Prefix\Sub\Class` onto a file path
1026+ * (<dir>/Sub/Class.php), which a dotted QN cannot express (#1186). */
1027+ const char * entry_rel = we -> items [i ].entry_rel ;
1028+ char * qn = pkgmap_is_psr4_prefix (we -> items [i ].pkg_name )
1029+ ? cbm_strndup (entry_rel , strlen (entry_rel ))
1030+ : cbm_pipeline_fqn_module (project_name , entry_rel );
10091031 if (!qn ) {
10101032 continue ;
10111033 }
@@ -1355,7 +1377,8 @@ static char *resolve_dot_prefix(CBMHashTable *map, const char *module_path,
13551377}
13561378
13571379/* Try backslash-based prefix matching (PHP PSR-4: App\\Controllers\\Foo).
1358- * Returns heap QN or NULL. */
1380+ * The map value of a PSR-4 prefix is its repo-relative directory (see
1381+ * cbm_pkgmap_build). Returns heap QN or NULL. */
13591382static char * resolve_backslash_prefix (CBMHashTable * map , const char * module_path ,
13601383 const char * project_name ) {
13611384 char * buf = strdup (module_path );
@@ -1375,7 +1398,8 @@ static char *resolve_backslash_prefix(CBMHashTable *map, const char *module_path
13751398 }
13761399 const char * subpath = module_path + (size_t )(bs - buf ) + SKIP_ONE ;
13771400 char path_result [PKGMAP_PATH_BUF ];
1378- snprintf (path_result , sizeof (path_result ), "%s/%s" , base_dir , subpath );
1401+ snprintf (path_result , sizeof (path_result ), "%s%s%s" , base_dir , base_dir [0 ] ? "/" : "" ,
1402+ subpath );
13791403 for (char * c = path_result ; * c ; c ++ ) {
13801404 if (* c == '\\' ) {
13811405 * c = '/' ;
@@ -2025,6 +2049,91 @@ static const cbm_gbuf_node_t *resolve_sibling_file(const cbm_pipeline_ctx_t *ctx
20252049 return found ;
20262050}
20272051
2052+ /* ── PHP PSR-4 class imports (#1186) ──────────────────────────────── */
2053+
2054+ typedef enum {
2055+ PSR4_NOT_APPLICABLE = 0 , /* no composer psr-4 prefix covers the import */
2056+ PSR4_RESOLVED , /* the class file exists: *out is its File node */
2057+ PSR4_UNRESOLVED , /* a prefix covers it but no class file exists */
2058+ } psr4_outcome_t ;
2059+
2060+ /* The File node at exactly `rel_path`, or NULL. Looked up through the name
2061+ * index (File nodes are named by basename) and matched on the full path, so
2062+ * a same-named file in another directory never qualifies. */
2063+ static const cbm_gbuf_node_t * psr4_file_node (const cbm_pipeline_ctx_t * ctx , const char * rel_path ) {
2064+ const cbm_gbuf_node_t * * hits = NULL ;
2065+ int hit_count = 0 ;
2066+ if (cbm_gbuf_find_by_name (ctx -> gbuf , path_leaf (rel_path ), & hits , & hit_count ) != 0 || !hits ) {
2067+ return NULL ;
2068+ }
2069+ for (int i = 0 ; i < hit_count ; i ++ ) {
2070+ const cbm_gbuf_node_t * n = hits [i ];
2071+ if (n && n -> label && strcmp (n -> label , "File" ) == 0 && n -> file_path &&
2072+ strcmp (n -> file_path , rel_path ) == 0 ) {
2073+ return n ;
2074+ }
2075+ }
2076+ return NULL ;
2077+ }
2078+
2079+ /* Resolve a PHP class import `use A\B\C;` the way composer's PSR-4 autoloader
2080+ * does: for every autoload.psr-4 prefix covering the name, longest first, the
2081+ * class lives in exactly <prefix-dir>/<rest>.php with sub-namespaces as
2082+ * subdirectories; the first existing file wins. When a prefix covers the name
2083+ * but no such file exists the import is UNRESOLVED: it must never fall through
2084+ * to the namespace bucket, which binds it to whichever file of the namespace
2085+ * came first. `use function` / `use const` name namespace members, not class
2086+ * files, so they are not applicable. */
2087+ static psr4_outcome_t resolve_php_psr4_class (const cbm_pipeline_ctx_t * ctx ,
2088+ const char * source_file_qn , const CBMImport * imp ,
2089+ const cbm_gbuf_node_t * * out ) {
2090+ * out = NULL ;
2091+ CBMHashTable * pkgmap = cbm_pipeline_get_pkgmap ();
2092+ if (!pkgmap || imp -> kind != CBM_IMPORT_KIND_DEFAULT ) {
2093+ return PSR4_NOT_APPLICABLE ;
2094+ }
2095+ const char * name = imp -> module_path ;
2096+ if (name [0 ] == '\\' ) {
2097+ name ++ ; /* fully qualified `use \App\X;` */
2098+ }
2099+ char key [PKGMAP_PATH_BUF ];
2100+ size_t name_len = strlen (name );
2101+ if (name_len == 0 || name_len >= sizeof (key ) || !strchr (name , '\\' )) {
2102+ return PSR4_NOT_APPLICABLE ;
2103+ }
2104+ bool covered = false;
2105+ /* Right to left: each '\' ends a candidate prefix, longest first. */
2106+ for (size_t cut = name_len - SKIP_ONE ; cut > 0 ; cut -- ) {
2107+ if (name [cut - SKIP_ONE ] != '\\' ) {
2108+ continue ;
2109+ }
2110+ memcpy (key , name , cut );
2111+ key [cut ] = '\0' ;
2112+ const char * dir = (const char * )cbm_ht_get (pkgmap , key );
2113+ if (!dir ) {
2114+ continue ;
2115+ }
2116+ covered = true;
2117+ char rel [PKGMAP_PATH_BUF ];
2118+ int w = snprintf (rel , sizeof (rel ), "%s%s%s.php" , dir , dir [0 ] ? "/" : "" , name + cut );
2119+ if (w <= 0 || (size_t )w >= sizeof (rel )) {
2120+ continue ;
2121+ }
2122+ for (char * c = rel ; * c ; c ++ ) {
2123+ if (* c == '\\' ) {
2124+ * c = '/' ;
2125+ }
2126+ }
2127+ const cbm_gbuf_node_t * file = psr4_file_node (ctx , rel );
2128+ if (file && (!source_file_qn || !file -> qualified_name ||
2129+ strcmp (file -> qualified_name , source_file_qn ) != 0 )) {
2130+ * out = file ;
2131+ return PSR4_RESOLVED ;
2132+ }
2133+ }
2134+ return covered ? PSR4_UNRESOLVED : PSR4_NOT_APPLICABLE ;
2135+ }
2136+
20282137const cbm_gbuf_node_t * cbm_pipeline_resolve_import_node (const cbm_pipeline_ctx_t * ctx ,
20292138 const char * source_rel ,
20302139 const char * source_file_qn ,
@@ -2042,6 +2151,18 @@ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t
20422151 return header_target ;
20432152 }
20442153
2154+ /* PHP class imports covered by a composer psr-4 prefix name exactly one
2155+ * file; when it is absent the import stays unresolved (#1186). */
2156+ const cbm_gbuf_node_t * psr4_target = NULL ;
2157+ switch (resolve_php_psr4_class (ctx , source_file_qn , imp , & psr4_target )) {
2158+ case PSR4_RESOLVED :
2159+ return psr4_target ;
2160+ case PSR4_UNRESOLVED :
2161+ return NULL ;
2162+ case PSR4_NOT_APPLICABLE :
2163+ break ;
2164+ }
2165+
20452166 /* Strategy 1: module-path resolution → existing node (Python/TS/Go).
20462167 * No label filter here: directory-module languages (Go/Java packages)
20472168 * legitimately resolve straight to a Folder node -- that's the intended,
0 commit comments