debtmap 0.16.3

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
//! Cluster refinement and pattern detection.
//!
//! Provides functions for:
//! - Subdividing oversized clusters
//! - Merging tiny clusters
//! - Applying Rust-specific naming patterns
//! - Detecting I/O boundary, query, matching, and lookup patterns

use std::collections::HashMap;

use super::super::types::{capitalize_first, BehaviorCategory, MethodCluster};
use super::cohesion::calculate_standalone_cohesion;

const MIN_CLUSTER_SIZE: usize = 3;
const OVERSIZED_CLUSTER_THRESHOLD: usize = 15;
const PATTERN_THRESHOLD: f64 = 0.6;

/// Subdivide oversized Domain clusters using verb patterns.
pub fn subdivide_oversized_clusters(
    clusters: Vec<MethodCluster>,
    adjacency: &HashMap<(String, String), usize>,
) -> Vec<MethodCluster> {
    clusters
        .into_iter()
        .flat_map(|cluster| subdivide_if_oversized(cluster, adjacency))
        .collect()
}

/// Subdivide a single cluster if it's oversized.
fn subdivide_if_oversized(
    cluster: MethodCluster,
    adjacency: &HashMap<(String, String), usize>,
) -> Vec<MethodCluster> {
    if !should_subdivide(&cluster) {
        return vec![cluster];
    }

    let subclusters = cluster_by_verb_patterns(&cluster.methods);

    if subclusters.len() <= 1 {
        return vec![cluster];
    }

    create_subclusters_from_verbs(subclusters, adjacency)
}

/// Check if a cluster should be subdivided.
fn should_subdivide(cluster: &MethodCluster) -> bool {
    cluster.methods.len() > OVERSIZED_CLUSTER_THRESHOLD
        && matches!(cluster.category, BehaviorCategory::Domain(_))
}

/// Create subclusters from verb pattern groups.
fn create_subclusters_from_verbs(
    verb_groups: HashMap<String, Vec<String>>,
    adjacency: &HashMap<(String, String), usize>,
) -> Vec<MethodCluster> {
    verb_groups
        .into_iter()
        .filter(|(_, methods)| methods.len() >= MIN_CLUSTER_SIZE)
        .map(|(verb, methods)| create_subcluster(verb, methods, adjacency))
        .collect()
}

/// Create a single subcluster from a verb group.
fn create_subcluster(
    verb: String,
    methods: Vec<String>,
    adjacency: &HashMap<(String, String), usize>,
) -> MethodCluster {
    let (internal_calls, external_calls) = calculate_standalone_cohesion(&methods, adjacency);

    let mut cluster = MethodCluster {
        category: BehaviorCategory::Domain(verb),
        methods,
        fields_accessed: vec![],
        internal_calls,
        external_calls,
        cohesion_score: 0.0,
    };
    cluster.calculate_cohesion();
    cluster
}

/// Cluster methods by verb/action patterns for secondary subdivision.
fn cluster_by_verb_patterns(methods: &[String]) -> HashMap<String, Vec<String>> {
    let mut clusters: HashMap<String, Vec<String>> = HashMap::new();

    for method in methods {
        let verb = extract_verb_pattern(method);
        clusters.entry(verb).or_default().push(method.clone());
    }

    clusters
}

/// Extract verb pattern from method name for grouping.
fn extract_verb_pattern(method_name: &str) -> String {
    let prefixes = get_verb_prefixes();

    for prefix in &prefixes {
        if method_name.to_lowercase().starts_with(prefix) {
            return capitalize_first(prefix);
        }
    }

    extract_first_word(method_name)
}

/// Get common verb prefixes in Rust.
fn get_verb_prefixes() -> Vec<&'static str> {
    vec![
        "parse",
        "build",
        "create",
        "make",
        "construct",
        "get",
        "fetch",
        "retrieve",
        "find",
        "search",
        "lookup",
        "query",
        "set",
        "update",
        "modify",
        "change",
        "is",
        "has",
        "can",
        "should",
        "check",
        "apply",
        "execute",
        "run",
        "process",
        "handle",
        "demangle",
        "normalize",
        "sanitize",
        "clean",
        "calculate",
        "compute",
        "derive",
        "match",
        "compare",
        "equals",
    ]
}

