debtmap 0.17.0

Code complexity and technical debt analyzer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
use crate::analyzers::typescript::parser::{detect_variant, parse_source};
use crate::analyzers::typescript::visitor::class_analysis::extract_classes;
use crate::analyzers::typescript::visitor::function_analysis::extract_functions;
use crate::analyzers::FileAnalyzer;
use crate::core::FunctionMetrics;
use crate::extraction::{ExtractedFileData, UnifiedFileExtractor};
use crate::organization::god_object::classifier::group_methods_by_responsibility;
use crate::organization::god_object::heuristics::{
    detect_from_content, fallback_god_object_heuristics, HEURISTIC_MAX_FUNCTIONS,
    HEURISTIC_MAX_LINES,
};
use crate::organization::god_object::scoring::calculate_god_object_score_weighted;
use crate::organization::god_object::{
    DetectionType, FunctionVisibilityBreakdown, GodObjectAnalysis, GodObjectConfidence,
    GodObjectThresholds, ModuleSplit, Priority, SplitAnalysisMethod,
};
use crate::priority::file_metrics::FileDebtMetrics;
use crate::risk::lcov::LcovData;
use anyhow::Result;
use std::collections::HashMap;
use std::path::Path;

/// Helper struct for complexity calculation results
struct ComplexityMetrics {
    total_complexity: u32,
    max_complexity: u32,
    avg_complexity: f64,
}

/// Helper struct for coverage calculation results
struct CoverageMetrics {
    coverage_percent: f64,
}

/// Helper struct for line calculation results
struct LineMetrics {
    total_lines: usize,
    uncovered_lines: usize,
}

fn is_javascript_like(path: &Path) -> bool {
    crate::core::Language::from_path(path).is_js_ts()
}

fn uses_extracted_god_object_analysis(path: &Path) -> bool {
    matches!(
        crate::core::Language::from_path(path),
        crate::core::Language::Rust | crate::core::Language::Python
    )
}

fn semantic_responsibility_groups(function_names: &[String]) -> HashMap<String, Vec<String>> {
    group_methods_by_responsibility(function_names)
        .into_iter()
        .filter(|(responsibility, methods)| responsibility != "unclassified" && !methods.is_empty())
        .collect()
}

fn build_semantic_splits(groups: &HashMap<String, Vec<String>>) -> Vec<ModuleSplit> {
    let mut splits: Vec<ModuleSplit> = groups
        .iter()
        .filter(|(_, methods)| methods.len() >= 2)
        .map(|(responsibility, methods)| ModuleSplit {
            suggested_name: format!("{}_module", responsibility.to_lowercase().replace(' ', "_")),
            methods_to_move: methods.clone(),
            structs_to_move: Vec::new(),
            responsibility: responsibility.clone(),
            estimated_lines: 0,
            method_count: methods.len(),
            warning: None,
            priority: if methods.len() > 10 {
                Priority::High
            } else {
                Priority::Medium
            },
            cohesion_score: Some(0.85),
            dependencies_in: Vec::new(),
            dependencies_out: Vec::new(),
            domain: responsibility.clone(),
            rationale: Some("Parser-derived function names share this responsibility".to_string()),
            method: SplitAnalysisMethod::MethodBased,
            severity: None,
            interface_estimate: None,
            classification_evidence: None,
            representative_methods: methods.iter().take(8).cloned().collect(),
            fields_needed: Vec::new(),
            trait_suggestion: None,
            behavior_category: Some(responsibility.clone()),
            core_type: None,
            data_flow: Vec::new(),
            suggested_type_definition: None,
            data_flow_stage: None,
            pipeline_position: None,
            input_types: Vec::new(),
            output_types: Vec::new(),
            merge_history: Vec::new(),
            alternative_names: Vec::new(),
            naming_confidence: None,
            naming_strategy: None,
            cluster_quality: None,
        })
        .collect();

    splits.sort_by(|a, b| {
        b.method_count
            .cmp(&a.method_count)
            .then_with(|| a.responsibility.cmp(&b.responsibility))
    });
    splits
}

