* 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 )
| 590 | * several share the name. |
| 591 | */ |
| 592 | function resolveControllerMethod( |
| 593 | controller: string, |
| 594 | method: string, |
| 595 | context: ResolutionContext |
| 596 | ): string | null { |
| 597 | const rel = controller.replace(/\\/g, '/'); |
| 598 | const methodIn = (filePath: string): string | null => |
| 599 | context.getNodesInFile(filePath).find((n) => n.kind === 'method' && n.name === method)?.id ?? null; |
| 600 | |
| 601 | const controllerPath = `app/Http/Controllers/${rel}.php`; |
| 602 | if (context.fileExists(controllerPath)) { |
| 603 | const found = methodIn(controllerPath); |
| 604 | if (found) return found; |
| 605 | } |
| 606 | |
| 607 | const className = rel.split('/').pop()!; |
| 608 | const classes = context |
| 609 | .getNodesByName(className) |
| 610 | .filter((n) => n.kind === 'class' && n.filePath.includes('Controllers')); |
| 611 | if (rel.includes('/')) { |
| 612 | const suffix = `/${rel.toLowerCase()}.php`; |
| 613 | const exact = classes.filter((n) => `/${n.filePath.toLowerCase()}`.endsWith(suffix)); |
| 614 | for (const ctrl of exact) { |
| 615 | const found = methodIn(ctrl.filePath); |
| 616 | if (found) return found; |
| 617 | } |
| 618 | // A namespace path that names no file decides nothing by name alone. |
| 619 | if (classes.length > 1) return null; |
| 620 | } |
| 621 | for (const ctrl of classes) { |
| 622 | const found = methodIn(ctrl.filePath); |
| 623 | if (found) return found; |
| 624 | } |
| 625 | return null; |
| 626 | } |
no test coverage detected