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

Function resolvePhpQualifiedClassRef

src/resolution/import-resolver.ts:3002–3055  ·  view source on GitHub ↗
(
  ref: UnresolvedRef,
  context: ResolutionContext,
)

Source from the content-addressed store, hash-verified

3000 * — it lives outside the project — so name fallbacks must not guess.
3001 */
3002export function resolvePhpQualifiedClassRef(
3003 ref: UnresolvedRef,
3004 context: ResolutionContext,
3005): ResolvedRef | null | undefined {
3006 if (ref.language !== 'php') return undefined;
3007 let name = ref.referenceName;
3008 let member: string | null = null;
3009 if (ref.referenceKind === 'calls') {
3010 // `(new Ns\Foo())->bar()` calls the object built, which the name matcher
3011 // reads (matchPhpNewReceiverCall), not a static method of a class.
3012 if (/^\(\s*new\s/i.test(name)) return undefined;
3013 // A static call on the class: `Field\FirstName::make()` is written `Field\FirstName.make`.
3014 const call = /^(.*\\[^\\.:]+)(?:\.|::)(\w+)$/.exec(name);
3015 if (!call) return undefined;
3016 name = call[1]!;
3017 member = call[2]!;
3018 } else if (!PHP_CLASS_NAME_REFS.has(ref.referenceKind)) {
3019 return undefined;
3020 }
3021 const separator = name.indexOf('\\');
3022 if (separator < 0) return undefined;
3023
3024 let fqn: string;
3025 if (separator === 0) {
3026 fqn = name.slice(1);
3027 } else {
3028 // PHP matches a namespace alias without regard to case.
3029 const head = name.slice(0, separator).toLowerCase();
3030 const imp = context.getImportMappings(ref.filePath, ref.language).find((i) => i.localName.toLowerCase() === head);
3031 if (imp) {
3032 fqn = imp.source.replace(/^\\/, '') + name.slice(separator);
3033 } else {
3034 // `namespace App;` applies until the next namespace statement.
3035 const namespace = context.getNodesInFile(ref.filePath)
3036 .filter((n) => n.kind === 'namespace' && n.startLine <= ref.line)
3037 .sort((a, b) => b.startLine - a.startLine)[0];
3038 fqn = namespace ? `${namespace.qualifiedName}\\${name}` : name;
3039 }
3040 }
3041
3042 const cut = fqn.lastIndexOf('\\');
3043 const qualifiedName = cut < 0 ? fqn : `${fqn.slice(0, cut)}::${fqn.slice(cut + 1)}`;
3044 const classes = context.getNodesByQualifiedName(qualifiedName)
3045 .filter((n) => n.language === 'php' && STATIC_MEMBER_CONTAINERS.has(n.kind));
3046 // A type mention can name something other than a class (a namespaced
3047 // constant or function), so it keeps the ordinary strategies.
3048 if (classes.length !== 1) return ref.referenceKind === 'references' ? undefined : null;
3049 const owner = classes[0]!;
3050 if (!member) return { original: ref, targetNodeId: owner.id, confidence: 0.95, resolvedBy: 'import' };
3051 const methods = phpOwnMethods(owner, member, context);
3052 // A method the class inherits is left to the strategies that walk supertypes.
3053 if (methods.length !== 1) return undefined;
3054 return { original: ref, targetNodeId: methods[0]!.id, confidence: 0.95, resolvedBy: 'import' };
3055}
3056
3057/** TypeScript and JavaScript files: where the extractors record type names. */
3058const TS_JS_FILE = /\.[cm]?[jt]sx?$/i;

Callers 1

resolveOneInnerMethod · 0.90

Calls 6

phpOwnMethodsFunction · 0.85
hasMethod · 0.80
getImportMappingsMethod · 0.80
execMethod · 0.65
getNodesInFileMethod · 0.65

Tested by

no test coverage detected