MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / resolveControllerMethod

Function resolveControllerMethod

src/resolution/frameworks/laravel.ts:669–719  ·  view source on GitHub ↗

* Resolve a Controller@method reference: the file the path names under * `app/Http/Controllers/`, else a class of that name in a controllers * directory — the one whose path ends with the written namespace path when * several share the name.

(
  controller: string,
  method: string,
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

667 * several share the name.
668 */
669function resolveControllerMethod(
670 controller: string,
671 method: string,
672 context: ResolutionContext
673): string | null {
674 // A class the routes file named through its imports (#2975, #3292): that
675 // class's own method, or nothing. A same-named controller elsewhere, like
676 // the top-level `UserController` beside `Admin\UserController`, is not the
677 // one named. A name no project class has is read as a path below.
678 if (controller.startsWith('\\')) {
679 const named = phpClassesNamed(controller, context);
680 if (named.length > 0) {
681 for (const cls of named) {
682 const own = context.getNodesByQualifiedName(`${cls.qualifiedName}::${method}`)
683 .find((n) => n.kind === 'method' && n.filePath === cls.filePath);
684 if (own) return own.id;
685 }
686 return null;
687 }
688 controller = controllerPath(controller);
689 }
690 const rel = controller.replace(/\\/g, '/');
691 const methodIn = (filePath: string): string | null =>
692 context.getNodesInFile(filePath).find((n) => n.kind === 'method' && n.name === method)?.id ?? null;
693
694 const conventional = `app/Http/Controllers/${rel}.php`;
695 if (context.fileExists(conventional)) {
696 const found = methodIn(conventional);
697 if (found) return found;
698 }
699
700 const className = rel.split('/').pop()!;
701 const classes = context
702 .getNodesByName(className)
703 .filter((n) => n.kind === 'class' && n.filePath.includes('Controllers'));
704 if (rel.includes('/')) {
705 const suffix = `/${rel.toLowerCase()}.php`;
706 const exact = classes.filter((n) => `/${n.filePath.toLowerCase()}`.endsWith(suffix));
707 for (const ctrl of exact) {
708 const found = methodIn(ctrl.filePath);
709 if (found) return found;
710 }
711 // A namespace path that names no file decides nothing by name alone.
712 if (classes.length > 1) return null;
713 }
714 for (const ctrl of classes) {
715 const found = methodIn(ctrl.filePath);
716 if (found) return found;
717 }
718 return null;
719}

Callers 1

resolveFunction · 0.70

Calls 6

phpClassesNamedFunction · 0.85
controllerPathFunction · 0.85
fileExistsMethod · 0.80
methodInFunction · 0.70
getNodesByNameMethod · 0.65

Tested by

no test coverage detected