repotoire 0.7.1

Graph-powered code analysis CLI. 110 detectors for security, architecture, bus factor, and code quality.
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
//! Data Clumps Detector
//!
//! Graph-aware detection of parameter groups that appear together across functions.
//! Uses function parameter data from the graph to identify missing abstractions.
//!
//! Enhanced with call graph analysis:
//! - Higher severity if functions with same params CALL each other (strong coupling)
//! - Lower severity if functions are in different modules with no call relationship
//! - Identifies refactoring opportunities where params travel through call chains

use crate::detectors::base::{Detector, DetectorConfig};
use crate::graph::GraphQueryExt;
use crate::models::{Finding, Severity};
use anyhow::Result;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use tracing::{debug, info};

/// Thresholds for data clumps detection
#[derive(Debug, Clone)]
pub struct DataClumpsThresholds {
    /// Minimum parameters to form a clump
    pub min_params: usize,
    /// Minimum functions sharing the clump
    pub min_occurrences: usize,
}

impl Default for DataClumpsThresholds {
    fn default() -> Self {
        Self {
            min_params: 4,
            min_occurrences: 5,
        }
    }
}

/// Framework convention parameter patterns to skip (forwarding/middleware signatures).
/// If the clump's param names are a subset of any pattern, it is not reported.
const FRAMEWORK_CONVENTIONS: &[&[&str]] = &[
    &["req", "res", "next"],
    &["ctx", "w", "r"],
    &["self", "other"],
    &["request", "response"],
    &["app", "req", "res"],
    &["t", "ctx"],
    &["db", "ctx"],
    &["conn", "ctx"],
];

/// Known parameter patterns and suggested names
fn suggest_struct_name(params: &[String]) -> String {
    let param_set: HashSet<&str> = params.iter().map(|s| s.as_str()).collect();

    // Check known patterns
    let patterns: &[(&[&str], &str)] = &[
        (&["x", "y"], "Point"),
        (&["x", "y", "z"], "Point3D"),
        (&["width", "height"], "Size"),
        (&["start", "end"], "Range"),
        (&["min", "max"], "Range"),
        (&["host", "port"], "Address"),
        (&["first_name", "last_name"], "Name"),
        (&["first_name", "last_name", "email"], "PersonInfo"),
        (&["latitude", "longitude"], "Coordinates"),
        (&["lat", "lng"], "Coordinates"),
        (&["red", "green", "blue"], "RGB"),
        (&["r", "g", "b"], "RGB"),
        (&["username", "password"], "Credentials"),
        (&["user", "password"], "Credentials"),
        (&["path", "filename"], "FilePath"),
        (&["name", "email"], "Contact"),
        (&["start_date", "end_date"], "DateRange"),
        (&["created_at", "updated_at"], "Timestamps"),
    ];

    for (pattern_params, name) in patterns {
        let pattern_set: HashSet<&str> = pattern_params.iter().copied().collect();
        if pattern_set.is_subset(&param_set) {
            return name.to_string();
        }
    }

    // Generate from first param
    if let Some(first) = params.first() {
        let base: String = first
            .split('_')
            .map(|w| {
                let mut c = w.chars();
                match c.next() {
                    None => String::new(),
                    Some(f) => f.to_uppercase().chain(c).collect(),
                }
            })
            .collect();
        return format!("{}Params", base);
    }

    "ParamGroup".to_string()
}

pub struct DataClumpsDetector {
    config: DetectorConfig,
    thresholds: DataClumpsThresholds,
}

impl DataClumpsDetector {
    pub fn new() -> Self {
        Self {
            config: DetectorConfig::new(),
            thresholds: DataClumpsThresholds::default(),
        }
    }

    pub fn with_config(config: DetectorConfig) -> Self {
        let thresholds = DataClumpsThresholds {
            min_params: config.get_option_or("min_params", 4),
            min_occurrences: config.get_option_or("min_occurrences", 5),
        };
        Self { config, thresholds }
    }

    /// Returns true if the given param names match (are a subset of) a framework convention pattern.
    fn is_framework_convention(params: &[String]) -> bool {
        let param_set: HashSet<&str> = params.iter().map(|s| s.as_str()).collect();
        for pattern in FRAMEWORK_CONVENTIONS {
            let pattern_set: HashSet<&str> = pattern.iter().copied().collect();
            if param_set.is_subset(&pattern_set) {
                return true;
            }
        }
        false
    }

