Skip to content

refactor(cli): split EnrichCommand.enrichFromCache to drop cognitive complexity (RAN-41)#73

Merged
aksOps merged 1 commit into
mainfrom
fix/ran-41-enrich-complexity
Apr 25, 2026
Merged

refactor(cli): split EnrichCommand.enrichFromCache to drop cognitive complexity (RAN-41)#73
aksOps merged 1 commit into
mainfrom
fix/ran-41-enrich-complexity

Conversation

@aksOps
Copy link
Copy Markdown
Contributor

@aksOps aksOps commented Apr 24, 2026

Summary

Refactor EnrichCommand.enrichFromCache() from a 313-line method (cognitive complexity 94, S3776 limit 15) into a thin orchestrator + small phase helpers, so any future edit in this file keeps the SonarCloud new-code quality gate green.

The S3776 finding was surfaced on PR #71 (RAN-23) when SpotBugs cleanup re-classified enrichFromCache lines as new code.

What changed

enrichFromCache() is now an orchestrator (~10 lines, complexity ~1). Each phase lives in its own private helper, with identical ordering, log lines, exit codes, and side effects:

  • applyGraphDirOverride, runLinkerPhase, runClassifierAndEnrichers, runServiceDetection
  • bulkLoadIntoNeo4j (top-level Neo4j) → clearGraph, bulkLoadNodes, createPrimaryIndex, createStubNodesForExternalRefs (+ flushStubBatches/writeStubBatch), buildValidEdgeMaps/toEdgeProps, bulkLoadEdges, createSecondaryIndexes, printCompletionSummary, shutdownQuietly
  • Shared logStepDone for the " Done in Xms" line
  • Magic numbers extracted to named constants (NODE_BATCH_SIZE, EDGE_BATCH_SIZE, STUB_BATCH_SIZE, CLEAR_BATCH_SIZE, PROGRESS_REPORT_INTERVAL, INDEX_AWAIT_SECONDS)
  • java.util.*, ServiceDetector, ObjectMapper references promoted to imports
  • Narrowed Jackson catch from Exception to JsonProcessingException

No behavior change. Determinism preserved (same buffered builder, same flush + flushDeferred sequence, same node/edge ordering).

Verification

  • mvn test -Dskip.npm=true3395 tests, 0 failures, 0 errors, 31 skipped
  • mvn spotbugs:check0 findings
  • EnrichCommandTest (4 tests) → green
  • All extracted methods score well below the S3776 threshold of 15 (orchestrator ~1, largest helper createStubNodesForExternalRefs ~11)

Test plan

  • EnrichCommand unit tests pass (4)
  • Full mvn test passes (3395)
  • SpotBugs clean
  • SonarCloud PR analysis: EnrichCommand.java reports 0 new S3776 findings

Closes RAN-41.

🤖 Generated with Claude Code

… (RAN-41)

The 313-line enrichFromCache method scored cognitive complexity 94 on
SonarCloud (S3776, allowed 15) when PR #71 (RAN-23) re-classified it as
new code. Even small future edits in this file would re-trip the
new-code quality gate.

Extracted phases into private helpers, preserving exact ordering,
side effects, log lines, exit codes, and determinism:

  - applyGraphDirOverride        — --graph option handling
  - runLinkerPhase               — GraphBuilder + cross-file linkers
  - runClassifierAndEnrichers    — layer / lexical / language enrichment
  - runServiceDetection          — ServiceDetector + node/edge merge
  - bulkLoadIntoNeo4j            — top-level Neo4j orchestration
    - clearGraph                 — batched DETACH DELETE
    - bulkLoadNodes              — UNWIND CREATE in NODE_BATCH_SIZE batches
    - createPrimaryIndex         — id index + awaitIndexes
    - createStubNodesForExternalRefs / flushStubBatches / writeStubBatch
    - buildValidEdgeMaps / toEdgeProps
    - bulkLoadEdges              — UNWIND MATCH/CREATE batches
    - createSecondaryIndexes     — kind/layer/module/path/fulltext indexes
    - printCompletionSummary
    - shutdownQuietly            — finally-block dbms shutdown
  - logStepDone                  — shared "Done in Xms" line

Also lifted magic numbers (batch sizes, await timeout, progress interval,
stub-merge Cypher) into named constants and replaced fully-qualified
references (java.util.HashSet, ServiceDetector, ObjectMapper) with
imports for readability.

After refactor each method's cognitive complexity is well under 15
(orchestrator at ~1, largest helper at ~11).

Verification:
- mvn test -Dskip.npm=true → 3395 tests, 0 failures, 0 errors
- mvn spotbugs:check       → 0 findings
- EnrichCommandTest (4)    → still green

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Paperclip <noreply@paperclip.ing>
@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
76.4% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@aksOps aksOps merged commit b6648c0 into main Apr 25, 2026
7 of 8 checks passed
@aksOps aksOps deleted the fix/ran-41-enrich-complexity branch April 25, 2026 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant