Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

fix(php): resolve a definition's namespace without rescanning the file

KageBinary7c8a387db98f
diff --git a/core-ingestion/src/__tests__/queries.php.test.ts b/core-ingestion/src/__tests__/queries.php.test.ts
index 94fb960b..1556d435 100644
--- a/core-ingestion/src/__tests__/queries.php.test.ts
+++ b/core-ingestion/src/__tests__/queries.php.test.ts
@@ -259,4 +259,46 @@ final class Union
phpCallKind: 'member',
});
});
+
+ it(
+ 'scopes every declaration in a large file without rescanning the top level',
+ { timeout: 10_000 },
+ () => {
+ // Resolving each definition's namespace by rescanning the file's top-level
+ // nodes is O(definitions x nodes). This file is ordinary, valid PSR-12 and
+ // well under the 1 MB ingest cap, but took ~13s to parse that way against
+ // ~150ms now — and a 16k-declaration file took over three minutes, which
+ // pins a parse worker with no per-file timeout.
+ const count = 4_000;
+ let source = '<?php\nnamespace App\\Support;\n';
+ for (let i = 0; i < count; i += 1) source += `function helper${i}($a) { return $a; }\n`;
+
+ const result = parseFile('/repo/big.php', source)!;
+
+ const scoped = result.entities.filter((e) => e.packageScope === 'App\\Support');
+ expect(scoped).toHaveLength(count);
+ },
+ );
+
+ it('scopes declarations by the nearest preceding unbraced namespace', () => {
+ // The binary search must land on the *last* declaration starting before the
+ // node, not the first or the nearest by distance.
+ const result = parseFile(
+ '/repo/Sequential.php',
+ `<?php
+namespace First;
+function alpha() {}
+namespace Second;
+function beta() {}
+namespace Third;
+function gamma() {}
+`,
+ )!;
+
+ const scopeOf = (name: string) =>
+ result.entities.find((e) => e.name === name)?.packageScope;
+ expect(scopeOf('alpha')).toBe('First');
+ expect(scopeOf('beta')).toBe('Second');
+ expect(scopeOf('gamma')).toBe('Third');
+ });
});
diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.ts
index 34848408..4188f08a 100644
--- a/core-ingestion/src/index.ts
+++ b/core-ingestion/src/index.ts
@@ -1986,19 +1986,55 @@ function unwrapRustCfgMacros(source: string): string {
// Main parse function
// ---------------------------------------------------------------------------
-function phpNamespaceForNode(rootNode: any, node: any): string | undefined {
+interface PhpNamespaceSpan {
+ startIndex: number;
+ namespace: string;
+}
+
+/**
+ * Start offsets of every unbraced `namespace X;` at file top level, ascending.
+ *
+ * Built once per parse. Doing this scan per definition instead made the file
+ * cost O(definitions x top-level nodes): a valid 890 KB PSR-12 file with ~16k
+ * functions took ~197s, against ~355ms without it.
+ *
+ * A braced `namespace X { ... }` scopes by containment rather than position and
+ * is handled by the ancestor walk in {@link phpNamespaceForNode}, so it is
+ * deliberately excluded here.
+ */
+function collectPhpNamespaceSpans(rootNode: any): PhpNamespaceSpan[] {
+ const spans: PhpNamespaceSpan[] = [];
+ // `namedChildren` materializes the list in a single walk; indexing
+ // `namedChild(i)` in a loop is what made the original scan expensive.
+ for (const child of rootNode.namedChildren ?? []) {
+ if (!child || child.type !== 'namespace_definition' || child.childForFieldName?.('body')) continue;
+ spans.push({
+ startIndex: child.startIndex,
+ namespace: child.childForFieldName?.('name')?.text?.replace(/\s+/g, '') ?? '',
+ });
+ }
+ return spans;
+}
+
+function phpNamespaceForNode(node: any, spans: PhpNamespaceSpan[]): string | undefined {
for (let current = node.parent; current; current = current.parent) {
if (current.type === 'namespace_definition') {
return current.childForFieldName?.('name')?.text?.replace(/\s+/g, '') ?? '';
}
}
+ // The last unbraced declaration starting before this node wins.
+ let low = 0;
+ let high = spans.length - 1;
let active: string | undefined;
- for (let i = 0; i < rootNode.namedChildCount; i++) {
- const child = rootNode.namedChild(i);
- if (!child || child.startIndex >= node.startIndex) break;
- if (child.type !== 'namespace_definition' || child.childForFieldName?.('body')) continue;
- active = child.childForFieldName?.('name')?.text?.replace(/\s+/g, '') ?? '';
+ while (low <= high) {
+ const mid = (low + high) >> 1;
+ if (spans[mid].startIndex < node.startIndex) {
+ active = spans[mid].namespace;
+ low = mid + 1;
+ } else {
+ high = mid - 1;
+ }
}
return active;
}
@@ -2147,6 +2183,12 @@ export function parseFile(filePath: string, source: string): FileParseResult | n
}
}
+ // Same idea for PHP: collect the file's namespace boundaries once rather
+ // than rescanning the top level for every definition.
+ const phpNamespaceSpans = language === SupportedLanguages.PHP
+ ? collectPhpNamespaceSpans(tree.rootNode)
+ : [];
+
// --- First pass: collect definitions ---
for (const match of pass1Matches) {
// Definition captures: name + definition.*
@@ -2220,7 +2262,7 @@ export function parseFile(filePath: string, source: string): FileParseResult | n
container: effectiveContainer,
packageScope: goPackage ?? (
language === SupportedLanguages.PHP
- ? phpNamespaceForNode(tree.rootNode, defNode)
+ ? phpNamespaceForNode(defNode, phpNamespaceSpans)
: undefined
),
});