fn build_js_visibility_breakdown(
    functions: &[crate::analyzers::typescript::types::JsFunctionMetrics],
) -> FunctionVisibilityBreakdown {
    functions
        .iter()
        .fold(FunctionVisibilityBreakdown::new(), |mut breakdown, func| {
            if func.is_exported {
                breakdown.public += 1;
            } else {
                breakdown.private += 1;
            }
            breakdown
        })
}

fn analyze_js_ts_god_object(path: &Path, content: &str) -> Option<GodObjectAnalysis> {
    if !is_javascript_like(path) {
        return None;
    }

    let language = crate::core::Language::from_path(path);
    let ast = parse_source(content, path, detect_variant(path)).ok()?;
    let functions = extract_functions(&ast, true);
    let production_functions: Vec<_> = functions.iter().filter(|func| !func.is_test).collect();
    let function_count = production_functions.len();
    let total_lines = content.lines().count();

    if function_count <= HEURISTIC_MAX_FUNCTIONS && total_lines <= HEURISTIC_MAX_LINES {
        return None;
    }

    let function_names: Vec<String> = production_functions
        .iter()
        .filter(|func| !func.name.is_empty())
        .map(|func| func.name.clone())
        .collect();
    let responsibility_groups = semantic_responsibility_groups(&function_names);
    let responsibility_method_counts: HashMap<String, usize> = responsibility_groups
        .iter()
        .map(|(name, methods)| (name.clone(), methods.len()))
        .collect();
    let mut responsibilities: Vec<String> = responsibility_groups.keys().cloned().collect();
    responsibilities.sort();

    let complexity_sum: u32 = production_functions
        .iter()
        .map(|func| func.cyclomatic)
        .sum();
    let avg_complexity = if function_count == 0 {
        0.0
    } else {
        complexity_sum as f64 / function_count as f64
    };
    let class_count = extract_classes(&ast).len();
    let thresholds = GodObjectThresholds::default();
    let responsibility_count = responsibilities.len();
    let god_object_score = calculate_god_object_score_weighted(
        function_count as f64,
        0,
        responsibility_count,
        total_lines,
        avg_complexity,
        &thresholds,
    );
    let confidence = crate::organization::god_object::classifier::determine_confidence(
        function_count,
        0,
        responsibility_count,
        total_lines,
        complexity_sum,
        &thresholds,
    );

    Some(GodObjectAnalysis {
        is_god_object: true,
        method_count: function_count,
        weighted_method_count: None,
        field_count: 0,
        responsibility_count,
        lines_of_code: total_lines,
        complexity_sum,
        god_object_score: god_object_score.max(0.0),
        recommended_splits: build_semantic_splits(&responsibility_groups),
        confidence: if confidence == GodObjectConfidence::NotGodObject {
            GodObjectConfidence::Possible
        } else {
            confidence
        },
        responsibilities,
        responsibility_method_counts,
        purity_distribution: None,
        module_structure: Some(match language {
            crate::core::Language::TypeScript => {
                crate::analysis::ModuleStructureAnalyzer::new_typescript()
                    .analyze_typescript_file(content, path)
            }
            _ => crate::analysis::ModuleStructureAnalyzer::new_javascript()
                .analyze_javascript_file(content, path),
        }),
        detection_type: if class_count == 0 {
            DetectionType::GodFile
        } else {
            DetectionType::GodModule
        },
        struct_name: None,
        struct_line: None,
        struct_location: None,
        visibility_breakdown: Some(build_js_visibility_breakdown(&functions)),
        domain_count: responsibility_count,
        domain_diversity: if function_count == 0 {
            0.0
        } else {
            responsibility_count as f64 / function_count as f64
        },
        struct_ratio: if function_count == 0 {
            0.0
        } else {
            class_count as f64 / function_count as f64
        },
        analysis_method: if responsibility_count == 0 {
            SplitAnalysisMethod::None
        } else {
            SplitAnalysisMethod::MethodBased
        },
        cross_domain_severity: None,
        domain_diversity_metrics: None,
        aggregated_entropy: None,
        aggregated_error_swallowing_count: None,
        aggregated_error_swallowing_patterns: None,
        layering_impact: None,
        anti_pattern_report: None,
        complexity_metrics: None,
        trait_method_summary: None,
    })
}

