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
use crate::core::FunctionMetrics;
use serde::{Deserialize, Serialize};

/// Preset threshold configurations
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum ThresholdPreset {
    /// Strict thresholds for high code quality standards
    Strict,
    /// Balanced thresholds for typical projects
    Balanced,
    /// Lenient thresholds for legacy or complex domains
    Lenient,
}

/// Complexity threshold configuration for determining when to flag functions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComplexityThresholds {
    /// Minimum total complexity to flag (cyclomatic + cognitive)
    #[serde(default = "default_minimum_total_complexity")]
    pub minimum_total_complexity: u32,

    /// Minimum cyclomatic complexity to flag
    #[serde(default = "default_minimum_cyclomatic_complexity")]
    pub minimum_cyclomatic_complexity: u32,

    /// Minimum cognitive complexity to flag
    #[serde(default = "default_minimum_cognitive_complexity")]
    pub minimum_cognitive_complexity: u32,

    /// Minimum number of match arms to flag
    #[serde(default = "default_minimum_match_arms")]
    pub minimum_match_arms: usize,

    /// Minimum if-else chain length to flag
    #[serde(default = "default_minimum_if_else_chain")]
    pub minimum_if_else_chain: usize,

    /// Minimum function length (lines) to flag
    #[serde(default = "default_minimum_function_length")]
    pub minimum_function_length: usize,

    // Role-based multipliers
    /// Multiplier for entry point functions (main, handlers)
    #[serde(default = "default_entry_point_multiplier")]
    pub entry_point_multiplier: f64,

    /// Multiplier for core logic functions
    #[serde(default = "default_core_logic_multiplier")]
    pub core_logic_multiplier: f64,

    /// Multiplier for utility functions (getters, setters)
    #[serde(default = "default_utility_multiplier")]
    pub utility_multiplier: f64,

    /// Multiplier for test functions
    #[serde(default = "default_test_function_multiplier")]
    pub test_function_multiplier: f64,
}

impl Default for ComplexityThresholds {
    fn default() -> Self {
        Self {
            minimum_total_complexity: default_minimum_total_complexity(),
            minimum_cyclomatic_complexity: default_minimum_cyclomatic_complexity(),
            minimum_cognitive_complexity: default_minimum_cognitive_complexity(),
            minimum_match_arms: default_minimum_match_arms(),
            minimum_if_else_chain: default_minimum_if_else_chain(),
            minimum_function_length: default_minimum_function_length(),
            entry_point_multiplier: default_entry_point_multiplier(),
            core_logic_multiplier: default_core_logic_multiplier(),
            utility_multiplier: default_utility_multiplier(),
            test_function_multiplier: default_test_function_multiplier(),
        }
    }
}

// Default values
fn default_minimum_total_complexity() -> u32 {
    8
}
fn default_minimum_cyclomatic_complexity() -> u32 {
    5
}
fn default_minimum_cognitive_complexity() -> u32 {
    10
}
fn default_minimum_match_arms() -> usize {
    4
}
fn default_minimum_if_else_chain() -> usize {
    3
}
fn default_minimum_function_length() -> usize {
    20
}
fn default_entry_point_multiplier() -> f64 {
    1.5
}
fn default_core_logic_multiplier() -> f64 {
    1.0
}
fn default_utility_multiplier() -> f64 {
    0.8
}
fn default_test_function_multiplier() -> f64 {
    2.0
}

/// Complexity level classification
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ComplexityLevel {
    Trivial,
    Moderate,
    High,
    Excessive,
}

impl ComplexityThresholds {
    /// Create thresholds from a preset configuration
    pub fn from_preset(preset: ThresholdPreset) -> Self {
        match preset {
            ThresholdPreset::Strict => Self {
                minimum_total_complexity: 5,
                minimum_cyclomatic_complexity: 3,
                minimum_cognitive_complexity: 7,
                minimum_match_arms: 3,
                minimum_if_else_chain: 2,
                minimum_function_length: 15,
                entry_point_multiplier: 1.2,
                core_logic_multiplier: 1.0,
                utility_multiplier: 0.6,
                test_function_multiplier: 3.0, // More lenient for test functions
            },
            ThresholdPreset::Balanced => Self::default(),
            ThresholdPreset::Lenient => Self {
                minimum_total_complexity: 15,
                minimum_cyclomatic_complexity: 10,
                minimum_cognitive_complexity: 20,
                minimum_match_arms: 8,
                minimum_if_else_chain: 5,
                minimum_function_length: 50,
                entry_point_multiplier: 2.0,
                core_logic_multiplier: 1.0,
                utility_multiplier: 1.0,
                test_function_multiplier: 3.0,
            },
        }
    }