    /// Extract parameter names from function's parameter property
    fn extract_params(
        &self,
        func: &crate::graph::CodeNode,
        graph: &dyn crate::graph::GraphQuery,
    ) -> Vec<String> {
        // Try to get params from extra_props side table
        let i = graph.interner();
        if let Some(params_key) = graph
            .extra_props(func.qualified_name)
            .and_then(|ep| ep.params)
        {
            let params_str = i.resolve(params_key);
            return params_str
                .split(',')
                .map(|p| p.trim().to_lowercase())
                .filter(|p| !p.is_empty() && !p.starts_with('_') && p != "self" && p != "this")
                .collect();
        }

        // Try param_count to see if function has parameters
        if func.param_count > 0 && (func.param_count as usize) >= self.thresholds.min_params {
            // We know it has params but can't extract names
            // Return empty - will need parser enhancement
        }

        vec![]
    }

    /// Find parameter clumps across functions using an inverted index.
    ///
    /// Instead of generating all C(N,k) parameter combinations (exponential),
    /// uses an inverted-index approach inspired by SourcererCC (arXiv:1512.06448):
    /// 1. Map each param → set of functions
    /// 2. For each pair sharing ≥1 param, compute intersection
    /// 3. Group by intersection, filter by min_occurrences
    fn find_clumps(&self, graph: &dyn crate::graph::GraphQuery) -> Vec<DataClump> {
        let i = graph.interner();
        let functions = graph.get_functions_shared();

        // Step 1: Extract params for qualifying functions
        let mut func_data: Vec<(FuncInfo, HashSet<String>)> = Vec::new();
        for func in functions.iter() {
            let params = self.extract_params(func, graph);
            if params.len() < self.thresholds.min_params {
                continue;
            }
            func_data.push((
                FuncInfo {
                    name: func.node_name(i).to_string(),
                    qualified_name: func.qn(i).to_string(),
                    file: func.path(i).to_string(),
                    line: func.line_start,
                },
                params.into_iter().collect(),
            ));
        }

        debug!(
            "DataClumps: {} functions with {}+ params",
            func_data.len(),
            self.thresholds.min_params
        );

        if func_data.len() < self.thresholds.min_occurrences {
            return vec![];
        }

        // Step 2: Build inverted index: param → Vec<func_index>
        let mut param_index: HashMap<&str, Vec<usize>> = HashMap::new();
        for (idx, (_, params)) in func_data.iter().enumerate() {
            for param in params {
                param_index.entry(param.as_str()).or_default().push(idx);
            }
        }

        // Step 3: For candidate pairs (sharing ≥1 param), compute intersection
        let mut seen_pairs: HashSet<(usize, usize)> = HashSet::new();
        let mut clump_map: HashMap<Vec<String>, HashSet<usize>> = HashMap::new();

        for indices in param_index.values() {
            if indices.len() < 2 {
                continue;
            }
            for (i_pos, &i) in indices.iter().enumerate() {
                for &j in &indices[i_pos + 1..] {
                    let key = if i < j { (i, j) } else { (j, i) };
                    if !seen_pairs.insert(key) {
                        continue;
                    }

                    // Compute param intersection for this pair
                    let mut shared: Vec<String> = func_data[key.0]
                        .1
                        .intersection(&func_data[key.1].1)
                        .cloned()
                        .collect();

                    if shared.len() >= self.thresholds.min_params {
                        shared.sort();
                        let entry = clump_map.entry(shared).or_default();
                        entry.insert(key.0);
                        entry.insert(key.1);
                    }
                }
            }
        }

        // Step 4: Pre-build callees lookup for functions in clumps
        let funcs_in_clumps: HashSet<usize> = clump_map
            .values()
            .flat_map(|indices| indices.iter().copied())
            .collect();

        let callees_map: HashMap<&str, HashSet<String>> = funcs_in_clumps
            .iter()
            .map(|&idx| {
                let qn = func_data[idx].0.qualified_name.as_str();
                let callees: HashSet<String> = graph
                    .get_callees(qn)
                    .iter()
                    .map(|c| c.qn(i).to_string())
                    .collect();
                (qn, callees)
            })
            .collect();

        // Step 5: Filter by min_occurrences, skip framework conventions, analyze call relationships
        let mut clumps: Vec<DataClump> = clump_map
            .into_iter()
            .filter(|(params, func_indices)| {
                func_indices.len() >= self.thresholds.min_occurrences
                    && !Self::is_framework_convention(params)
            })
            .map(|(params, func_indices)| {
                let funcs: Vec<FuncInfo> = func_indices
                    .iter()
                    .map(|&idx| func_data[idx].0.clone())
                    .collect();
                let (call_count, is_chain) =
                    self.analyze_call_relationships_cached(&callees_map, &funcs);
                DataClump {
                    params,
                    funcs,
                    call_relationships: call_count,
                    is_call_chain: is_chain,
                }
            })
            .collect();

        // Remove subsets
        clumps = self.remove_subsets(clumps);

        // Sort by call relationships first (stronger signal), then by function count
        clumps.sort_by(|a, b| {
            b.call_relationships
                .cmp(&a.call_relationships)
                .then(b.funcs.len().cmp(&a.funcs.len()))
        });

        clumps
    }

