Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(php): record the declared class name in imported_names (#2972)
Raghav Chamadiya622dcfbd6601
diff --git a/packages/core/src/repowise/core/ingestion/extractors/bindings/php.py b/packages/core/src/repowise/core/ingestion/extractors/bindings/php.pyindex 4731038e1..937be4b3a 100644--- a/packages/core/src/repowise/core/ingestion/extractors/bindings/php.py+++ b/packages/core/src/repowise/core/ingestion/extractors/bindings/php.py@@ -31,7 +31,16 @@ def _clause(clause: Node, prefix: str, src: str) -> tuple[str, str] | None:if not target:return Nonefqn = f"{prefix}\\{target}" if prefix else target- return fqn, alias or fqn.rsplit("\\", 1)[-1]+ return fqn, alias or php_class_name(fqn)+++def php_class_name(name: str) -> str:+ """The class a bare, relative or fully qualified name denotes, as declared.++ ``imported_names`` carries names as the target file declares them, so+ ``use A\\B as C`` imports ``B`` and ``\\A\\B::class`` names ``B``.+ """+ return name.rsplit("\\", 1)[-1]def php_use_clauses(stmt_node: Node, src: str) -> list[tuple[str, str]]:@@ -63,7 +72,7 @@ def php_use_clauses(stmt_node: Node, src: str) -> list[tuple[str, str]]:def extract_php_bindings(stmt_node: Node, src: str) -> tuple[list[str], list[NamedBinding]]:"""Extract bindings from PHP use declarations."""clauses = php_use_clauses(stmt_node, src)- return [local for _, local in clauses], [+ return [php_class_name(fqn) for fqn, _ in clauses], [NamedBinding(local_name=local, exported_name=fqn, source_file=None)for fqn, local in clauses]diff --git a/packages/core/src/repowise/core/ingestion/languages/php_same_namespace.py b/packages/core/src/repowise/core/ingestion/languages/php_same_namespace.pyindex 9259a955a..5388f15f2 100644--- a/packages/core/src/repowise/core/ingestion/languages/php_same_namespace.py+++ b/packages/core/src/repowise/core/ingestion/languages/php_same_namespace.py@@ -29,6 +29,7 @@ import refrom typing import TYPE_CHECKING, Anyfrom ..cohesion import DOC_COMMENT_HINT, SAME_NAMESPACE_HINT+from ..extractors.bindings.php import php_class_namefrom .scope_scan import FileScope, ScopeTier, emit_scope_edgesif TYPE_CHECKING:@@ -201,7 +202,19 @@ def resolve_php_same_namespace_refs(# reads the raw text, comments included; ``emit_scope_edges`` skips any# pair already linked, so it only adds files no code names.code_texts = sorted((path, blank_php_comments(text)) for path, text in texts.items())- added = emit_scope_edges(graph, code_texts, plan, skip_names=frozenset(), ident_re=_NAME_RE)+ added = emit_scope_edges(+ graph,+ code_texts,+ plan,+ skip_names=frozenset(),+ ident_re=_NAME_RE,+ declared_name=php_class_name,+ )return added + emit_scope_edges(- graph, sorted(texts.items()), comment_plan, skip_names=frozenset(), ident_re=_NAME_RE+ graph,+ sorted(texts.items()),+ comment_plan,+ skip_names=frozenset(),+ ident_re=_NAME_RE,+ declared_name=php_class_name,)diff --git a/packages/core/src/repowise/core/ingestion/languages/scope_scan.py b/packages/core/src/repowise/core/ingestion/languages/scope_scan.pyindex 30b9e3337..12ca3bf16 100644--- a/packages/core/src/repowise/core/ingestion/languages/scope_scan.py+++ b/packages/core/src/repowise/core/ingestion/languages/scope_scan.py@@ -59,6 +59,7 @@ def emit_scope_edges(*,skip_names: frozenset[str],ident_re: re.Pattern[str] = _TYPE_IDENT_RE,+ declared_name: Callable[[str], str] = str,) -> int:"""Scan *files* for implicit scope references and add ``imports`` edges.@@ -67,7 +68,9 @@ def emit_scope_edges(visible to one file, or ``None`` to skip the file entirely. *skip_names*is the language's stdlib/default-import set, checked before every tier.*ident_re* overrides the candidate-identifier shape for a language whose- type names are not reliably capitalised ASCII.+ type names are not reliably capitalised ASCII. *declared_name* maps an+ identifier to the name its target declares, for a language whose+ identifiers can be qualified; an edge's ``imported_names`` carries that.Returns the number of edges added.@@ -96,7 +99,7 @@ def emit_scope_edges(target = next(iter(declaring))if target != path:names, _ = found.setdefault(target, ([], tier.hint))- names.append(ident)+ names.append(declared_name(ident))breakfor target, (names, hint) in sorted(found.items()):diff --git a/packages/core/src/repowise/core/ingestion/parser.py b/packages/core/src/repowise/core/ingestion/parser.pyindex efa852045..9bf348607 100644--- a/packages/core/src/repowise/core/ingestion/parser.py+++ b/packages/core/src/repowise/core/ingestion/parser.py@@ -1047,14 +1047,14 @@ def _scala_imports(stmt_node: Node, raw: str, src: str) -> list[Import]:def _php_imports(stmt_node: Node, raw: str, src: str) -> list[Import]:"""PHP: one ``use`` declaration can name several classes, each its own file."""- from .extractors.bindings.php import php_use_clauses+ from .extractors.bindings.php import php_class_name, php_use_clausesfrom .models import NamedBindingreturn [Import(raw_statement=raw,module_path=fqn,- imported_names=[local],+ imported_names=[php_class_name(fqn)],is_relative=False,resolved_file=None,bindings=[NamedBinding(local_name=local, exported_name=fqn, source_file=None)],diff --git a/tests/unit/ingestion/test_php_resolver.py b/tests/unit/ingestion/test_php_resolver.pyindex cb155698b..897a9d219 100644--- a/tests/unit/ingestion/test_php_resolver.py+++ b/tests/unit/ingestion/test_php_resolver.py@@ -255,11 +255,13 @@ class TestUseDeclarations:pf = ASTParser().parse_file(fi, src)return [(i.module_path, i.imported_names) for i in pf.imports]+ # ``imported_names`` is the class as its file declares it; an alias is+ # the local name only, kept on the binding.def test_grouped_use_becomes_one_import_per_class(self) -> None:got = self._imports(b"<?php\nuse App\\Models\\{User, Http\\Post as P};\n")assert got == [("App\\Models\\User", ["User"]),- ("App\\Models\\Http\\Post", ["P"]),+ ("App\\Models\\Http\\Post", ["Post"]),]def test_function_and_const_imports_bind_no_class(self) -> None:@@ -271,4 +273,4 @@ class TestUseDeclarations:def test_comma_separated_use_keeps_every_clause(self) -> None:got = self._imports(b"<?php\nuse \\App\\X, App\\Y as Z;\n")- assert got == [("App\\X", ["X"]), ("App\\Y", ["Z"])]+ assert got == [("App\\X", ["X"]), ("App\\Y", ["Y"])]diff --git a/tests/unit/ingestion/test_php_same_namespace.py b/tests/unit/ingestion/test_php_same_namespace.pyindex 7bc276abf..64abf2e30 100644--- a/tests/unit/ingestion/test_php_same_namespace.py+++ b/tests/unit/ingestion/test_php_same_namespace.py@@ -131,7 +131,7 @@ def test_relative_qualified_names_resolve_from_the_namespace_or_an_alias() -> No)target = "src/View/Concerns/CompilesViews.php"assert graph.get_edge_data("src/View/Compiler.php", target)["hint_source"] == QUALIFIED_NAME_HINT- assert graph.has_edge("src/Other.php", target)+ assert graph.get_edge_data("src/Other.php", target)["imported_names"] == ["CompilesViews"]assert graph.number_of_edges() == 2@@ -159,6 +159,8 @@ def test_fully_qualified_reference_from_a_global_file() -> None:)edge = graph.get_edge_data("config/auth.php", "app/Actions/Login.php")assert edge is not None and edge["hint_source"] == QUALIFIED_NAME_HINT+ # The class as its file declares it, which is what dead code matches.+ assert edge["imported_names"] == ["Login"]assert graph.number_of_edges() == 1