pub struct UnifiedFileAnalyzer {
    coverage_data: Option<LcovData>,
}

impl UnifiedFileAnalyzer {
    pub fn new(coverage_data: Option<LcovData>) -> Self {
        Self { coverage_data }
    }

    fn count_lines(content: &str) -> usize {
        content.lines().count()
    }

    /// Analyze god object using pre-extracted data (spec 202).
    ///
    /// Uses `ExtractedFileData` to avoid redundant parsing.
    fn analyze_god_object_from_extracted(
        &self,
        path: &Path,
        content: &str,
        extracted: &ExtractedFileData,
    ) -> (
        Option<crate::organization::GodObjectAnalysis>,
        Option<crate::organization::GodObjectType>,
    ) {
        let language = crate::core::Language::from_path(path);

        if language == crate::core::Language::Python {
            let analysis = self.analyze_god_object_from_data(path, content, extracted);
            if analysis.0.is_some() {
                return analysis;
            }
        }

        // Use the comprehensive god object detector for Rust files
        // The detector needs the AST, but we can check if extracted data indicates
        // a potential god object first to avoid unnecessary re-parsing for simple files
        if language == crate::core::Language::Rust {
            // Quick heuristic check using extracted data
            let method_count: usize = extracted.impls.iter().map(|i| i.methods.len()).sum();
            let field_count: usize = extracted.structs.iter().map(|s| s.fields.len()).sum();
            let total_lines = extracted.total_lines;

            // Only do full analysis if there's potential for god object
            // This avoids re-parsing simple files
            if method_count > 10 || field_count > 8 || total_lines > 500 {
                // For comprehensive god object analysis, we need the AST
                // Use UnifiedFileExtractor to get a fresh parse with SourceMap reset
                if let Ok(data) = UnifiedFileExtractor::extract(path, content) {
                    // Create analysis from extracted data
                    return self.analyze_god_object_from_data(path, content, &data);
                }
            }
        }

        // Fallback to simple heuristics for non-Rust or simple files
        self.analyze_god_object_simple(content)
    }

    /// Simple heuristics-based god object detection for non-Rust files or fallback.
    ///
    /// Uses shared heuristics from `organization::god_object::heuristics` (Spec 212).
    fn analyze_god_object_simple(
        &self,
        content: &str,
    ) -> (
        Option<crate::organization::GodObjectAnalysis>,
        Option<crate::organization::GodObjectType>,
    ) {
        // Delegate to shared heuristics module (Spec 212 consolidation)
        (detect_from_content(content), None)
    }

    /// Analyze god object from extracted data.
    ///
    /// Spec 212: Uses the extraction adapter as the single source of truth
    /// for god object detection.
    fn analyze_god_object_from_data(
        &self,
        path: &Path,
        _content: &str,
        extracted: &ExtractedFileData,
    ) -> (
        Option<crate::organization::GodObjectAnalysis>,
        Option<crate::organization::GodObjectType>,
    ) {
        // Use extraction adapter as single source of truth (Spec 212)
        let analysis = crate::extraction::adapters::god_object::analyze_god_object(path, extracted);

        // Note: GodObjectType classification is not available from the adapter
        // since it was tightly coupled to the AST-based analysis.
        // For most use cases, the GodObjectAnalysis.detection_type field provides
        // sufficient classification information.
        (analysis, None)
    }