    /// Analyze call relationships using pre-built callees lookup table.
    ///
    /// Instead of calling `graph.get_callees()` per function per clump (thousands
    /// of queries), uses a pre-built HashMap for O(1) lookups.
    fn analyze_call_relationships_cached(
        &self,
        callees_map: &HashMap<&str, HashSet<String>>,
        funcs: &[FuncInfo],
    ) -> (usize, bool) {
        let func_qns: HashSet<&str> = funcs.iter().map(|f| f.qualified_name.as_str()).collect();
        let mut call_count = 0;
        let mut has_chain = false;

        for func in funcs {
            if let Some(callees) = callees_map.get(func.qualified_name.as_str()) {
                for callee_qn in callees {
                    if func_qns.contains(callee_qn.as_str()) {
                        call_count += 1;

                        // Check if callee also calls another function in the clump (chain)
                        if let Some(callee_callees) = callees_map.get(callee_qn.as_str()) {
                            for cc_qn in callee_callees {
                                if func_qns.contains(cc_qn.as_str())
                                    && *cc_qn != func.qualified_name
                                {
                                    has_chain = true;
                                }
                            }
                        }
                    }
                }
            }
        }

        (call_count, has_chain)
    }

    /// Remove clumps whose parameter set is a strict subset of another clump that has an equal or
    /// greater occurrence count (function count). For example, if `(a, b, c)` appears in 5
    /// functions and `(a, b, c, d)` also appears in 5 functions, only `(a, b, c, d)` is kept.
    fn remove_subsets(&self, mut clumps: Vec<DataClump>) -> Vec<DataClump> {
        // Sort largest param set first so supersets are processed before subsets
        clumps.sort_by_key(|c| std::cmp::Reverse(c.params.len()));

        let mut result: Vec<DataClump> = Vec::new();

        for clump in clumps {
            let param_set: HashSet<&String> = clump.params.iter().collect();

            let is_subset = result.iter().any(|existing: &DataClump| {
                let existing_params: HashSet<&String> = existing.params.iter().collect();
                // Strict subset of params (fewer params, all contained) AND
                // the superset has equal or more occurrences
                param_set.len() < existing_params.len()
                    && param_set.is_subset(&existing_params)
                    && existing.funcs.len() >= clump.funcs.len()
            });

            if !is_subset {
                result.push(clump);
            }
        }

        result
    }

    /// Calculate severity based on function count and call relationships
    fn calculate_severity(&self, clump: &DataClump) -> Severity {
        let func_count = clump.funcs.len();
        let call_rels = clump.call_relationships;

        // Base severity from function count
        let base = if func_count >= 6 {
            Severity::High
        } else if func_count >= 4 {
            Severity::Medium
        } else {
            Severity::Low
        };

        // Upgrade severity if there are call relationships (stronger signal)
        // Functions that call each other with the same params = definite refactor target
        if clump.is_call_chain {
            // Params traveling through a call chain = HIGH priority
            return match base {
                Severity::Low => Severity::Medium,
                Severity::Medium => Severity::High,
                _ => Severity::High,
            };
        }

        if call_rels >= 3 {
            // Many mutual calls = boost severity
            return match base {
                Severity::Low => Severity::Medium,
                _ => base,
            };
        }

        base
    }
}