/// Extract first word from method name as fallback.
fn extract_first_word(method_name: &str) -> String {
    method_name
        .split('_')
        .next()
        .filter(|s| !s.is_empty())
        .map(capitalize_first)
        .unwrap_or_else(|| "Utilities".to_string())
}

/// Merge tiny clusters (<3 methods) into related larger clusters.
pub fn merge_tiny_clusters(clusters: Vec<MethodCluster>) -> Vec<MethodCluster> {
    if clusters.len() <= 1 {
        return clusters;
    }

    let (normal, tiny) = partition_by_size(clusters);
    let (mut normal, unmerged) = merge_tiny_into_related(normal, tiny);

    handle_unmerged_methods(&mut normal, unmerged);
    normal
}

/// Partition clusters into normal (>=3) and tiny (<3).
fn partition_by_size(clusters: Vec<MethodCluster>) -> (Vec<MethodCluster>, Vec<MethodCluster>) {
    clusters
        .into_iter()
        .partition(|c| c.methods.len() >= MIN_CLUSTER_SIZE)
}

/// Try to merge tiny clusters into related normal clusters.
fn merge_tiny_into_related(
    mut normal: Vec<MethodCluster>,
    tiny: Vec<MethodCluster>,
) -> (Vec<MethodCluster>, Vec<String>) {
    let mut unmerged = Vec::new();

    for tiny_cluster in tiny {
        if !try_merge_into_related(&mut normal, &tiny_cluster) {
            unmerged.extend(tiny_cluster.methods);
        }
    }

    (normal, unmerged)
}

/// Try to merge a tiny cluster into a related normal cluster.
fn try_merge_into_related(normal: &mut [MethodCluster], tiny: &MethodCluster) -> bool {
    for cluster in normal.iter_mut() {
        if categories_are_related(&tiny.category, &cluster.category) {
            cluster.methods.extend(tiny.methods.clone());
            return true;
        }
    }
    false
}

/// Handle unmerged methods by creating or extending utilities cluster.
fn handle_unmerged_methods(normal: &mut Vec<MethodCluster>, unmerged: Vec<String>) {
    if unmerged.is_empty() {
        return;
    }

    if let Some(utilities) = find_utilities_cluster(normal) {
        utilities.methods.extend(unmerged);
    } else if unmerged.len() >= MIN_CLUSTER_SIZE {
        normal.push(create_utilities_cluster(unmerged));
    } else if let Some(largest) = find_largest_cluster(normal) {
        largest.methods.extend(unmerged);
    } else {
        normal.push(create_utilities_cluster(unmerged));
    }
}

/// Find existing utilities cluster.
fn find_utilities_cluster(clusters: &mut [MethodCluster]) -> Option<&mut MethodCluster> {
    clusters
        .iter_mut()
        .find(|c| matches!(&c.category, BehaviorCategory::Domain(name) if name == "Utilities"))
}

/// Find largest cluster by method count.
fn find_largest_cluster(clusters: &mut [MethodCluster]) -> Option<&mut MethodCluster> {
    clusters.iter_mut().max_by_key(|c| c.methods.len())
}

/// Create a new utilities cluster.
fn create_utilities_cluster(methods: Vec<String>) -> MethodCluster {
    MethodCluster {
        category: BehaviorCategory::Domain("Utilities".to_string()),
        methods,
        fields_accessed: vec![],
        internal_calls: 0,
        external_calls: 0,
        cohesion_score: 0.0,
    }
}