    fn analyze_god_object(
        &self,
        path: &Path,
        content: &str,
    ) -> (
        Option<crate::organization::GodObjectAnalysis>,
        Option<crate::organization::GodObjectType>,
    ) {
        // Use extracted data for languages with a unified extractor.
        if uses_extracted_god_object_analysis(path) {
            // Try to use extracted data first (spec 202)
            if let Ok(extracted) = UnifiedFileExtractor::extract(path, content) {
                return self.analyze_god_object_from_extracted(path, content, &extracted);
            }
        }

        if let Some(analysis) = analyze_js_ts_god_object(path, content) {
            return (Some(analysis), None);
        }

        // Fallback to simple heuristics for non-Rust files
        self.analyze_god_object_simple(content)
    }

    fn get_file_coverage(&self, path: &Path) -> f64 {
        if let Some(ref coverage) = self.coverage_data {
            coverage.get_file_coverage(path).unwrap_or(0.0) / 100.0
        } else {
            0.0
        }
    }
}

impl UnifiedFileAnalyzer {
    /// Calculate complexity-related metrics for functions
    fn calculate_complexity_metrics(functions: &[FunctionMetrics]) -> ComplexityMetrics {
        let total_complexity: u32 = functions.iter().map(|f| f.cyclomatic).sum();
        let max_complexity = functions.iter().map(|f| f.cyclomatic).max().unwrap_or(0);
        let function_count = functions.len();
        let avg_complexity = if function_count > 0 {
            total_complexity as f64 / function_count as f64
        } else {
            0.0
        };

        ComplexityMetrics {
            total_complexity,
            max_complexity,
            avg_complexity,
        }
    }

    /// Calculate individual function scores based on complexity
    /// This provides a basic score based solely on function metrics
    fn calculate_function_scores(functions: &[FunctionMetrics]) -> Vec<f64> {
        functions
            .iter()
            .map(|func| {
                // Calculate a basic score based on complexity (0-10 scale)
                let complexity_score = (func.cyclomatic + func.cognitive) as f64 / 2.0;
                let length_penalty = if func.length > 50 { 2.0 } else { 1.0 };
                let nesting_penalty = if func.nesting > 3 { 1.5 } else { 1.0 };

                // Basic scoring: complexity * length_penalty * nesting_penalty
                // Clamped to 0-10 range
                (complexity_score * length_penalty * nesting_penalty).min(10.0)
            })
            .collect()
    }

    /// Estimate class count using simple heuristics
    fn estimate_class_count(functions: &[FunctionMetrics]) -> usize {
        functions
            .iter()
            .filter(|f| f.name.contains("::new") || f.name.contains("__init__"))
            .count()
    }

    /// Calculate coverage-related metrics
    fn calculate_coverage_metrics(
        &self,
        functions: &[FunctionMetrics],
        function_count: usize,
    ) -> CoverageMetrics {
        let coverage_percent = match &self.coverage_data {
            Some(coverage) => {
                let covered_functions = functions
                    .iter()
                    .filter(|f| {
                        coverage
                            .get_function_coverage(&f.file, &f.name)
                            .map(|c| c > 0.0)
                            .unwrap_or(false)
                    })
                    .count();
                covered_functions as f64 / function_count as f64
            }
            None => 0.0,
        };

        CoverageMetrics { coverage_percent }
    }

    /// Calculate line-related metrics including uncovered lines
    fn calculate_line_metrics(
        functions: &[FunctionMetrics],
        function_count: usize,
        coverage_percent: f64,
    ) -> LineMetrics {
        const OVERHEAD_LINES_PER_FUNCTION: usize = 5;

        let total_lines: usize = functions.iter().map(|f| f.length).sum::<usize>()
            + function_count * OVERHEAD_LINES_PER_FUNCTION;
        let uncovered_lines = ((1.0 - coverage_percent) * total_lines as f64) as usize;

        LineMetrics {
            total_lines,
            uncovered_lines,
        }
    }

    /// Detect god object patterns and calculate indicators.
    ///
    /// Uses shared heuristics from `organization::god_object::heuristics` (Spec 212).
    fn detect_god_object(
        function_count: usize,
        total_lines: usize,
    ) -> Option<crate::organization::GodObjectAnalysis> {
        // Delegate to shared heuristics module (Spec 212 consolidation)
        // Field count estimated as 0 when not available from AST
        fallback_god_object_heuristics(function_count, total_lines, 0, 0)
    }
}

