(ref: UnresolvedRef, context: ResolutionContext)
| 754 | }, |
| 755 | |
| 756 | resolve(ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null { |
| 757 | const name = ref.referenceName; |
| 758 | const resolved = (target: Node | undefined, confidence: number): ResolvedRef | null => |
| 759 | target ? { original: ref, targetNodeId: target.id, confidence, resolvedBy: 'framework' } : null; |
| 760 | |
| 761 | if (name.startsWith(SERVICE_REF)) return resolved(serviceNode(name.slice(SERVICE_REF.length), context), 0.9); |
| 762 | if (name.startsWith(FQCN_REF)) return resolved(classByFqcn(name.slice(FQCN_REF.length).replace(':', ''), context), 0.9); |
| 763 | if (name.startsWith(CLASS_REF)) { |
| 764 | const [file, className] = name.slice(CLASS_REF.length).split('#'); |
| 765 | return resolved(context.getNodesInFile(file!).find((n) => n.kind === 'class' && n.name === className), 0.95); |
| 766 | } |
| 767 | |
| 768 | const onContainer = CONTAINER_CALL.exec(name); |
| 769 | const onDrupal = onContainer ? null : DRUPAL_SERVICE_CALL.exec(name); |
| 770 | if (onContainer || onDrupal) { |
| 771 | const method = (onContainer?.[2] ?? onDrupal?.[1])!; |
| 772 | const id = onContainer?.[1] ?? serviceOnLine(ref, method, context); |
| 773 | const cls = id ? serviceClass(id, context) : undefined; |
| 774 | return resolved(cls && methodOf(cls, method, context), 0.9); |
| 775 | } |
| 776 | |
| 777 | // _controller: '\Drupal\module\...\ClassName::methodName' (double colon) or the |
| 778 | // single-colon controller-service form '\Drupal\...\ClassName:methodName'. |
| 779 | const controllerMatch = name.match(/^\\?(?:Drupal\\[^:]+\\)?([^\\:]+):{1,2}(\w+)$/); |
| 780 | if (controllerMatch) { |
| 781 | const [, className, methodName] = controllerMatch; |
| 782 | const classNodes = context.getNodesByName(className!); |
| 783 | for (const cls of classNodes) { |
| 784 | if (cls.kind !== 'class') continue; |
| 785 | const fileNodes = context.getNodesInFile(cls.filePath); |
| 786 | const method = fileNodes.find((n) => n.kind === 'method' && n.name === methodName); |
| 787 | if (method) { |
| 788 | return { original: ref, targetNodeId: method.id, confidence: 0.9, resolvedBy: 'framework' }; |
| 789 | } |
| 790 | return { original: ref, targetNodeId: cls.id, confidence: 0.7, resolvedBy: 'framework' }; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | // _form / _entity_form: '\Drupal\module\...\ClassName' (bare FQCN, no method) |
| 795 | if (name.includes('\\') && !name.includes(':')) { |
| 796 | // The namespace first: one class name can sit in several, as a plugin's |
| 797 | // attribute and annotation classes do. |
| 798 | const className = lastSegment(name); |
| 799 | const cls = classByFqcn(name, context) ?? (className ? context.getNodesByName(className).find((n) => n.kind === 'class') : undefined); |
| 800 | if (cls) { |
| 801 | return { original: ref, targetNodeId: cls.id, confidence: 0.85, resolvedBy: 'framework' }; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | // hook_X — find any function whose name ends in _{hookSuffix} in a hook file |
| 806 | // A method only has #[Hook] to go by: another module's function is no answer to it. |
| 807 | if (name.startsWith('hook_') && !ref.fromNodeId.startsWith('method:')) { |
| 808 | const hookSuffix = name.slice(5); // strip 'hook_' |
| 809 | const candidates = context.getNodesByKind('function').filter( |
| 810 | (n) => n.name.endsWith(`_${hookSuffix}`) && isDrupalHookFile(n.filePath) |
| 811 | ); |
| 812 | if (candidates.length > 0) { |
| 813 | return { |
nothing calls this directly
no test coverage detected