Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(routes): leave a PSR-4-covered handler without a class file unresolved

Martin Vogel70e73442ed3c
diff --git a/src/pipeline/pass_pkgmap.c b/src/pipeline/pass_pkgmap.c
index 71d3d3bec..c82cdf645 100644
--- a/src/pipeline/pass_pkgmap.c
+++ b/src/pipeline/pass_pkgmap.c
@@ -2138,18 +2138,10 @@ static bool psr4_class_label(const char *label) {
strcmp(label, "Trait") == 0 || strcmp(label, "Enum") == 0);
}
-const char *cbm_pipeline_psr4_member_qn(const cbm_gbuf_t *gbuf, const char *class_fqn,
- const char *member) {
- if (!gbuf || !class_fqn || !member || !member[0]) {
- return NULL;
- }
- const CBMImport imp = {.module_path = class_fqn, .kind = CBM_IMPORT_KIND_DEFAULT};
- const cbm_gbuf_node_t *file = NULL;
- if (resolve_php_psr4_class(gbuf, NULL, &imp, &file) != PSR4_RESOLVED || !file->file_path) {
- return NULL;
- }
- const char *cls = strrchr(class_fqn, '\\');
- cls = cls ? cls + SKIP_ONE : class_fqn;
+/* The QN of `member` on the class `cls` defined in `file_path`, or NULL when
+ * that file defines no such class or the class has no such member. */
+static const char *psr4_member_in_file(const cbm_gbuf_t *gbuf, const char *file_path,
+ const char *cls, const char *member) {
const cbm_gbuf_node_t **hits = NULL;
int hit_count = 0;
if (cbm_gbuf_find_by_name(gbuf, cls, &hits, &hit_count) != 0 || !hits) {
@@ -2158,7 +2150,7 @@ const char *cbm_pipeline_psr4_member_qn(const cbm_gbuf_t *gbuf, const char *clas
for (int i = 0; i < hit_count; i++) {
const cbm_gbuf_node_t *c = hits[i];
if (!c || !psr4_class_label(c->label) || !c->qualified_name || !c->file_path ||
- strcmp(c->file_path, file->file_path) != 0) {
+ strcmp(c->file_path, file_path) != 0) {
continue;
}
char qn[CBM_SZ_1K];
@@ -2172,6 +2164,29 @@ const char *cbm_pipeline_psr4_member_qn(const cbm_gbuf_t *gbuf, const char *clas
return NULL;
}
+cbm_psr4_member_t cbm_pipeline_psr4_member_qn(const cbm_gbuf_t *gbuf, const char *class_fqn,
+ const char *member, const char **out_qn) {
+ *out_qn = NULL;
+ if (!gbuf || !class_fqn || !member || !member[0]) {
+ return CBM_PSR4_MEMBER_NOT_COVERED;
+ }
+ const CBMImport imp = {.module_path = class_fqn, .kind = CBM_IMPORT_KIND_DEFAULT};
+ const cbm_gbuf_node_t *file = NULL;
+ psr4_outcome_t placed = resolve_php_psr4_class(gbuf, NULL, &imp, &file);
+ if (placed == PSR4_NOT_APPLICABLE) {
+ return CBM_PSR4_MEMBER_NOT_COVERED;
+ }
+ /* Covered from here on: composer would load this file or none, so a
+ * missing file, class or member leaves the member unresolved. */
+ if (placed != PSR4_RESOLVED || !file->file_path) {
+ return CBM_PSR4_MEMBER_UNRESOLVED;
+ }
+ const char *cls = strrchr(class_fqn, '\\');
+ cls = cls ? cls + SKIP_ONE : class_fqn;
+ *out_qn = psr4_member_in_file(gbuf, file->file_path, cls, member);
+ return *out_qn ? CBM_PSR4_MEMBER_RESOLVED : CBM_PSR4_MEMBER_UNRESOLVED;
+}
+
const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t *ctx,
const char *source_rel,
const char *source_file_qn,
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 650f8a08d..fb3ed1253 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -213,24 +213,38 @@ void cbm_pipeline_set_pkgmap(CBMHashTable *map);
* Laravel `[Class::class, 'method']` and invokable `Class::class` handlers
* (#1146), with the class already qualified through the file's `use` imports
* and namespace — is placed on a method QN only by what places the class file:
- * the composer.json PSR-4 class file in gbuf (cbm_pipeline_psr4_member_qn),
- * else the one candidate whose directories end with the class's namespace.
- * Never by short name alone, so a vendor controller that is not in the repo
- * gets no handler. A reference without "::" resolves exactly as
- * cbm_registry_resolve does. */
+ * the composer.json PSR-4 class file in gbuf (cbm_pipeline_psr4_member_qn).
+ * When a PSR-4 prefix covers the class, that answer is final: a covered class
+ * whose file or member is absent stays unresolved, as its `use` import does
+ * (#1186). Only a class no prefix covers falls back to the one candidate whose
+ * directories end with the class's namespace. Never by short name alone, so a
+ * vendor controller that is not in the repo gets no handler. A reference
+ * without "::" resolves exactly as cbm_registry_resolve does. */
cbm_resolution_t cbm_registry_resolve_handler(const cbm_registry_t *r, const char *handler_ref,
const char *module_qn, const char **import_map_keys,
const char **import_map_vals, int import_map_count,
const cbm_gbuf_t *gbuf);
-/* The QN of `member` on the PHP class `class_fqn` ("Ns\\Class", NUL-
- * terminated), placed the way composer's PSR-4 autoloader places the class:
- * the class file the import resolver picks for `use Ns\\Class;` (#1186), the
- * class that file defines, then that class's member. NULL when no PSR-4
- * prefix covers the class, its file is absent, or the class has no such
- * member. The returned string is owned by gbuf. Read-only on gbuf. */
-const char *cbm_pipeline_psr4_member_qn(const cbm_gbuf_t *gbuf, const char *class_fqn,
- const char *member);
+/* Where composer's PSR-4 autoloader places a PHP class member. Tri-state, so
+ * "no prefix covers the class" (another strategy may answer) is never
+ * confused with "a prefix covers it but places nothing" (final). */
+typedef enum {
+ CBM_PSR4_MEMBER_NOT_COVERED = 0, /* no psr-4 prefix covers the class */
+ CBM_PSR4_MEMBER_RESOLVED, /* *out_qn is the member's QN */
+ CBM_PSR4_MEMBER_UNRESOLVED, /* covered, but no class file holds the
+ * class with this member: leave it
+ * unresolved, never guess elsewhere */
+} cbm_psr4_member_t;
+
+/* Place `member` on the PHP class `class_fqn` ("Ns\\Class", NUL-terminated)
+ * the way composer's PSR-4 autoloader places the class: the class file the
+ * import resolver picks for `use Ns\\Class;` (#1186), the class that file
+ * defines, then that class's member. *out_qn is set only on
+ * CBM_PSR4_MEMBER_RESOLVED (else NULL) and is owned by gbuf. Missing
+ * arguments or no package map give CBM_PSR4_MEMBER_NOT_COVERED. Read-only on
+ * gbuf. */
+cbm_psr4_member_t cbm_pipeline_psr4_member_qn(const cbm_gbuf_t *gbuf, const char *class_fqn,
+ const char *member, const char **out_qn);
/* Unified module resolver: relative → pkgmap → fqn_module fallback.
* Handles bare specifiers via pkgmap lookup with prefix matching.
diff --git a/src/pipeline/registry.c b/src/pipeline/registry.c
index 3e564f816..1e4e0819e 100644
--- a/src/pipeline/registry.c
+++ b/src/pipeline/registry.c
@@ -1506,17 +1506,17 @@ static int qn_class_member_tail(const char *qn, const char *cls, size_t cls_len,
* defines, and that class's member — through the PSR-4 import resolver
* (cbm_pipeline_psr4_member_qn), so a root whose folder does not mirror its
* namespace (Acme\\Blog\\ -> packages/blog/src) places the class as well.
- * Returns the member QN, or NULL (no root covers the FQN, its class file is
- * absent, or the class holds no such member). */
-static const char *psr4_member_qn(const cbm_gbuf_t *gbuf, const char *fqn, size_t fqn_len,
- const char *member) {
+ * Returns its tri-state outcome; *out_qn is the member QN when RESOLVED. */
+static cbm_psr4_member_t psr4_member_qn(const cbm_gbuf_t *gbuf, const char *fqn, size_t fqn_len,
+ const char *member, const char **out_qn) {
char cls_fqn[CBM_SZ_512];
+ *out_qn = NULL;
if (!gbuf || fqn_len >= sizeof(cls_fqn)) {
- return NULL;
+ return CBM_PSR4_MEMBER_NOT_COVERED;
}
memcpy(cls_fqn, fqn, fqn_len);
cls_fqn[fqn_len] = '\0';
- return cbm_pipeline_psr4_member_qn(gbuf, cls_fqn, member);
+ return cbm_pipeline_psr4_member_qn(gbuf, cls_fqn, member, out_qn);
}
/* Do the directory segments of qn (between the project root segment and the
@@ -1597,9 +1597,16 @@ cbm_resolution_t cbm_registry_resolve_handler(const cbm_registry_t *r, const cha
if (cls_len == 0 || !member[0]) {
return empty_result();
}
- const char *qn = psr4_member_qn(gbuf, handler_ref, fqn_len, member);
- if (qn) {
+ const char *qn = NULL;
+ switch (psr4_member_qn(gbuf, handler_ref, fqn_len, member, &qn)) {
+ case CBM_PSR4_MEMBER_RESOLVED:
return (cbm_resolution_t){qn, "php_psr4", CONF_PSR4_MEMBER, REG_RESOLVED};
+ case CBM_PSR4_MEMBER_UNRESOLVED:
+ /* A covered class lives where PSR-4 says or nowhere; a namespace-
+ * mirroring folder elsewhere would be a guessed edge (#1186). */
+ return empty_result();
+ case CBM_PSR4_MEMBER_NOT_COVERED:
+ break;
}
qn = aligned_member_qn(r, handler_ref, ns_len, cls, cls_len, member);
if (qn) {
diff --git a/tests/test_edge_types_probe.c b/tests/test_edge_types_probe.c
index 999019c45..6dfa94e1c 100644
--- a/tests/test_edge_types_probe.c
+++ b/tests/test_edge_types_probe.c
@@ -939,7 +939,11 @@ static int et_qn_has_tail(const char *qn, const char *tail) {
return ql >= tl && strcmp(qn + ql - tl, tail) == 0 && (ql == tl || qn[ql - tl - 1] == '.');
}
-static int et_handles_exact(const EtFile *files, int nfiles, const EtHandles *want, int parallel) {
+/* route_qns (optional, NULL-terminated): Route qualified_names that must
+ * exist, so a test asserting that a route gets NO handler cannot pass merely
+ * because the route itself was never extracted. */
+static int et_handles_exact_routes(const EtFile *files, int nfiles, const EtHandles *want,
+ int parallel, const char *const *route_qns) {
EtProj lp;
cbm_store_t *store =
parallel ? et_index_parallel(&lp, files, nfiles) : et_index_files(&lp, files, nfiles);
@@ -983,6 +987,16 @@ static int et_handles_exact(const EtFile *files, int nfiles, const EtHandles *wa
want[wi].route);
}
}
+ for (int ri = 0; route_qns && route_qns[ri]; ri++) {
+ cbm_node_t route = {0};
+ if (!store ||
+ cbm_store_find_node_by_qn(store, lp.project, route_qns[ri], &route) != CBM_STORE_OK) {
+ ok = 0;
+ fprintf(stderr, " [ET-HANDLES] missing Route %s\n", route_qns[ri]);
+ } else {
+ cbm_node_free_fields(&route);
+ }
+ }
if (!ok) {
fprintf(stderr, " [ET-HANDLES] FAIL (%s path) expected=%d actual=%d\n",
parallel ? "parallel" : "sequential", wanted, n);
@@ -992,6 +1006,10 @@ static int et_handles_exact(const EtFile *files, int nfiles, const EtHandles *wa
return ok;
}
+static int et_handles_exact(const EtFile *files, int nfiles, const EtHandles *want, int parallel) {
+ return et_handles_exact_routes(files, nfiles, want, parallel, NULL);
+}
+
/* #1146: Laravel's two class-based handler forms —
* Route::get('/x', [UserController::class, 'show']) (controller method)
* Route::get('/x', GetCurrentUserController::class) (invokable: __invoke)
@@ -1085,6 +1103,56 @@ TEST(handles_laravel_class_handlers_no_composer_issue1146) {
PASS();
}
+/* A PSR-4 prefix that covers a handler class decides where the class lives,
+ * as it does for the class's `use` import (#1186): when composer would load
+ * no file holding the member, the route stays without a handler. Both routes
+ * below are covered by Acme\Blog\ -> packages/blog/src/:
+ * /blog/missing MissingController has no class file there at all;
+ * /blog/archive PostsController's class file exists but has no archive().
+ * legacy/Acme/Blog/Http mirrors the namespace and holds both members, so the
+ * namespace/folder fallback would bind both routes to a class composer never
+ * loads. It must not run for a covered class. These files are added to the
+ * fixture above, whose four handlers must keep their edges. */
+static const EtFile et_laravel_psr4_absent_extra[] = {
+ {"legacy/Acme/Blog/Http/MissingController.php",
+ "<?php\nnamespace Acme\\Blog\\Http;\n\n"
+ "class MissingController {\n"
+ " public function index() { return ['stale' => true]; }\n}\n"},
+ {"legacy/Acme/Blog/Http/PostsController.php",
+ "<?php\nnamespace Acme\\Blog\\Http;\n\n"
+ "class PostsController {\n"
+ " public function archive() { return ['stale' => true]; }\n}\n"},
+ {"routes/blog.php",
+ "<?php\n"
+ "use Illuminate\\Support\\Facades\\Route;\n\n"
+ "Route::get('/blog/missing', [\\Acme\\Blog\\Http\\MissingController::class, 'index']);\n"
+ "Route::get('/blog/archive', [\\Acme\\Blog\\Http\\PostsController::class, 'archive']);\n"}};
+
+enum { ET_LARAVEL_PSR4_ABSENT_EXTRA = 3 };
+
+static const char *const et_laravel_psr4_absent_routes[] = {
+ "__route__GET__/blog/missing", "__route__GET__/blog/archive", NULL};
+
+static int et_laravel_psr4_absent(int parallel) {
+ EtFile f[ET_LARAVEL_CLASS_FILES + ET_LARAVEL_PSR4_ABSENT_EXTRA];
+ memcpy(f, et_laravel_class_handlers, sizeof(et_laravel_class_handlers));
+ memcpy(f + ET_LARAVEL_CLASS_FILES, et_laravel_psr4_absent_extra,
+ sizeof(et_laravel_psr4_absent_extra));
+ return et_handles_exact_routes(f, ET_LARAVEL_CLASS_FILES + ET_LARAVEL_PSR4_ABSENT_EXTRA,
+ et_laravel_class_handles, parallel,
+ et_laravel_psr4_absent_routes);
+}
+
+TEST(handles_laravel_psr4_absent_class_issue1146) {
+ ASSERT_TRUE(et_laravel_psr4_absent(0));
+ PASS();
+}
+
+TEST(handles_laravel_psr4_absent_class_parallel_issue1146) {
+ ASSERT_TRUE(et_laravel_psr4_absent(1));
+ PASS();
+}
+
/* Rails (Ruby) — ActionDispatch router. The handler MUST be passed as a bare
* identifier (not the idiomatic `to: 'list_items'` string, which extract_handler_arg
* cannot capture). mapper.get resolves by name to the Mapper#get method whose QN
@@ -2304,6 +2372,8 @@ SUITE(edge_types_probe) {
RUN_TEST(handles_laravel_class_handlers_issue1146);
RUN_TEST(handles_laravel_class_handlers_parallel_issue1146);
RUN_TEST(handles_laravel_class_handlers_no_composer_issue1146);
+ RUN_TEST(handles_laravel_psr4_absent_class_issue1146);
+ RUN_TEST(handles_laravel_psr4_absent_class_parallel_issue1146);
RUN_TEST(handles_rails_ruby);
RUN_TEST(handles_actix_rust);