struct DataClump {
    params: Vec<String>,
    funcs: Vec<FuncInfo>,
    /// Number of call relationships between functions in this clump
    call_relationships: usize,
    /// Whether functions form a call chain (A->B->C all have same params)
    is_call_chain: bool,
}

#[derive(Clone)]
struct FuncInfo {
    name: String,
    qualified_name: String,
    file: String,
    line: u32,
}

/// Generate combinations of k items (kept for tests only)
#[cfg(test)]
fn combinations(items: &[String], k: usize) -> Vec<Vec<String>> {
    if k > items.len() {
        return vec![];
    }
    if k == items.len() {
        return vec![items.to_vec()];
    }
    if k == 0 {
        return vec![vec![]];
    }

    let mut result = Vec::new();
    let mut indices: Vec<usize> = (0..k).collect();
    let n = items.len();

    loop {
        result.push(indices.iter().map(|&i| items[i].clone()).collect());

        let mut i = k;
        while i > 0 {
            i -= 1;
            if indices[i] < n - k + i {
                break;
            }
        }

        if i == 0 && indices[0] >= n - k {
            break;
        }

        indices[i] += 1;
        for j in (i + 1)..k {
            indices[j] = indices[j - 1] + 1;
        }
    }

    result
}

impl Default for DataClumpsDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl Detector for DataClumpsDetector {
    fn name(&self) -> &'static str {
        "DataClumpsDetector"
    }

    fn description(&self) -> &'static str {
        "Detects parameter groups that appear together across functions"
    }

    fn category(&self) -> &'static str {
        "code_smell"
    }

    fn config(&self) -> Option<&DetectorConfig> {
        Some(&self.config)
    }

    fn detect(
        &self,
        ctx: &crate::detectors::analysis_context::AnalysisContext,
    ) -> Result<Vec<Finding>> {
        let graph = ctx.graph;
        let mut findings = Vec::new();

        let clumps = self.find_clumps(graph);

        for clump in clumps {
            let severity = self.calculate_severity(&clump);
            let struct_name = suggest_struct_name(&clump.params);
            let params_str = clump.params.join(", ");

            let func_list: String = clump
                .funcs
                .iter()
                .take(5)
                .map(|f| format!("  - {} ({}:{})", f.name, f.file, f.line))
                .collect::<Vec<_>>()
                .join("\n");

            let more_note = if clump.funcs.len() > 5 {
                format!("\n  ... and {} more functions", clump.funcs.len() - 5)
            } else {
                String::new()
            };

            // Add call relationship info if present
            let call_info = if clump.is_call_chain {
                "\n\n⚠️ **Call chain detected**: These parameters travel through a call chain, \
                 making refactoring especially valuable."
                    .to_string()
            } else if clump.call_relationships > 0 {
                format!(
                    "\n\n📞 **{} call relationships** found between these functions.",
                    clump.call_relationships
                )
            } else {
                String::new()
            };

            let mut files: Vec<PathBuf> = clump
                .funcs
                .iter()
                .map(|f| PathBuf::from(&f.file))
                .collect::<HashSet<_>>()
                .into_iter()
                .collect();
            files.sort();

            findings.push(Finding {
                id: String::new(),
                detector: "DataClumpsDetector".to_string(),
                severity,
                title: format!("Data clump: ({})", params_str),
                description: format!(
                    "Parameters **({})** appear together in **{} functions**.\n\n\
                     This suggests a missing abstraction - consider extracting a `{}` struct.\n\n\
                     **Affected functions:**\n{}{}{}",
                    params_str,
                    clump.funcs.len(),
                    struct_name,
                    func_list,
                    more_note,
                    call_info
                ),
                affected_files: files,
                line_start: clump.funcs.first().map(|f| f.line),
                line_end: None,
                suggested_fix: Some(format!(
                    "Extract parameters into a struct:\n\n\
                     ```rust\n\
                     struct {} {{\n\
                     {}\n\
                     }}\n\
                     ```\n\n\
                     Then refactor functions to accept `{}` instead of {} separate parameters.{}",
                    struct_name,
                    clump.params.iter().map(|p| format!("    {}: Type,", p)).collect::<Vec<_>>().join("\n"),
                    struct_name,
                    clump.params.len(),
                    if clump.is_call_chain {
                        "\n\nSince these functions call each other, the refactoring can be done incrementally."
                    } else { "" }
                )),
                estimated_effort: Some(if clump.funcs.len() >= 6 || clump.is_call_chain {
                    "Large (2-4 hours)".to_string()
                } else {
                    "Medium (1-2 hours)".to_string()
                }),
                category: Some("code_smell".to_string()),
                cwe_id: None,
                why_it_matters: Some(
                    "Data clumps indicate missing abstractions. Grouping related parameters \
                     into a struct improves readability, type safety, and makes changes easier."
                        .to_string()
                ),
                ..Default::default()
            });
        }

        info!(
            "DataClumpsDetector found {} findings (graph-aware)",
            findings.len()
        );
        Ok(findings)
    }
}