/// Check if two behavioral categories are related for merging purposes.
pub fn categories_are_related(cat1: &BehaviorCategory, cat2: &BehaviorCategory) -> bool {
    use BehaviorCategory::*;

    match (cat1, cat2) {
        // Same category type
        (Lifecycle, Lifecycle)
        | (StateManagement, StateManagement)
        | (Persistence, Persistence)
        | (Validation, Validation)
        | (Rendering, Rendering)
        | (EventHandling, EventHandling)
        | (Computation, Computation)
        | (Parsing, Parsing)
        | (Filtering, Filtering)
        | (Transformation, Transformation)
        | (DataAccess, DataAccess)
        | (Construction, Construction)
        | (Processing, Processing)
        | (Communication, Communication) => true,

        // Domain categories with same or similar names
        (Domain(name1), Domain(name2)) => names_are_related(name1, name2),

        // Related categories
        (Persistence, StateManagement) | (StateManagement, Persistence) => true,
        (Validation, Computation) | (Computation, Validation) => true,
        (Parsing, DataAccess) | (DataAccess, Parsing) => true,
        (Filtering, Transformation) | (Transformation, Filtering) => true,

        _ => false,
    }
}

/// Check if two domain names are related.
fn names_are_related(name1: &str, name2: &str) -> bool {
    name1 == name2
        || name1.to_lowercase().contains(&name2.to_lowercase())
        || name2.to_lowercase().contains(&name1.to_lowercase())
}

/// Apply Rust-specific naming patterns to improve categorization.
pub fn apply_rust_patterns(clusters: Vec<MethodCluster>) -> Vec<MethodCluster> {
    clusters.into_iter().map(apply_pattern_to_cluster).collect()
}

/// Apply pattern detection to a single cluster.
fn apply_pattern_to_cluster(mut cluster: MethodCluster) -> MethodCluster {
    if !matches!(cluster.category, BehaviorCategory::Domain(_)) {
        return cluster;
    }

    // Only change category if a specific pattern is detected
    // Otherwise preserve the original category
    if let Some(new_category) = detect_pattern(&cluster.methods) {
        cluster.category = new_category;
    }
    cluster
}

/// Detect the dominant pattern in a cluster's methods.
///
/// Returns Some(category) if a specific pattern is detected, None otherwise.
fn detect_pattern(methods: &[String]) -> Option<BehaviorCategory> {
    if is_io_boundary_cluster(methods) {
        Some(BehaviorCategory::Domain("Parser".to_string()))
    } else if is_query_cluster(methods) {
        Some(BehaviorCategory::Domain("Query".to_string()))
    } else if is_matching_cluster(methods) {
        Some(BehaviorCategory::Domain("Matching".to_string()))
    } else if is_lookup_cluster(methods) {
        Some(BehaviorCategory::Domain("Lookup".to_string()))
    } else {
        None // Preserve original category
    }
}

/// Check if cluster is an I/O boundary (parser, reader, writer).
fn is_io_boundary_cluster(methods: &[String]) -> bool {
    let keywords = [
        "parse",
        "read",
        "write",
        "load",
        "save",
        "deserialize",
        "serialize",
    ];
    pattern_ratio(methods, &keywords) > PATTERN_THRESHOLD
}

/// Check if cluster is query-focused.
fn is_query_cluster(methods: &[String]) -> bool {
    let prefixes = ["get_", "fetch_", "retrieve_", "query_"];
    prefix_ratio(methods, &prefixes) > PATTERN_THRESHOLD
}

/// Check if cluster is matching/strategy focused.
fn is_matching_cluster(methods: &[String]) -> bool {
    let keywords = ["match", "compare", "equals", "strategy"];
    pattern_ratio(methods, &keywords) > PATTERN_THRESHOLD
}

/// Check if cluster is lookup-focused.
fn is_lookup_cluster(methods: &[String]) -> bool {
    let prefixes = ["find_", "search_", "lookup_", "locate_"];
    prefix_ratio(methods, &prefixes) > PATTERN_THRESHOLD
}

