repotoire 0.3.112

Graph-powered code analysis CLI. 114 detectors for security, architecture, and code quality.
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
//! Message Chain detector for Law of Demeter violations
//!
//! Detects long method chains like: a.b().c().d().e()
//! These violate the Law of Demeter by coupling to internal object structure.
//!
//! Uses both:
//! - Source code pattern matching for inline chains
//! - Call graph analysis for cross-function delegation chains

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

static CHAIN_PATTERN: OnceLock<Regex> = OnceLock::new();

fn chain_pattern() -> &'static Regex {
    CHAIN_PATTERN.get_or_init(|| {
        // Match method chains: .method().method().method()
        // At least 4 chained calls
        Regex::new(r"(\.[a-zA-Z_][a-zA-Z0-9_]*\s*\([^)]*\)){4,}").expect("valid regex")
    })
}

/// Thresholds for message chain detection
#[derive(Debug, Clone)]
pub struct MessageChainThresholds {
    /// Minimum chain depth to report
    pub min_chain_depth: usize,
    /// Chain depth for high severity
    pub high_severity_depth: usize,
}

impl Default for MessageChainThresholds {
    fn default() -> Self {
        Self {
            min_chain_depth: 5,     // 4 was too aggressive — A→B→C→D is normal abstraction
            high_severity_depth: 8, // 6 was too aggressive for High severity
        }
    }
}

/// Patterns to exclude (builder patterns, fluent APIs)
const EXCLUDE_PATTERNS: &[&str] = &[
    "builder", "with_", "set_", "add_", "and_", "or_", "filter", "map", "reduce", "collect",
    "iter", "select", "where", "order_by", "group_by", "join", "expect", "unwrap", "ok", "err",
    "and_then",
];

/// Detects Law of Demeter violations
pub struct MessageChainDetector {
    config: DetectorConfig,
    thresholds: MessageChainThresholds,
    repository_path: PathBuf,
}

impl MessageChainDetector {
    pub fn new(repository_path: impl Into<PathBuf>) -> Self {
        Self {
            config: DetectorConfig::new(),
            thresholds: MessageChainThresholds::default(),
            repository_path: repository_path.into(),
        }
    }

    #[allow(dead_code)] // Builder pattern method
    pub fn with_config(config: DetectorConfig, repository_path: impl Into<PathBuf>) -> Self {
        let thresholds = MessageChainThresholds {
            min_chain_depth: config.get_option_or("min_chain_depth", 4),
            high_severity_depth: config.get_option_or("high_severity_depth", 6),
        };
        Self {
            config,
            thresholds,
            repository_path: repository_path.into(),
        }
    }

    /// Check if chain is a fluent API pattern (not a violation)
    fn is_fluent_pattern(&self, chain: &str) -> bool {
        let lower = chain.to_lowercase();
        EXCLUDE_PATTERNS.iter().any(|p| lower.contains(p))
    }

    /// Count depth of a method chain
    fn count_chain_depth(&self, chain: &str) -> usize {
        // Count method calls: .name()
        chain.matches(").").count() + 1
    }

    fn calculate_severity(&self, depth: usize) -> Severity {
        if depth >= self.thresholds.high_severity_depth {
            Severity::High
        } else {
            Severity::Medium
        }
    }