    /// Check if a function should be flagged based on thresholds.
    ///
    /// A function is flagged if ANY of these conditions are met:
    /// 1. Cyclomatic complexity alone exceeds its threshold
    /// 2. Cognitive complexity alone exceeds its threshold
    /// 3. Total complexity (cyclomatic + cognitive) exceeds its threshold AND length exceeds minimum
    /// 4. Function length is extremely long (5x the minimum threshold)
    ///
    /// This ensures single-dimension complexity issues are caught, not just
    /// functions that are complex in ALL dimensions simultaneously.
    pub fn should_flag_function(&self, metrics: &FunctionMetrics, role: FunctionRole) -> bool {
        let multiplier = self.get_role_multiplier(role);

        // Apply multiplier to thresholds (higher multiplier = more lenient)
        let adjusted_cyclomatic_threshold =
            (self.minimum_cyclomatic_complexity as f64 * multiplier) as u32;
        let adjusted_cognitive_threshold =
            (self.minimum_cognitive_complexity as f64 * multiplier) as u32;
        let adjusted_total_threshold = (self.minimum_total_complexity as f64 * multiplier) as u32;
        let adjusted_length_threshold = (self.minimum_function_length as f64 * multiplier) as usize;

        let total_complexity = metrics.cyclomatic + metrics.cognitive;

        // Flag if ANY significant threshold is exceeded
        let cyclomatic_exceeded = metrics.cyclomatic >= adjusted_cyclomatic_threshold;
        let cognitive_exceeded = metrics.cognitive >= adjusted_cognitive_threshold;
        let total_exceeded = total_complexity >= adjusted_total_threshold;
        let length_exceeded = metrics.length >= adjusted_length_threshold;
        let extremely_long = metrics.length >= adjusted_length_threshold * 5;

        // Flag if:
        // - Cyclomatic alone is high enough, OR
        // - Cognitive alone is high enough, OR
        // - Total complexity is high AND function is not trivially short, OR
        // - Function is extremely long (5x threshold)
        cyclomatic_exceeded
            || cognitive_exceeded
            || (total_exceeded && length_exceeded)
            || extremely_long
    }

    /// Get the complexity level for given metrics
    pub fn get_complexity_level(&self, metrics: &FunctionMetrics) -> ComplexityLevel {
        let total = metrics.cyclomatic + metrics.cognitive;
        match total {
            t if t < self.minimum_total_complexity => ComplexityLevel::Trivial,
            t if t < self.minimum_total_complexity * 2 => ComplexityLevel::Moderate,
            t if t < self.minimum_total_complexity * 3 => ComplexityLevel::High,
            _ => ComplexityLevel::Excessive,
        }
    }

    /// Get role-based multiplier
    pub fn get_role_multiplier(&self, role: FunctionRole) -> f64 {
        match role {
            FunctionRole::EntryPoint => self.entry_point_multiplier,
            FunctionRole::CoreLogic => self.core_logic_multiplier,
            FunctionRole::Utility => self.utility_multiplier,
            FunctionRole::Test => self.test_function_multiplier,
            FunctionRole::Unknown => self.core_logic_multiplier,
        }
    }

    /// Validate thresholds are reasonable
    pub fn validate(&self) -> Result<(), String> {
        if self.minimum_total_complexity == 0 {
            return Err("minimum_total_complexity must be greater than 0".to_string());
        }
        if self.minimum_cyclomatic_complexity == 0 {
            return Err("minimum_cyclomatic_complexity must be greater than 0".to_string());
        }
        if self.minimum_cognitive_complexity == 0 {
            return Err("minimum_cognitive_complexity must be greater than 0".to_string());
        }

        // Check multipliers are positive
        if self.entry_point_multiplier <= 0.0 {
            return Err("entry_point_multiplier must be positive".to_string());
        }
        if self.core_logic_multiplier <= 0.0 {
            return Err("core_logic_multiplier must be positive".to_string());
        }
        if self.utility_multiplier <= 0.0 {
            return Err("utility_multiplier must be positive".to_string());
        }
        if self.test_function_multiplier <= 0.0 {
            return Err("test_function_multiplier must be positive".to_string());
        }

        Ok(())
    }