/// Calculate ratio of methods containing any keyword.
fn pattern_ratio(methods: &[String], keywords: &[&str]) -> f64 {
    let count = methods
        .iter()
        .filter(|m| {
            let lower = m.to_lowercase();
            keywords.iter().any(|kw| lower.contains(kw))
        })
        .count();

    count as f64 / methods.len() as f64
}

/// Calculate ratio of methods starting with any prefix.
fn prefix_ratio(methods: &[String], prefixes: &[&str]) -> f64 {
    let count = methods
        .iter()
        .filter(|m| prefixes.iter().any(|p| m.starts_with(p)))
        .count();

    count as f64 / methods.len() as f64
}

/// Merge clusters with identical categories to avoid duplicate module names.
pub fn merge_duplicate_categories(clusters: Vec<MethodCluster>) -> Vec<MethodCluster> {
    let mut category_map: HashMap<String, MethodCluster> = HashMap::new();

    for cluster in clusters {
        let key = cluster.category.display_name();

        if let Some(existing) = category_map.get_mut(&key) {
            merge_clusters(existing, cluster);
        } else {
            category_map.insert(key, cluster);
        }
    }

    category_map.into_values().collect()
}

/// Merge one cluster into another.
fn merge_clusters(target: &mut MethodCluster, source: MethodCluster) {
    target.methods.extend(source.methods);
    target.internal_calls += source.internal_calls;
    target.external_calls += source.external_calls;
    target.calculate_cohesion();
}

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

    #[test]
    fn test_extract_verb_pattern_known() {
        assert_eq!(extract_verb_pattern("parse_json"), "Parse");
        assert_eq!(extract_verb_pattern("get_value"), "Get");
        assert_eq!(extract_verb_pattern("calculate_sum"), "Calculate");
    }

    #[test]
    fn test_extract_verb_pattern_unknown() {
        assert_eq!(extract_verb_pattern("custom_method"), "Custom");
        assert_eq!(extract_verb_pattern("another_thing"), "Another");
    }

    #[test]
    fn test_categories_related_same_type() {
        assert!(categories_are_related(
            &BehaviorCategory::Parsing,
            &BehaviorCategory::Parsing
        ));
    }

    #[test]
    fn test_categories_related_cross_type() {
        assert!(categories_are_related(
            &BehaviorCategory::Persistence,
            &BehaviorCategory::StateManagement
        ));
    }

    #[test]
    fn test_categories_not_related() {
        assert!(!categories_are_related(
            &BehaviorCategory::Rendering,
            &BehaviorCategory::Parsing
        ));
    }

    #[test]
    fn test_domain_names_related() {
        assert!(names_are_related("Parser", "Parser"));
        assert!(names_are_related("FileParser", "Parser"));
        assert!(!names_are_related("Parser", "Query"));
    }

    #[test]
    fn test_is_io_boundary_cluster() {
        let methods = vec![
            "parse_json".into(),
            "parse_xml".into(),
            "read_file".into(),
            "other".into(),
        ];
        assert!(is_io_boundary_cluster(&methods));
    }

    #[test]
    fn test_is_not_io_boundary_cluster() {
        let methods = vec![
            "calculate_sum".into(),
            "validate_input".into(),
            "process_data".into(),
        ];
        assert!(!is_io_boundary_cluster(&methods));
    }

    #[test]
    fn test_merge_duplicate_categories() {
        let clusters = vec![
            MethodCluster {
                category: BehaviorCategory::Parsing,
                methods: vec!["a".into()],
                fields_accessed: vec![],
                internal_calls: 1,
                external_calls: 0,
                cohesion_score: 0.5,
            },
            MethodCluster {
                category: BehaviorCategory::Parsing,
                methods: vec!["b".into()],
                fields_accessed: vec![],
                internal_calls: 2,
                external_calls: 1,
                cohesion_score: 0.6,
            },
        ];

        let merged = merge_duplicate_categories(clusters);
        assert_eq!(merged.len(), 1);
        assert_eq!(merged[0].methods.len(), 2);
    }
}