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(refactoring): stop extract_class splitting data holders, core-plus-offshoot classes and trait impls (#2973)
Raghav Chamadiya8973090d4f08
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py b/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.pyindex efa2a73f9..f9c4732a9 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py@@ -13,6 +13,10 @@ a field declared in the class body or to a sibling method, and that noparameter or local of the method rebinds. A C# ``partial`` class is neverscored, because its other parts live in files this pass does not see, and inthose languages only components that hold state count (``_stateful_groups``).+A Rust trait impl is not scored either: its methods are the trait's contract.+A member a class only calls and never reads or writes (``self.x()`` on an+inherited, abstract or trait-provided method) links the methods that call it+but is not counted as a field."""from __future__ import annotations@@ -25,6 +29,7 @@ from .ast_utils import _IDENTIFIER_SUFFIX, _find_name, is_function_node, is_misrfrom .languages import LanguageNodeMap, get_language_mapfrom .models import ClassComplexity, CohesionGroup, FunctionComplexityfrom .nloc import CodeLineIndex+from .signature import is_trait_implif TYPE_CHECKING:from collections.abc import Iterator@@ -52,13 +57,14 @@ class _ClassBody(NamedTuple):class _MemberRefs(NamedTuple):- """Each method's member references, the methods the class body only- declares (a C++ method defined out of line: a call target, never a field),+ """Each method's member references, the referenced members that are+ methods rather than fields (a C++ method declared here and defined out of+ line, a ``self.x()`` the class only calls: call targets, never state),and the indices of the methods that are contracts rather than behaviour(constructors and overrides)."""per_method: list[set[str]]- out_of_line: frozenset[str]+ call_targets: frozenset[str]contracts: frozenset[int] = frozenset()@@ -255,17 +261,22 @@ def _self_member_name(node: Node, lmap: LanguageNodeMap) -> str | None:return prop.text.decode("utf-8", errors="replace")-def _collect_self_members(method_node: Node, lmap: LanguageNodeMap) -> set[str]:- """Set of instance-member names referenced by *method_node*.+def _is_callee(node: Node, lmap: LanguageNodeMap) -> bool:+ """Whether *node* is the function a call invokes (``self.x()``)."""+ parent = node.parent+ if parent is None or parent.type not in lmap.call_kinds:+ return False+ function = parent.child_by_field_name("function")+ return function is not None and function.id == node.id+++def _self_member_nodes(method_node: Node, lmap: LanguageNodeMap) -> Iterator[tuple[Node, str]]:+ """Every ``self.member`` access in *method_node* with its member name.Walks the method body (descending through nested functions/lambdas,which close over the same instance) but stops at nested class- definitions. Both field reads and method calls reduce to a member- name here — both are evidence two methods touch the same thing.+ definitions."""- members: set[str] = set()- if not lmap.self_identifiers or not lmap.member_access_kinds:- return membersstack: list[Node] = list(method_node.children)while stack:node = stack.pop()@@ -274,10 +285,38 @@ def _collect_self_members(method_node: Node, lmap: LanguageNodeMap) -> set[str]:if node.type in lmap.member_access_kinds:name = _self_member_name(node, lmap)if name:- members.add(name)- for child in node.children:- stack.append(child)- return members+ yield node, name+ stack.extend(node.children)+++def _collect_self_members(+ method_node: Node, lmap: LanguageNodeMap+) -> tuple[set[str], set[str]]:+ """Instance-member names *method_node* references, and the subset it+ reads or writes rather than only calls.++ Both field reads and method calls reduce to a member name here: both+ are evidence two methods touch the same thing.+ """+ if not lmap.self_identifiers or not lmap.member_access_kinds:+ return set(), set()+ accesses = list(_self_member_nodes(method_node, lmap))+ members = {name for _, name in accesses}+ read = {name for node, name in accesses if not _is_callee(node, lmap)}+ return members, read+++def _explicit_refs(method_nodes: list[Node], lmap: LanguageNodeMap) -> _MemberRefs:+ """Each method's ``self.x`` references. A member no method reads or+ writes, only calls, is a method the class gets from elsewhere (a base+ class, an abstract declaration, a trait default): a call target, never+ this class's state. Without field declarations a callback field that is+ only ever invoked reads the same way; that can merge or drop a split,+ never invent one."""+ pairs = [_collect_self_members(node, lmap) for node in method_nodes]+ referenced: set[str] = set().union(*(members for members, _ in pairs))+ read: set[str] = set().union(*(names for _, names in pairs))+ return _MemberRefs([members for members, _ in pairs], frozenset(referenced - read))def _foreign_member_id(node: Node, lmap: LanguageNodeMap) -> int | None:@@ -382,9 +421,9 @@ def _class_member_refs(it. A static method has no instance: a bare name in it (an objectinitializer in a factory) is never this object's state."""- explicit = [_collect_self_members(node, lmap) for node in body.methods]+ explicit = _explicit_refs(body.methods, lmap)if not lmap.field_decl_kinds:- return _MemberRefs(explicit, frozenset())+ return explicitfields: set[str] = set()out_of_line: set[str] = set()named_decls: list[tuple[Node, list[str]]] = []@@ -397,10 +436,12 @@ def _class_member_refs(links = _member_links(named_decls, body.nested, lmap, declared)per_method = [_method_refs(node, members, lmap, declared, links)- for node, members in zip(body.methods, explicit, strict=True)+ for node, members in zip(body.methods, explicit.per_method, strict=True)]+ # A declared field may be read by bare name where ``this->f()`` only calls it.+ call_targets = out_of_line | (explicit.call_targets - fields)return _MemberRefs(- per_method, frozenset(out_of_line), _contract_indices(body.methods, method_fcs, class_name)+ per_method, frozenset(call_targets), _contract_indices(body.methods, method_fcs, class_name))@@ -444,10 +485,17 @@ def _stateful_groups(return [groupfor idxs, group in indexed_groups- if len(group.fields) >= 2 and not contracts.issuperset(idxs)+ if holds_state(group) and not contracts.issuperset(idxs)]+def holds_state(group: CohesionGroup) -> bool:+ """Whether a cohesion component is state of its own: two or more fields.+ Accessors, setters and counters over one field are how a data class or a+ builder exposes its state, not a class to extract."""+ return len(group.fields) >= 2++def _tcc(field_sets: list[set[str]]) -> float:"""Tight Class Cohesion (Bieman-Kang): the fraction of method pairs thatshare at least one instance field, in ``[0, 1]``.@@ -530,6 +578,7 @@ def _indexed_groups(method_fcs: list[FunctionComplexity],members_per_method: list[set[str]],method_names: set[str],+ call_targets: frozenset[str],) -> list[tuple[list[int], CohesionGroup]]:"""The per-component membership behind the LCOM4 integer.@@ -537,7 +586,8 @@ def _indexed_groups(position, ``(start_line, name)`` rather than collection index, so thesplit reads top-to-bottom and is stable across runs. Each group keeps itsmethod indices, which stay correct when method names repeat. Its fields- are the members its methods reference that aren't method names.+ are the members its methods reference that aren't method names; the+ call targets they reach are kept apart as its calls."""by_root: dict[int, list[int]] = {}for i, root in enumerate(roots):@@ -546,14 +596,16 @@ def _indexed_groups(def _pos(i: int) -> tuple[int, str]:return (method_fcs[i].start_line, method_fcs[i].name)+ # A call to a sibling method is an edge, not a use of something inherited.+ outside_calls = call_targets - {fc.name for fc in method_fcs}indexed: list[tuple[list[int], CohesionGroup]] = []for member_idxs in by_root.values():member_idxs.sort(key=_pos)- group_fields: set[str] = set()- for i in member_idxs:- group_fields |= members_per_method[i] - method_names+ referenced: set[str] = set().union(*(members_per_method[i] for i in member_idxs))group = CohesionGroup(- methods=[method_fcs[i].name for i in member_idxs], fields=sorted(group_fields)+ methods=[method_fcs[i].name for i in member_idxs],+ fields=sorted(referenced - method_names),+ calls=tuple(sorted(referenced & outside_calls)),)indexed.append((member_idxs, group))indexed.sort(key=lambda pair: _pos(pair[0][0]))@@ -590,9 +642,9 @@ def _compute_lcom4(if not method_nodes:return 1, 0, [], 1.0if refs is None:- refs = _MemberRefs([_collect_self_members(n, lmap) for n in method_nodes], frozenset())+ refs = _explicit_refs(method_nodes, lmap)members_per_method = refs.per_method- method_names = {fc.name for fc in method_fcs} | refs.out_of_line+ method_names = {fc.name for fc in method_fcs} | refs.call_targetsall_members: set[str] = set().union(*members_per_method)field_count = len(all_members - method_names)@@ -605,7 +657,9 @@ def _compute_lcom4(implicit = bool(lmap.field_decl_kinds)roots = _component_roots(members_per_method, method_fcs, implicit)- indexed = _indexed_groups(roots, method_fcs, members_per_method, method_names)+ indexed = _indexed_groups(+ roots, method_fcs, members_per_method, method_names, refs.call_targets+ )if implicit:groups = _stateful_groups(indexed, refs.contracts)return max(len(groups), 1), field_count, groups, tcc@@ -638,7 +692,10 @@ def _collect_classes(# Keep nodes and FCs aligned (a method missing from the function# pass — unusual — drops out of both).body = body._replace(methods=[m for m in body.methods if m.id in fc_by_node_id])- if "partial" in _decl_words(class_node):+ # A partial class's other parts live in files this pass does not see;+ # a trait impl's methods are the trait's, so none can move out.+ contract_impl = is_trait_impl(class_node, lmap)+ if "partial" in _decl_words(class_node) or contract_impl:lcom4, field_count, components, tcc = 1, 0, [], 1.0else:refs = _class_member_refs(body, method_fcs, lmap, _class_name(class_node))@@ -658,6 +715,7 @@ def _collect_classes(field_count=field_count,components=components,tcc=tcc,+ contract_impl=contract_impl,))return classesdiff --git a/packages/core/src/repowise/core/analysis/health/complexity/models.py b/packages/core/src/repowise/core/analysis/health/complexity/models.pyindex ad179b9e5..cd122d308 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/models.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/models.py@@ -143,11 +143,14 @@ class CohesionGroup:by the Extract Class refactoring detector — when a class has``lcom4 >= 2`` each group is a candidate extracted class. ``methods``and ``fields`` are stable-sorted (by first appearance / name) so the- same class yields the same split across runs.+ same class yields the same split across runs. ``calls`` are the members+ the cluster only calls (a base-class, abstract or trait-provided method):+ a use of the class, not state of its own."""methods: list[str]fields: list[str]+ calls: tuple[str, ...] = ()@dataclass@@ -189,6 +192,9 @@ class ClassComplexity:# safety valve. A cohesive Extract Class split raises the worst split# class's TCC toward ``1``; the enrich self-check reads it before/after.tcc: float = 1.0+ # Every method is fixed by a contract the class implements (a Rust+ # ``impl Trait for T``): none can move out, and cohesion is not scored.+ contract_impl: bool = False@dataclass(frozen=True)diff --git a/packages/core/src/repowise/core/analysis/health/complexity/signature.py b/packages/core/src/repowise/core/analysis/health/complexity/signature.pyindex 5656e835a..e896dd55e 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/signature.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/signature.py@@ -113,13 +113,19 @@ def is_constructor(fn_node: Node, name: str, lmap: LanguageNodeMap) -> bool:return name == _enclosing_type_name(fn_node)+def is_trait_impl(node: Node, lmap: LanguageNodeMap) -> bool:+ """Whether *node* implements a trait (Rust ``impl Trait for T``): its+ members are the trait's, not its own."""+ return node.type in lmap.trait_impl_kinds and node.child_by_field_name("trait") is not None++def _in_trait_impl(fn_node: Node, lmap: LanguageNodeMap) -> bool:node = fn_node.parentwhile node is not None and node.type not in lmap.trait_impl_kinds:if node.type in lmap.function_kinds:return Falsenode = node.parent- return node is not None and node.child_by_field_name("trait") is not None+ return node is not None and is_trait_impl(node, lmap)def _is_published(node: Node, lmap: LanguageNodeMap) -> bool:diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/extract_class.py b/packages/core/src/repowise/core/analysis/health/refactoring/extract_class.pyindex c9b738e0c..977543cb5 100644--- a/packages/core/src/repowise/core/analysis/health/refactoring/extract_class.py+++ b/packages/core/src/repowise/core/analysis/health/refactoring/extract_class.py@@ -19,20 +19,23 @@ cohesive classes, so the gate demands genuine state to partition:- the class is one the cohesion biomarker already flags (``lcom4 >= 2``,``method_count >= _MIN_METHODS``), so the list never exceeds what healthsurfaces;-- at least two *field-bearing* groups — the state splits into ≥2 independent- stateful clusters, not "one core + loose helpers";+- at least two groups that are a class of their own: state (``holds_state``,+ two or more fields) and behaviour (two or more methods), so the class+ splits into independent clusters, not "one core + loose helpers".+ Accessors, setters or a counter over one field are how a data class or+ builder exposes its state, and one method is a method, not a class;- a minimum field density (fields per method) — this rejects statelessstrategy / dialect classes (many independent predicate methods, almost noshared state) that LCOM4 over-fires on but that should never be "split".-The displayed plan keeps only the substantive groups (field-bearing or-multi-method); lone fieldless helper methods don't constitute their own-extracted class and are dropped from the split so the plan reads honestly.+The displayed plan keeps only those stateful groups; the rest stay with the+class."""from __future__ import annotations-from ..complexity.class_analysis import cohesion_applies+from ..complexity.class_analysis import cohesion_applies, holds_state+from ..complexity.models import CohesionGroupfrom .models import RefactoringContext, RefactoringSuggestionfrom .registry import RefactoringDetector, effort_bucket, register@@ -54,13 +57,10 @@ _MIN_METHODS = 5_MIN_FIELD_DENSITY = 0.4-def _is_substantive(group: object) -> bool:- """A group worth extracting on its own: ≥2 methods, or ≥1 method that- touches ≥1 field. Filters out lone, fieldless helper methods so a class- isn't sold as an N-way split when it is really "one class + loners"."""- methods = getattr(group, "methods", [])- fields = getattr(group, "fields", [])- return len(methods) >= 2 or (len(methods) >= 1 and len(fields) >= 1)+def _is_extractable(group: CohesionGroup) -> bool:+ """A group that could stand as its own class: it holds state and has+ more than one method."""+ return holds_state(group) and len(group.methods) >= 2@register@@ -83,12 +83,7 @@ class ExtractClassDetector(RefactoringDetector):if field_density < _MIN_FIELD_DENSITY:continue# The split must partition real state into ≥2 stateful clusters.- field_bearing = [g for g in components if getattr(g, "fields", None)]- if len(field_bearing) < 2:- continue- # Present only the substantive groups; lone fieldless helpers don't- # form their own class. Re-check we still have a ≥2-way split.- substantive = [g for g in components if _is_substantive(g)]+ substantive = [g for g in components if _is_extractable(g)]if len(substantive) < 2:continuediff --git a/packages/core/src/repowise/core/analysis/health/refactoring/move_method.py b/packages/core/src/repowise/core/analysis/health/refactoring/move_method.pyindex 795187594..6a5a0796d 100644--- a/packages/core/src/repowise/core/analysis/health/refactoring/move_method.py+++ b/packages/core/src/repowise/core/analysis/health/refactoring/move_method.py@@ -215,17 +215,30 @@ def _is_target(graph: Any, class_id: str, accessed: set[str], home: set[str]) ->def _uses_own_state(classes: list[Any], parent: str, name: str, line: int | None) -> bool:- """Whether the method shares a cohesion component that holds fields with- the rest of its class. The ``calls`` graph sees no field reads; the- components do (``ClassComplexity.components``)."""- own = [- cls- for cls in classes- if getattr(cls, "name", None) == parent- and (line is None or cls.start_line <= line <= cls.end_line)- ]- groups = [g for cls in own[:1] for g in getattr(cls, "components", None) or ()]- return any(name in g.methods and g.fields for g in groups)+ """Whether the method is bound to its class: its class implements a+ contract that fixes every method (a Rust trait impl), or it shares a+ cohesion component that holds fields, or calls an inherited or abstract+ member, with the rest of its class. The ``calls`` graph sees none of+ these; the class analysis does (``ClassComplexity``)."""+ own = next(+ (+ cls+ for cls in classes+ if getattr(cls, "name", None) == parent+ and (line is None or cls.start_line <= line <= cls.end_line)+ ),+ None,+ )+ return own is not None and _binds_method(own, name)+++def _binds_method(cls: Any, name: str) -> bool:+ """Whether *cls* holds method *name* in place: a contract impl, or a+ cohesion component with fields or outside calls that contains it."""+ if getattr(cls, "contract_impl", False):+ return True+ groups = getattr(cls, "components", None) or ()+ return any(name in g.methods and (g.fields or g.calls) for g in groups)def _owning_class_id(graph: Any, callee_id: str) -> str | None:diff --git a/tests/unit/health/test_refactoring_extract_class.py b/tests/unit/health/test_refactoring_extract_class.pyindex befc624be..b529e8c34 100644--- a/tests/unit/health/test_refactoring_extract_class.py+++ b/tests/unit/health/test_refactoring_extract_class.py@@ -97,6 +97,7 @@ def test_lcom4_returns_components_for_split_class():assert ["render_b", "render_b2", "render_b3"] in [list(k) for k in groups]for g in cls.components:assert g.fields # each cluster touches at least one field+ assert g.calls == () # sibling calls are edges, not outside callsdef test_cohesive_class_has_single_or_no_component():@@ -226,3 +227,135 @@ def test_confidence_high_for_strong_god_class():cls.method_count = 16det = ExtractClassDetector()assert det._confidence(cls) == "high"+++# ---- what is not a class to extract ----------------------------------------+++def _suggest(path: str, language: str, src: str) -> list[str]:+ fc = walk_file(path, language, src.encode())+ ctx = RefactoringContext(file_path=path, language=language, nloc=0, classes=fc.classes)+ return [s.target_symbol for s in ExtractClassDetector().detect(ctx)]+++def test_one_field_counters_and_accessors_are_not_split():+ # Three independent counters, each with its own accessors: a data holder.+ src = """+class Minter {+ private a = 0+ private b = 0+ private c = 0+ nextA() { return ++this.a }+ peekA() { return this.a }+ nextB() { return ++this.b }+ peekB() { return this.b }+ nextC() { return ++this.c }+ peekC() { return this.c }+}+"""+ assert _suggest("minter.ts", "typescript", src) == []+++def test_core_with_one_method_offshoots_is_not_split():+ src = """+class Core:+ def a(self):+ return self.x + self.y+ def b(self):+ return self.a() + self.x+ def c(self):+ return self.y + self.b()+ def d(self):+ return self.p + self.q+ def e(self):+ return self.r + self.s+"""+ assert _suggest("core.py", "python", src) == []+++def test_members_only_called_are_methods_not_state():+ # ``this.base*()`` come from the base class: calling them is no state, so+ # the class has no second stateful cluster to extract.+ src = """+class Adapter extends Base {+ private opts = {}+ read() { return this.baseParse(this.opts) && this.baseMode() }+ write() { return this.baseParse(this.opts) }+ stop() { return this.baseStop() && this.baseKill() }+ kill() { return this.baseKill() && this.baseStop() }+ list() { return this.baseStop() }+}+"""+ fc = walk_file("adapter.ts", "typescript", src.encode())+ assert {f for g in fc.classes[0].components for f in g.fields} == {"opts"}+ # Still a use of the class: move_method must not read the method as free.+ assert {c for g in fc.classes[0].components for c in g.calls} == {+ "baseKill", "baseMode", "baseParse", "baseStop"+ }+ assert _suggest("adapter.ts", "typescript", src) == []+++def test_python_called_only_members_are_not_fields_and_real_clusters_still_split():+ src = """+class Worker(Base):+ def parse(self):+ return self.a + self.a2 + self.log()+ def parse2(self):+ return self.parse() + self.a+ def render(self):+ return self.b + self.b2 + self.log()+ def render2(self):+ return self.render() * self.b+ def reset(self):+ self.b2 = self.log()+"""+ fc = walk_file("worker.py", "python", src.encode())+ cls = fc.classes[0]+ assert {f for g in cls.components for f in g.fields} == {"a", "a2", "b", "b2"}+ # ``log`` is only called: it links the clusters it reaches, it is not state.+ assert all("log" not in g.fields for g in cls.components)+++def test_two_clusters_one_calling_an_inherited_method_still_split():+ src = """+class Two extends Base {+ p1() { return this.a + this.a2 }+ p2() { return this.p1() + this.a }+ p3() { return this.a2 + this.p2() }+ r1() { return this.b + this.b2 + this.helper() }+ r2() { return this.r1() * this.b }+ r3() { return this.b2 + this.r2() }+}+"""+ assert _suggest("two.ts", "typescript", src) == ["Two"]+ fc = walk_file("two.ts", "typescript", src.encode())+ assert [sorted(g.fields) for g in fc.classes[0].components] == [["a", "a2"], ["b", "b2"]]+++def test_rust_trait_impl_is_not_scored():+ src = """+struct M { a: u8, b: u8, c: u8, d: u8 }++impl Matcher for M {+ fn f1(&self) -> u8 { self.a + self.b }+ fn f2(&self) -> u8 { self.a * self.b }+ fn f3(&self) -> u8 { self.c + self.d }+ fn f4(&self) -> u8 { self.c * self.d }+ fn f5(&self) -> u8 { self.f4() }+}++impl M {+ fn g1(&self) -> u8 { self.a + self.b }+ fn g2(&self) -> u8 { self.a * self.b }+ fn g3(&self) -> u8 { self.c + self.d }+ fn g4(&self) -> u8 { self.c * self.d }+ fn g5(&self) -> u8 { self.g4() }+}+"""+ fc = walk_file("lib.rs", "rust", src.encode())+ trait_impl, inherent = sorted(fc.classes, key=lambda c: c.start_line)+ assert (trait_impl.lcom4, trait_impl.components) == (1, [])+ assert trait_impl.contract_impl and not inherent.contract_impl+ # The inherent impl with the same two stateful clusters still splits.+ assert inherent.lcom4 == 2+ assert _suggest("lib.rs", "rust", src) == ["M"]diff --git a/tests/unit/health/test_refactoring_move_method.py b/tests/unit/health/test_refactoring_move_method.pyindex 9dcda18d2..ee16e7c9e 100644--- a/tests/unit/health/test_refactoring_move_method.py+++ b/tests/unit/health/test_refactoring_move_method.py@@ -378,3 +378,22 @@ def test_method_sharing_its_class_state_does_not_move():stateless = CohesionGroup(methods=["envious"], fields=[])cls.components = [stateless]assert len(_detect_java(_java_envy_graph(), classes=[cls])) == 1+++def test_method_calling_an_inherited_member_or_in_a_trait_impl_does_not_move():+ from repowise.core.analysis.health.complexity import ClassComplexity, CohesionGroup++ cls = ClassComplexity(+ name="C",+ start_line=1,+ end_line=40,+ method_count=2,+ total_nloc=30,+ methods=[],+ lcom4=1,+ components=[CohesionGroup(methods=["envious"], fields=[], calls=("base",))],+ )+ assert _detect_java(_java_envy_graph(), classes=[cls]) == []+ cls.components = []+ cls.contract_impl = True+ assert _detect_java(_java_envy_graph(), classes=[cls]) == []