    /// Get preset configurations
    pub fn preset(name: &str) -> Option<Self> {
        match name {
            "strict" => Some(Self {
                minimum_total_complexity: 5,
                minimum_cyclomatic_complexity: 3,
                minimum_cognitive_complexity: 7,
                minimum_match_arms: 3,
                minimum_if_else_chain: 2,
                minimum_function_length: 15,
                entry_point_multiplier: 1.2,
                core_logic_multiplier: 1.0,
                utility_multiplier: 0.6,
                test_function_multiplier: 3.0,
            }),
            "balanced" => Some(Self::default()),
            "lenient" => Some(Self {
                minimum_total_complexity: 15,
                minimum_cyclomatic_complexity: 10,
                minimum_cognitive_complexity: 20,
                minimum_match_arms: 8,
                minimum_if_else_chain: 5,
                minimum_function_length: 50,
                entry_point_multiplier: 2.0,
                core_logic_multiplier: 1.0,
                utility_multiplier: 1.0,
                test_function_multiplier: 3.0,
            }),
            _ => None,
        }
    }
}

/// Function role classification (duplicated here to avoid circular dependency)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum FunctionRole {
    EntryPoint,
    CoreLogic,
    Utility,
    Test,
    Unknown,
}

impl FunctionRole {
    /// Determine role from function name
    pub fn from_name(name: &str) -> Self {
        if name == "main" || name.ends_with("_handler") || name.starts_with("handle_") {
            Self::EntryPoint
        } else if name.starts_with("test_") || name.ends_with("_test") {
            Self::Test
        } else if name.starts_with("get_")
            || name.starts_with("set_")
            || name.starts_with("is_")
            || name.starts_with("has_")
        {
            Self::Utility
        } else {
            Self::CoreLogic
        }
    }
}

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

    #[test]
    fn test_default_thresholds() {
        let thresholds = ComplexityThresholds::default();
        assert_eq!(thresholds.minimum_total_complexity, 8);
        assert_eq!(thresholds.minimum_cyclomatic_complexity, 5);
        assert_eq!(thresholds.minimum_cognitive_complexity, 10);
    }

    #[test]
    fn test_should_flag_function() {
        let thresholds = ComplexityThresholds::default();

        let mut simple_metrics =
            FunctionMetrics::new("simple".to_string(), std::path::PathBuf::from("test.rs"), 1);
        simple_metrics.cyclomatic = 2;
        simple_metrics.cognitive = 3;
        simple_metrics.length = 10;
        simple_metrics.is_pure = Some(true);
        simple_metrics.purity_confidence = Some(0.9);

        // Simple function should not be flagged
        assert!(!thresholds.should_flag_function(&simple_metrics, FunctionRole::CoreLogic));

        let mut complex_metrics = FunctionMetrics::new(
            "complex".to_string(),
            std::path::PathBuf::from("test.rs"),
            1,
        );
        complex_metrics.cyclomatic = 10;
        complex_metrics.cognitive = 15;
        complex_metrics.length = 50;
        complex_metrics.is_pure = Some(false);
        complex_metrics.purity_confidence = Some(0.9);

        // Complex function should be flagged
        assert!(thresholds.should_flag_function(&complex_metrics, FunctionRole::CoreLogic));
    }

    #[test]
    fn test_complexity_levels() {
        let thresholds = ComplexityThresholds::default();

        let mut trivial = FunctionMetrics::new(
            "trivial".to_string(),
            std::path::PathBuf::from("test.rs"),
            1,
        );
        trivial.cyclomatic = 2;
        trivial.cognitive = 3;
        assert_eq!(
            thresholds.get_complexity_level(&trivial),
            ComplexityLevel::Trivial
        );

        let mut moderate = FunctionMetrics::new(
            "moderate".to_string(),
            std::path::PathBuf::from("test.rs"),
            1,
        );
        moderate.cyclomatic = 5;
        moderate.cognitive = 8;
        assert_eq!(
            thresholds.get_complexity_level(&moderate),
            ComplexityLevel::Moderate
        );

        let mut high =
            FunctionMetrics::new("high".to_string(), std::path::PathBuf::from("test.rs"), 1);
        high.cyclomatic = 8;
        high.cognitive = 12;
        assert_eq!(
            thresholds.get_complexity_level(&high),
            ComplexityLevel::High
        );

        let mut excessive = FunctionMetrics::new(
            "excessive".to_string(),
            std::path::PathBuf::from("test.rs"),
            1,
        );
        excessive.cyclomatic = 20;
        excessive.cognitive = 30;
        assert_eq!(
            thresholds.get_complexity_level(&excessive),
            ComplexityLevel::Excessive
        );
    }

    #[test]
    fn test_presets() {
        let strict = ComplexityThresholds::preset("strict").unwrap();
        assert_eq!(strict.minimum_total_complexity, 5);

        let lenient = ComplexityThresholds::preset("lenient").unwrap();
        assert_eq!(lenient.minimum_total_complexity, 15);

        let balanced = ComplexityThresholds::preset("balanced").unwrap();
        assert_eq!(balanced.minimum_total_complexity, 8);
    }

    #[test]
    fn test_role_multipliers() {
        let thresholds = ComplexityThresholds::default();

        assert_eq!(
            thresholds.get_role_multiplier(FunctionRole::EntryPoint),
            1.5
        );
        assert_eq!(thresholds.get_role_multiplier(FunctionRole::CoreLogic), 1.0);
        assert_eq!(thresholds.get_role_multiplier(FunctionRole::Utility), 0.8);
        assert_eq!(thresholds.get_role_multiplier(FunctionRole::Test), 2.0);
    }

    /// Bug fix test: A function with extremely high cyclomatic complexity
    /// should be flagged even if cognitive complexity is low.
    /// The current bug requires ALL thresholds to be exceeded, which means
    /// single-dimension complexity issues are missed.
    #[test]
    fn test_high_cyclomatic_low_cognitive_should_flag() {
        let thresholds = ComplexityThresholds::default();
        // Default thresholds: cyclomatic=5, cognitive=10, total=8, length=20

        let mut high_cyclomatic = FunctionMetrics::new(
            "high_cyclomatic".to_string(),
            std::path::PathBuf::from("test.rs"),
            1,
        );
        // Very high cyclomatic (10x threshold), low cognitive (below threshold)
        high_cyclomatic.cyclomatic = 50; // Way over 5 threshold
        high_cyclomatic.cognitive = 5; // Under 10 threshold
        high_cyclomatic.length = 100; // Over 20 threshold
                                      // Total = 55, way over 8 threshold

        // This should be flagged - cyclomatic=50 is extremely complex
        assert!(
            thresholds.should_flag_function(&high_cyclomatic, FunctionRole::CoreLogic),
            "Function with cyclomatic=50 should be flagged even if cognitive is low"
        );
    }

    /// Bug fix test: A function with extremely high cognitive complexity
    /// should be flagged even if cyclomatic complexity is low.
    #[test]
    fn test_high_cognitive_low_cyclomatic_should_flag() {
        let thresholds = ComplexityThresholds::default();

        let mut high_cognitive = FunctionMetrics::new(
            "high_cognitive".to_string(),
            std::path::PathBuf::from("test.rs"),
            1,
        );
        // Low cyclomatic (below threshold), very high cognitive (5x threshold)
        high_cognitive.cyclomatic = 3; // Under 5 threshold
        high_cognitive.cognitive = 50; // Way over 10 threshold
        high_cognitive.length = 100;
        // Total = 53, way over 8 threshold

        // This should be flagged - cognitive=50 is extremely complex
        assert!(
            thresholds.should_flag_function(&high_cognitive, FunctionRole::CoreLogic),
            "Function with cognitive=50 should be flagged even if cyclomatic is low"
        );
    }

    /// Bug fix test: A very long function should be flagged even if
    /// complexity metrics are moderate.
    #[test]
    fn test_very_long_function_should_flag() {
        let thresholds = ComplexityThresholds::default();

        let mut very_long = FunctionMetrics::new(
            "very_long".to_string(),
            std::path::PathBuf::from("test.rs"),
            1,
        );
        // Moderate complexity but very long
        very_long.cyclomatic = 6; // Just over 5 threshold
        very_long.cognitive = 12; // Just over 10 threshold
        very_long.length = 500; // 25x the threshold!
                                // Total = 18, over 8 threshold

        // This should be flagged - 500 lines is way too long
        assert!(
            thresholds.should_flag_function(&very_long, FunctionRole::CoreLogic),
            "Function with 500 lines should be flagged"
        );
    }
}