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): scope use to its namespace block, and stop double-emitting calls

KageBinary663c6ce0c3fd
diff --git a/core-ingestion/src/__tests__/patchBuilder.resolution.test.ts b/core-ingestion/src/__tests__/patchBuilder.resolution.test.ts
index 9dd909bb..a49dbde3 100644
--- a/core-ingestion/src/__tests__/patchBuilder.resolution.test.ts
+++ b/core-ingestion/src/__tests__/patchBuilder.resolution.test.ts
@@ -427,4 +427,29 @@ describe('multi-repo co-ingest identity convergence', () => {
expect(callsDst).toBe(svcbUtilNode);
});
+
+ it('emits one edge when two PHP call kinds resolve to the same node', () => {
+ // `log()` and `Logger::log()` are two relationships once phpCallKind splits
+ // the dedup key. Both resolve to the same member here, so salting the edge
+ // id by call kind would mint two ids for one edge — and the dedup
+ // downstream keys on id, so both would commit.
+ const consumer = parseFile(
+ '/repo/Caller.php',
+ `<?php
+use Vendor\\Package\\Logger;
+function handler(): void { log(); Logger::log(); }
+`,
+ )!;
+ const logger = parseFile(
+ '/repo/Vendor/Package/Logger.php',
+ '<?php namespace Vendor\\Package; class Logger { public static function log(): void {} }',
+ )!;
+
+ const patch = buildPatchWithResolution(consumer, 'hash', '', resolveEdges([consumer, logger]));
+ const callEdges = patch.ops.filter(
+ (op) => op.type === 'UpsertEdge' && op.predicate === 'CALLS',
+ );
+
+ expect(callEdges).toHaveLength(1);
+ });
});
diff --git a/core-ingestion/src/__tests__/resolveEdges.test.ts b/core-ingestion/src/__tests__/resolveEdges.test.ts
index 076ea204..e3126044 100644
--- a/core-ingestion/src/__tests__/resolveEdges.test.ts
+++ b/core-ingestion/src/__tests__/resolveEdges.test.ts
@@ -357,6 +357,37 @@ function run(Alpha $a): void { new Alpha(); }
expect(edges.filter((edge) => edge.dstFilePath === '/repo/Other/Alpha.php')).toEqual([]);
});
+ it('does not leak a PHP use across braced namespace blocks', () => {
+ // `use` is scoped to its namespace block. `go` lives in namespace B, which
+ // declares its own Thing, so it must not resolve to the Thing imported by
+ // namespace A.
+ const consumer = parseFile(
+ '/repo/Multi.php',
+ `<?php
+namespace A {
+ use Vendor\\Package\\Thing;
+ function run(Thing $t): void { new Thing(); }
+}
+namespace B {
+ class Thing {}
+ function go(Thing $t): void { new Thing(); }
+}
+`,
+ )!;
+ const vendor = parseFile(
+ '/repo/Vendor/Package/Thing.php',
+ '<?php namespace Vendor\\Package; class Thing {}',
+ )!;
+
+ const edges = resolveEdges([consumer, vendor]);
+
+ expect(
+ edges.filter(
+ (edge) => edge.srcName === 'go' && edge.dstFilePath === '/repo/Vendor/Package/Thing.php',
+ ),
+ ).toEqual([]);
+ });
+
it('resolves PHP class imports, references, and calls by exact FQCN', () => {
const consumer = parseFile(
'/repo/Consumer.php',
diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.ts
index e7603d86..34848408 100644
--- a/core-ingestion/src/index.ts
+++ b/core-ingestion/src/index.ts
@@ -3332,6 +3332,18 @@ export function resolveEdges(
const phpFqcnImportsByLocal = new Map<string, Map<string, string | null>>();
for (const result of results) {
if (result.language !== SupportedLanguages.PHP) continue;
+ // A `use` is scoped to its namespace *block*, but this index is per file.
+ // In a file with several braced namespaces, an import in one block would be
+ // applied to the others — resolving a name that a later block declares
+ // itself, and pointing it confidently at the wrong file. One namespace per
+ // file is the PSR-12 norm, so skip the rare multi-block file entirely
+ // rather than mis-resolve it; it simply keeps the pre-existing behaviour.
+ const namespaceScopes = new Set(
+ result.entities
+ .filter(entity => entity.kind !== 'file' && typeof entity.packageScope === 'string')
+ .map(entity => entity.packageScope as string),
+ );
+ if (namespaceScopes.size > 1) continue;
for (const rel of result.relationships) {
if (
rel.predicate !== 'IMPORTS' ||
diff --git a/core-ingestion/src/patch-builder.ts b/core-ingestion/src/patch-builder.ts
index 114f6eb9..f23ec1a6 100644
--- a/core-ingestion/src/patch-builder.ts
+++ b/core-ingestion/src/patch-builder.ts
@@ -617,6 +617,7 @@ export function buildPatchWithResolution(
}
const seenExternalNodes2 = new Set<string>();
+ const emittedEdgeIdentities = new Set<string>();
const relationshipShapeCounts = new Map<string, number>();
for (const relationship of relationships) {
const key = `${relationship.srcName}:${relationship.predicate}:${relationship.dstName}`;
@@ -665,6 +666,14 @@ export function buildPatchWithResolution(
const edgeDstKey = r.phpCallKind && (relationshipShapeCounts.get(baseResolutionKey) ?? 0) > 1
? `${dstKey}:${r.phpCallKind}`
: dstKey;
+ // The phpCallKind salt above exists so `foo()` and `Bar::foo()` keep distinct
+ // ids when they resolve to *different* targets. When they resolve to the same
+ // node it instead mints two ids for one edge, and since the dedup downstream
+ // keys on id, both commit. An edge is identified by (src, dst, predicate) —
+ // two of those with empty attrs are indistinguishable in the graph.
+ const edgeIdentity = `${nodeId(idPath, srcKey)}${dstNodeId}${r.predicate}`;
+ if (emittedEdgeIdentities.has(edgeIdentity)) continue;
+ emittedEdgeIdentities.add(edgeIdentity);
ops.push({
type: 'UpsertEdge',
id: edgeId(idPath, srcKey, edgeDstKey, r.predicate),