fix(extract): resolve prefixed PHP use-imports to their target (#2661)

This commit is contained in:
Ben Younes
2026-08-12 14:20:21 +01:00
committed by safishamsi
parent b645a3a7f1
commit c285a94d45
2 changed files with 35 additions and 1 deletions
+3 -1
View File
@@ -2871,11 +2871,13 @@ def _resolve_php_type_references(
continue
tgt = edge.get("target")
label = stub_label.get(tgt)
uses = uses_by_file.get(ref_file, {})
if not label and relation == "imports":
label = next((alias for alias in uses if _make_id(alias) == tgt), "")
if not label:
continue
bare = label.strip().lower()
ns = ns_by_file[ref_file]
uses = uses_by_file.get(ref_file, {})
raw = None
if relation in _PHP_SUPERTYPE_RELATIONS:
+32
View File
@@ -156,3 +156,35 @@ def test_php_plain_no_namespace_inheritance_preserved(tmp_path: Path):
"no-namespace inheritance must still resolve to the real Base def"
)
assert tgt.get("label") == "Base"
def test_php_import_resolves_when_target_name_prefixes_sibling_classes(tmp_path: Path):
pivot = _write(
tmp_path / "src/Pivot.php",
"<?php\nnamespace App\\Entities;\nclass Pivot {}\n",
)
importer = _write(
tmp_path / "src/ModelWithRelation.php",
"<?php\nnamespace App\\Models;\n"
"use App\\Entities\\Pivot;\n"
"class ModelWithRelation {}\n",
)
siblings = [
_write(
tmp_path / f"src/{name}.php",
f"<?php\nnamespace App\\Repositories;\nclass {name} {{}}\n",
)
for name in ("PivotRepository", "PivotRepositoryEloquent", "PivotValidator")
]
result = extract([pivot, importer, *siblings], cache_root=tmp_path)
pivot_id = _class_defs(result, "Pivot")[0]["id"]
imports = [
edge
for edge in result["edges"]
if edge["relation"] == "imports"
and "modelwithrelation" in edge.get("source", "").lower()
]
assert len(imports) == 1
assert imports[0]["target"] == pivot_id