================================================================================
BASELINE COMPLEXITY METRICS - install_hooks.py Target Functions
================================================================================
Analysis Date: 2026-01-10
┌─────────────────────────────────────────────────────────────────────────────┐
│ SUMMARY COMPARISON TABLE │
├────────────────────┬────────────────┬────────────────┬────────────────────┤
│ Metric │ detect_claude │ _parse_mcp │ validate_mcp │
│ │ _config │ _get_output │ _prerequisites │
├────────────────────┼────────────────┼────────────────┼────────────────────┤
│ Priority │ P2 (Medium) │ P0 (CRITICAL) │ P1 (High) │
│ Lines │ 198-236 (39) │ 238-268 (31) │ 351-385 (35) │
│ Complexity (radon) │ B (9) │ C (12) 🔴 │ C (12) 🔴 │
│ Nesting Depth │ 5 levels 🔴 │ 9 levels 🔴🔴 │ 4 levels 🟡 │
│ If Statements │ 3 │ 7 │ 8 │
│ For Loops │ 1 │ 1 │ 0 │
│ Try/Except │ 4 │ 2 │ 0 │
│ Boolean Ops │ 4 ✅ │ 5 🟡 │ 12 🔴🔴 │
│ Security Issues │ 2 (LOW) │ 0 │ 0 │
│ Target Complexity │ ≤6 (A) │ ≤6 (A) │ ≤5 (A) │
│ Target Nesting │ ≤3 levels │ ≤3 levels │ ≤3 levels │
│ Estimated Effort │ 1-2 hours │ 2-3 hours │ 2-3 hours │
└────────────────────┴────────────────┴────────────────┴────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│ FILE-LEVEL METRICS │
├────────────────────────────────────────┬────────────────┬──────────────────┤
│ Metric │ Current │ Target │
├────────────────────────────────────────┼────────────────┼──────────────────┤
│ Total LOC │ 1454 │ - │
│ Maintainability Index │ C (2.38) 🔴 │ B (≥5.0) ✅ │
│ Average Complexity │ B (7.7) 🟡 │ A (≤5.0) ✅ │
│ Worst Function │ main() F (42) │ (not in scope) │
└────────────────────────────────────────┴────────────────┴──────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│ CRITICAL ISSUES │
├─────────────────────────────────────────────────────────────────────────────┤
│ 1. _parse_mcp_get_output() - 9 NESTING LEVELS 🔴🔴 │
│ - Worst offender in entire file │
│ - Try → For → If → Elif chain │
│ - IMMEDIATE refactoring required │
│ │
│ 2. validate_mcp_prerequisites() - 12 BOOLEAN OPERATORS 🔴🔴 │
│ - Hard to test │
│ - Complex conditional logic │
│ - Multiple concerns mixed │
│ │
│ 3. detect_claude_mcp_configuration() - 4 TRY/EXCEPT BLOCKS 🟡 │
│ - Over-engineered exception handling │
│ - Mixed concerns (detection + parsing + fallback) │
└─────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│ EXPECTED IMPROVEMENTS │
├─────────────────────────────────────────────────────────────────────────────┤
│ ✅ Readability: 67% reduction in nesting (9 → 3 levels) │
│ ✅ Testability: 58% reduction in boolean complexity (12 → <5) │
│ ✅ Maintainability: File MI improves C (2.38) → B (5.0+) │
│ ✅ Security: Eliminate 2 LOW severity subprocess issues │
│ ✅ Performance: No expected changes (pure refactoring) │
└─────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│ VALIDATION COMMANDS │
├─────────────────────────────────────────────────────────────────────────────┤
│ # Check cyclomatic complexity │
│ source .venv-analysis/bin/activate │
│ radon cc claude-hooks/install_hooks.py -s -a │
│ │
│ # Check maintainability index │
│ radon mi claude-hooks/install_hooks.py -s │
│ │
│ # Check nesting depth (custom) │
│ python3 /tmp/analyze_functions.py │
│ │
│ # Security scan │
│ bandit -r claude-hooks/install_hooks.py │
│ │
│ # Compare before/after │
│ diff .metrics/baseline_cc_install_hooks.txt \ │
│ .metrics/after_cc_install_hooks.txt │
└─────────────────────────────────────────────────────────────────────────────┘
================================================================================
REFACTORING ORDER (RECOMMENDED)
================================================================================
Phase 1: _parse_mcp_get_output() - P0 CRITICAL
├─ Extract field parser dictionary
├─ Replace if/elif with lookup table
├─ Reduce nesting 9 → 3 levels
└─ Expected: C (12) → A (4), Nesting 9 → 3
Time: 2-3 hours
Phase 2: validate_mcp_prerequisites() - P1 HIGH
├─ Extract validation rules
├─ Create validator classes
├─ Reduce boolean complexity 12 → 3
└─ Expected: C (12) → A (5), Boolean ops 12 → 3
Time: 2-3 hours
Phase 3: detect_claude_mcp_configuration() - P2 MEDIUM
├─ Extract subprocess helpers
├─ Simplify exception handling
├─ Reduce nesting 5 → 3 levels
└─ Expected: B (9) → A (5), Nesting 5 → 3
Time: 1-2 hours
TOTAL ESTIMATED EFFORT: 5-8 hours
================================================================================
SUCCESS CRITERIA CHECKLIST
================================================================================
Function-Level:
[ ] _parse_mcp_get_output: Complexity ≤6, Nesting ≤3
[ ] validate_mcp_prerequisites: Complexity ≤5, Boolean ops <5
[ ] detect_claude_mcp_configuration: Complexity ≤6, Nesting ≤3
File-Level:
[ ] Maintainability Index ≥5.0 (B grade or better)
[ ] Average Complexity ≤5.0 (A grade)
[ ] No new security issues introduced
[ ] All existing tests pass
[ ] Test coverage maintained or increased
================================================================================