impl FileAnalyzer for UnifiedFileAnalyzer {
    fn analyze_file(&self, path: &Path, content: &str) -> Result<FileDebtMetrics> {
        let total_lines = Self::count_lines(content);
        let (god_object_analysis, god_object_type) = self.analyze_god_object(path, content);
        let coverage_percent = self.get_file_coverage(path);
        let uncovered_lines = ((1.0 - coverage_percent) * total_lines as f64) as usize;

        // Classify file type for context-aware thresholds (spec 135)
        let file_type = Some(crate::organization::classify_file(content, path));

        Ok(FileDebtMetrics {
            path: path.to_path_buf(),
            total_lines,
            function_count: 0, // Will be filled by aggregate_functions
            class_count: 0,    // Will be filled by aggregate_functions
            avg_complexity: 0.0,
            max_complexity: 0,
            total_complexity: 0,
            coverage_percent,
            uncovered_lines,
            god_object_analysis,
            function_scores: Vec::new(),
            god_object_type,
            file_type,
            // Spec 201: File-level dependency metrics (populated during analysis aggregation)
            afferent_coupling: 0,
            efferent_coupling: 0,
            instability: 0.0,
            dependents: Vec::new(),
            dependencies_list: Vec::new(),
        })
    }

    fn aggregate_functions(&self, functions: &[FunctionMetrics]) -> FileDebtMetrics {
        if functions.is_empty() {
            return FileDebtMetrics::default();
        }

        let path = functions[0].file.clone();
        let function_count = functions.len();
        let complexity_metrics = Self::calculate_complexity_metrics(functions);
        let class_count = Self::estimate_class_count(functions);
        let coverage_metrics = self.calculate_coverage_metrics(functions, function_count);
        let line_metrics = Self::calculate_line_metrics(
            functions,
            function_count,
            coverage_metrics.coverage_percent,
        );

        // BUG FIX: Read file content to properly detect boilerplate patterns
        // This was missing - we need to analyze the actual file content to detect
        // boilerplate (like ripgrep's flags/defs.rs trait implementations)
        let (god_object_analysis, god_object_type) =
            if let Ok(content) = std::fs::read_to_string(&path) {
                self.analyze_god_object(&path, &content)
            } else {
                // Fallback to simple heuristics if we can't read the file
                let fallback_analysis =
                    Self::detect_god_object(function_count, line_metrics.total_lines);
                (fallback_analysis, None)
            };

        // Calculate individual function scores based on complexity
        let function_scores = Self::calculate_function_scores(functions);

        // Classify file type for context-aware thresholds (spec 135)
        let file_type = if let Ok(content) = std::fs::read_to_string(&path) {
            Some(crate::organization::classify_file(&content, &path))
        } else {
            None
        };

        FileDebtMetrics {
            path,
            total_lines: line_metrics.total_lines,
            function_count,
            class_count,
            avg_complexity: complexity_metrics.avg_complexity,
            max_complexity: complexity_metrics.max_complexity,
            total_complexity: complexity_metrics.total_complexity,
            coverage_percent: coverage_metrics.coverage_percent,
            uncovered_lines: line_metrics.uncovered_lines,
            god_object_analysis,
            function_scores,
            god_object_type,
            file_type,
            // Spec 201: File-level dependency metrics (populated during analysis aggregation)
            afferent_coupling: 0,
            efferent_coupling: 0,
            instability: 0.0,
            dependents: Vec::new(),
            dependencies_list: Vec::new(),
        }
    }
}