    /// Scan source files for method chains
    fn scan_source_files(&self) -> Vec<Finding> {
        let mut findings = Vec::new();
        let mut seen: HashSet<(String, u32)> = HashSet::new();

        let walker = ignore::WalkBuilder::new(&self.repository_path)
            .hidden(false)
            .git_ignore(true)
            .build();

        for entry in walker.filter_map(|e| e.ok()) {
            let path = entry.path();
            if !path.is_file() {
                continue;
            }

            let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
            if !matches!(ext, "py" | "js" | "ts" | "java" | "go" | "rs" | "rb") {
                continue;
            }

            // Skip test files
            let path_str = path.to_string_lossy();
            if path_str.contains("/test") || path_str.contains("_test.") {
                continue;
            }

            // Skip non-production paths
            if crate::detectors::content_classifier::is_non_production_path(&path_str) {
                continue;
            }

            let rel_path = path
                .strip_prefix(&self.repository_path)
                .unwrap_or(path)
                .to_path_buf();

            if let Some(content) = crate::cache::global_cache().get_content(path) {
                for (i, line) in content.lines().enumerate() {
                    let line_num = (i + 1) as u32;

                    // Skip comments
                    let trimmed = line.trim();
                    if trimmed.starts_with("//")
                        || trimmed.starts_with("#")
                        || trimmed.starts_with("*")
                    {
                        continue;
                    }

                    if let Some(m) = chain_pattern().find(line) {
                        let chain = m.as_str();

                        // Skip fluent APIs
                        if self.is_fluent_pattern(chain) {
                            continue;
                        }

                        let depth = self.count_chain_depth(chain);
                        if depth < self.thresholds.min_chain_depth {
                            continue;
                        }

                        // Deduplicate
                        let key = (rel_path.to_string_lossy().to_string(), line_num);
                        if seen.contains(&key) {
                            continue;
                        }
                        seen.insert(key);

                        let severity = self.calculate_severity(depth);

                        findings.push(Finding {
                            id: String::new(),
                            detector: "MessageChainDetector".to_string(),
                            severity,
                            title: format!("Law of Demeter violation: {}-level chain", depth),
                            description: format!(
                                "Method chain with **{} levels** found:\n```\n{}\n```\n\n\
                                 This violates the Law of Demeter by coupling to internal object structure.",
                                depth, chain.trim()
                            ),
                            affected_files: vec![rel_path.clone()],
                            line_start: Some(line_num),
                            line_end: Some(line_num),
                            suggested_fix: Some(
                                "Options:\n\
                                 1. Add a delegate method on the first object\n\
                                 2. Use Tell, Don't Ask - have the object do the work\n\
                                 3. Create a Facade to hide the chain"
                                    .to_string()
                            ),
                            estimated_effort: Some("Small (30 min)".to_string()),
                            category: Some("coupling".to_string()),
                            cwe_id: None,
                            why_it_matters: Some(
                                "Long method chains couple your code to internal object structure. \
                                 Changes to intermediate objects break the chain."
                                    .to_string()
                            ),
                            ..Default::default()
                        });
                    }
                }
            }
        }

        findings
    }

    /// Use call graph to find delegation chains across functions.
    ///
    /// A delegation chain is a sequence of functions where each one just calls
    /// the next with minimal logic — pure pass-through indirection.
    ///
    /// We only report the chain HEAD (the entry point) to avoid N findings
    /// for a single chain.
    fn find_delegation_chains(&self, graph: &dyn crate::graph::GraphQuery) -> Vec<Finding> {
        let mut findings = Vec::new();
        let mut reported_in_chain: HashSet<String> = HashSet::new();

        for func in graph.get_functions() {
            // Skip if already reported as part of another chain
            if reported_in_chain.contains(&func.qualified_name) {
                continue;
            }

            let callees = graph.get_callees(&func.qualified_name);
            let callers = graph.get_callers(&func.qualified_name);

            // Chain HEAD: has callers > 1 OR callers == 0, but single callee with low complexity
            // This means it's the entry point of a chain, not a middle link
            let is_chain_head = callers.len() != 1 && callees.len() == 1;
            if !is_chain_head {
                continue;
            }

            let complexity = func.complexity().unwrap_or(1);
            if complexity > 3 {
                continue; // Not a pass-through
            }

            // Trace the chain forward
            let (chain_depth, chain_members) =
                self.trace_chain_with_members(graph, &func.qualified_name, 0);

            if chain_depth < self.thresholds.min_chain_depth as i32 {
                continue;
            }

            // Skip trait delegation chains — when most links in the chain have the
            // same function name (e.g. get_callers → self.inner.get_callers → ...).
            // This is a standard Rust/OOP pattern for trait forwarding, not a design issue.
            if self.is_trait_delegation_chain(&chain_members) {
                debug!(
                    "Skipping trait delegation chain starting at {} ({} levels, same-name forwarding)",
                    func.name, chain_depth
                );
                for member in &chain_members {
                    reported_in_chain.insert(member.clone());
                }
                continue;
            }

            // Skip chains where all functions are in the same file
            // (same-file decomposition is usually intentional)
            let all_funcs = graph.get_functions();
            let files_in_chain: HashSet<String> = chain_members
                .iter()
                .filter_map(|qn| {
                    all_funcs
                        .iter()
                        .find(|f| f.qualified_name == *qn)
                        .map(|f| f.file_path.clone())
                })
                .collect();
            if files_in_chain.len() <= 1 {
                continue; // All in same file — normal decomposition
            }

            // Mark all chain members as reported
            for member in &chain_members {
                reported_in_chain.insert(member.clone());
            }

            // Only flag with Low severity — delegation chains are a style observation
            let severity = if chain_depth >= self.thresholds.high_severity_depth as i32 {
                Severity::Medium
            } else {
                Severity::Low
            };

            findings.push(Finding {
                id: String::new(),
                detector: "MessageChainDetector".to_string(),
                severity,
                title: format!("Delegation chain: {} starts a {}-level chain", func.name, chain_depth),
                description: format!(
                    "Function '{}' is the entry point of a {}-level delegation chain across {} files.\n\n\
                     Each function in the chain just delegates to the next with minimal logic. \
                     Consider collapsing intermediate layers.",
                    func.name, chain_depth, files_in_chain.len()
                ),
                affected_files: vec![func.file_path.clone().into()],
                line_start: Some(func.line_start),
                line_end: Some(func.line_end),
                suggested_fix: Some("Consider collapsing the delegation chain or using direct access".to_string()),
                estimated_effort: Some("Medium (1-2 hours)".to_string()),
                category: Some("coupling".to_string()),
                cwe_id: None,
                why_it_matters: Some("Deep delegation chains add indirection without value".to_string()),
                ..Default::default()
            });
        }

        findings
    }