impl crate::detectors::RegisteredDetector for DataClumpsDetector {
    fn create(init: &crate::detectors::DetectorInit) -> std::sync::Arc<dyn Detector> {
        std::sync::Arc::new(Self::with_config(init.config_for("DataClumpsDetector")))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_suggest_struct_name() {
        assert_eq!(
            suggest_struct_name(&["x".to_string(), "y".to_string()]),
            "Point"
        );
        assert_eq!(
            suggest_struct_name(&["host".to_string(), "port".to_string()]),
            "Address"
        );
        assert_eq!(
            suggest_struct_name(&["foo".to_string(), "bar".to_string(), "baz".to_string()]),
            "FooParams"
        );
    }

    #[test]
    fn test_combinations() {
        let items = vec!["a".to_string(), "b".to_string(), "c".to_string()];
        let combos = combinations(&items, 2);
        assert_eq!(combos.len(), 3); // ab, ac, bc
    }

    #[test]
    fn test_severity() {
        let detector = DataClumpsDetector::new();

        // Test base severity from function count
        let clump_3 = DataClump {
            params: vec!["a".to_string(), "b".to_string(), "c".to_string()],
            funcs: vec![
                FuncInfo {
                    name: "f1".to_string(),
                    qualified_name: "mod::f1".to_string(),
                    file: "a.rs".to_string(),
                    line: 1,
                },
                FuncInfo {
                    name: "f2".to_string(),
                    qualified_name: "mod::f2".to_string(),
                    file: "a.rs".to_string(),
                    line: 10,
                },
                FuncInfo {
                    name: "f3".to_string(),
                    qualified_name: "mod::f3".to_string(),
                    file: "a.rs".to_string(),
                    line: 20,
                },
            ],
            call_relationships: 0,
            is_call_chain: false,
        };
        assert_eq!(detector.calculate_severity(&clump_3), Severity::Low);

        let clump_4 = DataClump {
            params: vec!["a".to_string(), "b".to_string(), "c".to_string()],
            funcs: vec![
                FuncInfo {
                    name: "f1".to_string(),
                    qualified_name: "mod::f1".to_string(),
                    file: "a.rs".to_string(),
                    line: 1,
                },
                FuncInfo {
                    name: "f2".to_string(),
                    qualified_name: "mod::f2".to_string(),
                    file: "a.rs".to_string(),
                    line: 10,
                },
                FuncInfo {
                    name: "f3".to_string(),
                    qualified_name: "mod::f3".to_string(),
                    file: "a.rs".to_string(),
                    line: 20,
                },
                FuncInfo {
                    name: "f4".to_string(),
                    qualified_name: "mod::f4".to_string(),
                    file: "a.rs".to_string(),
                    line: 30,
                },
            ],
            call_relationships: 0,
            is_call_chain: false,
        };
        assert_eq!(detector.calculate_severity(&clump_4), Severity::Medium);

        let clump_6 = DataClump {
            params: vec!["a".to_string(), "b".to_string(), "c".to_string()],
            funcs: vec![
                FuncInfo {
                    name: "f1".to_string(),
                    qualified_name: "mod::f1".to_string(),
                    file: "a.rs".to_string(),
                    line: 1,
                },
                FuncInfo {
                    name: "f2".to_string(),
                    qualified_name: "mod::f2".to_string(),
                    file: "a.rs".to_string(),
                    line: 10,
                },
                FuncInfo {
                    name: "f3".to_string(),
                    qualified_name: "mod::f3".to_string(),
                    file: "a.rs".to_string(),
                    line: 20,
                },
                FuncInfo {
                    name: "f4".to_string(),
                    qualified_name: "mod::f4".to_string(),
                    file: "a.rs".to_string(),
                    line: 30,
                },
                FuncInfo {
                    name: "f5".to_string(),
                    qualified_name: "mod::f5".to_string(),
                    file: "a.rs".to_string(),
                    line: 40,
                },
                FuncInfo {
                    name: "f6".to_string(),
                    qualified_name: "mod::f6".to_string(),
                    file: "a.rs".to_string(),
                    line: 50,
                },
            ],
            call_relationships: 0,
            is_call_chain: false,
        };
        assert_eq!(detector.calculate_severity(&clump_6), Severity::High);

        // Test call chain boost
        let clump_chain = DataClump {
            params: vec!["a".to_string(), "b".to_string(), "c".to_string()],
            funcs: vec![
                FuncInfo {
                    name: "f1".to_string(),
                    qualified_name: "mod::f1".to_string(),
                    file: "a.rs".to_string(),
                    line: 1,
                },
                FuncInfo {
                    name: "f2".to_string(),
                    qualified_name: "mod::f2".to_string(),
                    file: "a.rs".to_string(),
                    line: 10,
                },
                FuncInfo {
                    name: "f3".to_string(),
                    qualified_name: "mod::f3".to_string(),
                    file: "a.rs".to_string(),
                    line: 20,
                },
            ],
            call_relationships: 2,
            is_call_chain: true, // Call chain boosts Low -> Medium
        };
        assert_eq!(detector.calculate_severity(&clump_chain), Severity::Medium);
    }

    #[test]
    fn test_framework_convention_skipped() {
        // (req, res, next) is a known Express.js middleware signature — must be skipped
        assert!(DataClumpsDetector::is_framework_convention(&[
            "req".to_string(),
            "res".to_string(),
            "next".to_string(),
        ]));

        // (ctx, w, r) is a known HTTP handler signature — must be skipped
        assert!(DataClumpsDetector::is_framework_convention(&[
            "ctx".to_string(),
            "w".to_string(),
            "r".to_string(),
        ]));

        // A subset of a framework convention also matches
        assert!(DataClumpsDetector::is_framework_convention(&[
            "req".to_string(),
            "res".to_string(),
        ]));

        // An arbitrary param set is NOT a framework convention
        assert!(!DataClumpsDetector::is_framework_convention(&[
            "user_id".to_string(),
            "email".to_string(),
            "name".to_string(),
        ]));
    }

    #[test]
    fn test_subset_deduplication() {
        let detector = DataClumpsDetector::new();

        let make_funcs = |n: usize| -> Vec<FuncInfo> {
            (1..=n)
                .map(|i| FuncInfo {
                    name: format!("f{}", i),
                    qualified_name: format!("mod::f{}", i),
                    file: "a.rs".to_string(),
                    line: i as u32,
                })
                .collect()
        };

        // (a, b, c, d) with 5 occurrences
        let superset = DataClump {
            params: vec![
                "a".to_string(),
                "b".to_string(),
                "c".to_string(),
                "d".to_string(),
            ],
            funcs: make_funcs(5),
            call_relationships: 0,
            is_call_chain: false,
        };

        // (a, b, c) with 5 occurrences — strict subset, same occurrence count → should be removed
        let subset = DataClump {
            params: vec!["a".to_string(), "b".to_string(), "c".to_string()],
            funcs: make_funcs(5),
            call_relationships: 0,
            is_call_chain: false,
        };

        let result = detector.remove_subsets(vec![superset, subset]);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].params.len(), 4, "only the superset should remain");

        // (a, b, c) with MORE occurrences than the superset → should NOT be removed
        let subset_more_occ = DataClump {
            params: vec!["a".to_string(), "b".to_string(), "c".to_string()],
            funcs: make_funcs(8),
            call_relationships: 0,
            is_call_chain: false,
        };
        let superset2 = DataClump {
            params: vec![
                "a".to_string(),
                "b".to_string(),
                "c".to_string(),
                "d".to_string(),
            ],
            funcs: make_funcs(5),
            call_relationships: 0,
            is_call_chain: false,
        };
        let result2 = detector.remove_subsets(vec![superset2, subset_more_occ]);
        assert_eq!(
            result2.len(),
            2,
            "subset with more occurrences should be kept"
        );
    }
}