pub fn analyze_file_with_metrics(
    path: &Path,
    content: &str,
    functions: &[FunctionMetrics],
    coverage: Option<&LcovData>,
) -> Result<FileDebtMetrics> {
    let analyzer = UnifiedFileAnalyzer::new(coverage.cloned());

    // Get base file metrics
    let mut file_metrics = analyzer.analyze_file(path, content)?;

    // Aggregate function data
    let aggregated = analyzer.aggregate_functions(functions);

    // Merge the results
    file_metrics.function_count = aggregated.function_count;
    file_metrics.class_count = aggregated.class_count;
    file_metrics.avg_complexity = aggregated.avg_complexity;
    file_metrics.max_complexity = aggregated.max_complexity;
    file_metrics.total_complexity = aggregated.total_complexity;
    file_metrics.function_scores = aggregated.function_scores;

    // Override god object detection with aggregated data if more accurate
    if aggregated.function_count > 0 {
        file_metrics.god_object_analysis = aggregated.god_object_analysis;
    }

    Ok(file_metrics)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::FunctionMetrics;
    use std::path::PathBuf;

    fn create_test_function_metrics(name: &str, cyclomatic: u32, length: usize) -> FunctionMetrics {
        FunctionMetrics {
            name: name.to_string(),
            file: PathBuf::from("test.rs"),
            line: 1,
            cyclomatic,
            cognitive: cyclomatic, // Approximate cognitive complexity
            nesting: 1,
            length,
            is_test: false,
            visibility: None,
            is_trait_method: false,
            in_test_module: false,
            entropy_score: None,
            is_pure: None,
            purity_confidence: None,
            purity_reason: None,
            call_dependencies: None,
            detected_patterns: None,
            upstream_callers: None,
            downstream_callees: None,
            mapping_pattern_result: None,
            adjusted_complexity: None,
            composition_metrics: None,
            language_specific: None,
            purity_level: None,
            error_swallowing_count: None,
            error_swallowing_patterns: None,
            entropy_analysis: None,
        }
    }

    #[test]
    fn test_calculate_complexity_metrics() {
        let functions = vec![
            create_test_function_metrics("func1", 5, 20),
            create_test_function_metrics("func2", 10, 30),
            create_test_function_metrics("func3", 3, 15),
        ];

        let metrics = UnifiedFileAnalyzer::calculate_complexity_metrics(&functions);

        assert_eq!(metrics.total_complexity, 18);
        assert_eq!(metrics.max_complexity, 10);
        assert_eq!(metrics.avg_complexity, 6.0);
    }

    #[test]
    fn test_estimate_class_count() {
        let functions = vec![
            create_test_function_metrics("MyClass::new", 1, 10),
            create_test_function_metrics("AnotherClass::new", 1, 10),
            create_test_function_metrics("__init__", 1, 10),
            create_test_function_metrics("regular_function", 1, 10),
            create_test_function_metrics("another_regular", 1, 10),
        ];

        let class_count = UnifiedFileAnalyzer::estimate_class_count(&functions);
        assert_eq!(class_count, 3); // Two ::new and one __init__
    }

    #[test]
    fn test_calculate_coverage_metrics_with_data() {
        use crate::risk::lcov::LcovData;
        use std::collections::HashMap;

        use crate::risk::lcov::FunctionCoverage;

        let mut functions = HashMap::new();
        let function_coverages = vec![
            FunctionCoverage {
                name: "func1".to_string(),
                start_line: 1,
                execution_count: 10,
                coverage_percentage: 80.0,
                uncovered_lines: vec![2, 3],
                normalized: crate::risk::lcov::NormalizedFunctionName::simple("func1"),
            },
            FunctionCoverage {
                name: "func2".to_string(),
                start_line: 10,
                execution_count: 0,
                coverage_percentage: 0.0,
                uncovered_lines: vec![10, 11, 12, 13, 14],
                normalized: crate::risk::lcov::NormalizedFunctionName::simple("func2"),
            },
            FunctionCoverage {
                name: "func3".to_string(),
                start_line: 20,
                execution_count: 5,
                coverage_percentage: 50.0,
                uncovered_lines: vec![21, 22],
                normalized: crate::risk::lcov::NormalizedFunctionName::simple("func3"),
            },
        ];
        functions.insert(PathBuf::from("test.rs"), function_coverages);

        let mut coverage_data = LcovData {
            functions,
            total_lines: 100,
            lines_hit: 50,
            ..Default::default()
        };
        coverage_data.build_index(); // Rebuild index after modifying functions

        let analyzer = UnifiedFileAnalyzer::new(Some(coverage_data));
        let functions = vec![
            create_test_function_metrics("func1", 1, 10),
            create_test_function_metrics("func2", 1, 10),
            create_test_function_metrics("func3", 1, 10),
        ];

        let metrics = analyzer.calculate_coverage_metrics(&functions, 3);
        assert_eq!(metrics.coverage_percent, 2.0 / 3.0); // 2 out of 3 have coverage > 0
    }

    #[test]
    fn test_calculate_coverage_metrics_without_data() {
        let analyzer = UnifiedFileAnalyzer::new(None);
        let functions = vec![
            create_test_function_metrics("func1", 1, 10),
            create_test_function_metrics("func2", 1, 10),
        ];

        let metrics = analyzer.calculate_coverage_metrics(&functions, 2);
        assert_eq!(metrics.coverage_percent, 0.0);
    }

    #[test]
    fn test_calculate_line_metrics() {
        let functions = vec![
            create_test_function_metrics("func1", 1, 20),
            create_test_function_metrics("func2", 1, 30),
            create_test_function_metrics("func3", 1, 10),
        ];

        let metrics = UnifiedFileAnalyzer::calculate_line_metrics(&functions, 3, 0.6);

        // Total lines = 20 + 30 + 10 + 3*5 (overhead) = 75
        assert_eq!(metrics.total_lines, 75);
        // Uncovered lines = (1.0 - 0.6) * 75 = 30
        assert_eq!(metrics.uncovered_lines, 30);
    }

    #[test]
    fn test_detect_god_object() {
        // Test non-god object
        let normal_analysis = UnifiedFileAnalyzer::detect_god_object(20, 500);
        assert!(normal_analysis.is_none());

        // Test god object by function count
        let function_god = UnifiedFileAnalyzer::detect_god_object(60, 500);
        assert!(function_god.is_some());
        let function_god = function_god.unwrap();
        assert!(function_god.is_god_object);
        // Spec 212: Uses weighted scoring algorithm now
        assert!(function_god.god_object_score > 0.0);
        assert_eq!(function_god.method_count, 60);

        // Test god object by line count
        let line_god = UnifiedFileAnalyzer::detect_god_object(30, 2500);
        assert!(line_god.is_some());
        let line_god = line_god.unwrap();
        assert!(line_god.is_god_object);
        assert_eq!(line_god.method_count, 30);

        // Test extreme case - should produce a high score
        let extreme_god = UnifiedFileAnalyzer::detect_god_object(150, 5000);
        assert!(extreme_god.is_some());
        let extreme_god = extreme_god.unwrap();
        assert!(extreme_god.is_god_object);
        // Spec 212: Weighted scoring produces higher scores for severe violations
        assert!(extreme_god.god_object_score > function_god.god_object_score);
    }

    #[test]
    fn test_aggregate_functions_integration() {
        let analyzer = UnifiedFileAnalyzer::new(None);
        let functions = vec![
            create_test_function_metrics("MyClass::new", 3, 15),
            create_test_function_metrics("regular_func", 8, 25),
            create_test_function_metrics("another_func", 4, 20),
        ];

        let result = analyzer.aggregate_functions(&functions);

        assert_eq!(result.function_count, 3);
        assert_eq!(result.class_count, 1); // One ::new function
        assert_eq!(result.total_complexity, 15); // 3 + 8 + 4
        assert_eq!(result.max_complexity, 8);
        assert_eq!(result.avg_complexity, 5.0); // 15/3
        assert_eq!(result.total_lines, 75); // 15+25+20 + 3*5 overhead
        assert_eq!(result.coverage_percent, 0.0);
        assert_eq!(result.uncovered_lines, 75); // All uncovered
        assert!(result.god_object_analysis.is_none()); // Not a god object
        assert_eq!(result.function_scores.len(), 3);

        // Verify function scores are not all zeros
        assert!(
            result.function_scores.iter().any(|&score| score > 0.0),
            "Function scores should not all be zero"
        );
    }

    #[test]
    fn test_function_scores_calculation() {
        let functions = vec![
            create_test_function_metrics("simple_func", 2, 10), // Low complexity, short
            create_test_function_metrics("complex_func", 10, 100), // High complexity, long
            create_test_function_metrics("nested_func", 5, 30), // Medium complexity, medium length
        ];

        let scores = UnifiedFileAnalyzer::calculate_function_scores(&functions);

        assert_eq!(scores.len(), 3);

        // Simple function should have lower score
        assert!(scores[0] > 0.0);
        assert!(scores[0] < 5.0);

        // Complex function should have highest score (gets length penalty)
        assert!(scores[1] > scores[0]);
        assert!(scores[1] > scores[2]);

        // All scores should be in 0-10 range
        for score in scores {
            assert!(score >= 0.0);
            assert!(score <= 10.0);
        }
    }

    #[test]
    fn test_js_god_object_uses_semantic_responsibilities() {
        let mut content = String::new();
        for i in 0..20 {
            content.push_str(&format!("function parseThing{}() {{ return {}; }}\n", i, i));
        }
        for i in 0..20 {
            content.push_str(&format!(
                "function validateThing{}() {{ return true; }}\n",
                i
            ));
        }
        for i in 0..12 {
            content.push_str(&format!(
                "function renderThing{}() {{ return String({}); }}\n",
                i, i
            ));
        }

        let analyzer = UnifiedFileAnalyzer::new(None);
        let metrics = analyzer
            .analyze_file(Path::new("generator.js"), &content)
            .unwrap();
        let analysis = metrics.god_object_analysis.unwrap();

        assert!(analysis.is_god_object);
        assert_eq!(analysis.method_count, 52);
        assert!(analysis.responsibilities.contains(&"Parsing".to_string()));
        assert!(analysis
            .responsibilities
            .contains(&"Validation".to_string()));
        assert!(analysis.responsibilities.contains(&"Rendering".to_string()));
        assert!(!analysis
            .responsibilities
            .iter()
            .any(|name| name.starts_with("responsibility_")));
    }

    #[test]
    fn test_python_god_object_uses_semantic_responsibilities() {
        let mut content = String::from("class ReportGenerator:\n");
        for i in 0..20 {
            content.push_str(&format!("    def parse_thing_{}(self):\n", i));
            content.push_str(&format!("        return {}\n", i));
        }
        for i in 0..20 {
            content.push_str(&format!("    def validate_thing_{}(self):\n", i));
            content.push_str("        return True\n");
        }
        for i in 0..12 {
            content.push_str(&format!("    def render_thing_{}(self):\n", i));
            content.push_str(&format!("        return str({})\n", i));
        }

        let analyzer = UnifiedFileAnalyzer::new(None);
        let metrics = analyzer
            .analyze_file(Path::new("generator.py"), &content)
            .unwrap();
        let analysis = metrics.god_object_analysis.unwrap();

        assert!(analysis.is_god_object);
        assert_eq!(analysis.method_count, 52);
        assert!(analysis.responsibilities.contains(&"Parsing".to_string()));
        assert!(analysis
            .responsibilities
            .contains(&"Validation".to_string()));
        assert!(analysis.responsibilities.contains(&"Rendering".to_string()));
        assert!(!analysis
            .responsibilities
            .iter()
            .any(|name| name.starts_with("responsibility_")));
    }

    #[test]
    fn test_heuristic_fallback_does_not_invent_responsibilities() {
        let analysis = UnifiedFileAnalyzer::detect_god_object(60, 1000).unwrap();

        assert!(analysis.is_god_object);
        assert_eq!(analysis.responsibility_count, 0);
        assert!(analysis.responsibilities.is_empty());
        assert!(analysis.responsibility_method_counts.is_empty());
    }
}