    /// Check if a chain is trait delegation — most members share the same function name.
    /// e.g. GraphStore::get_callers → CompactGraphStore::get_callers → MmapStore::get_callers
    fn is_trait_delegation_chain(&self, chain_members: &[String]) -> bool {
        if chain_members.len() < 3 {
            return false;
        }

        // Extract the simple function name from each qualified name (last segment after ::)
        let names: Vec<&str> = chain_members
            .iter()
            .filter_map(|qn| qn.rsplit("::").next())
            .collect();

        if names.is_empty() {
            return false;
        }

        // Count how many share the most common name
        let mut freq: HashMap<&str, usize> = HashMap::new();
        for name in &names {
            *freq.entry(name).or_default() += 1;
        }

        let max_freq = freq.values().copied().max().unwrap_or(0);
        // If >50% of chain members share the same function name, it's trait delegation
        max_freq * 2 > names.len()
    }

    /// Trace how deep a delegation chain goes, collecting member names.
    #[allow(clippy::only_used_in_recursion)]
    fn trace_chain_with_members(
        &self,
        graph: &dyn crate::graph::GraphQuery,
        qn: &str,
        depth: i32,
    ) -> (i32, Vec<String>) {
        if depth > 10 {
            return (depth, vec![qn.to_string()]);
        }

        let callees = graph.get_callees(qn);
        if callees.len() != 1 {
            return (depth, vec![qn.to_string()]);
        }

        // Check callee is also a pass-through (low complexity, single callee)
        let callee = &callees[0];
        let complexity = callee.complexity().unwrap_or(1);
        if complexity > 3 {
            return (
                depth + 1,
                vec![qn.to_string(), callee.qualified_name.clone()],
            );
        }

        let (sub_depth, mut members) =
            self.trace_chain_with_members(graph, &callee.qualified_name, depth + 1);
        members.insert(0, qn.to_string());
        (sub_depth, members)
    }
}

impl Default for MessageChainDetector {
    fn default() -> Self {
        Self::new(".")
    }
}

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

    fn description(&self) -> &'static str {
        "Detects Law of Demeter violations through long method chains"
    }

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

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

    fn detect(&self, graph: &dyn crate::graph::GraphQuery) -> Result<Vec<Finding>> {
        let mut findings = Vec::new();

        // Source code scanning for inline chains
        findings.extend(self.scan_source_files());

        // Graph analysis for delegation chains
        findings.extend(self.find_delegation_chains(graph));

        info!("MessageChainDetector found {} findings", findings.len());
        Ok(findings)
    }
}

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

    #[test]
    fn test_is_fluent_pattern() {
        let detector = MessageChainDetector::new(".");

        assert!(detector.is_fluent_pattern(".filter().map().collect()"));
        assert!(detector.is_fluent_pattern(".with_name().with_age().build()"));
        assert!(!detector.is_fluent_pattern(".get_user().get_profile().get_settings()"));
    }

    #[test]
    fn test_count_chain_depth() {
        let detector = MessageChainDetector::new(".");

        // .a().b() = 2 calls (a, b)
        assert_eq!(detector.count_chain_depth(".a().b()"), 2);
        // .a().b().c().d() = 4 calls
        assert_eq!(detector.count_chain_depth(".a().b().c().d()"), 4);
    }

    #[test]
    fn test_severity() {
        let detector = MessageChainDetector::new(".");

        assert_eq!(detector.calculate_severity(5), Severity::Medium);
        assert_eq!(detector.calculate_severity(7), Severity::Medium);
        assert_eq!(detector.calculate_severity(8), Severity::High);
    }
}