pmat 3.30.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP)
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
#![allow(unused)]
#![cfg_attr(coverage_nightly, coverage(off))]
//! Complexity Analysis Facade
//!
//! Provides a simplified interface for complexity analysis operations.

use crate::services::service_registry::ServiceRegistry;
use anyhow::Result;
use serde::Serialize;
use std::path::Path;
use std::sync::Arc;

/// Request for complexity analysis
#[derive(Debug, Clone)]
pub struct ComplexityAnalysisRequest {
    pub path: std::path::PathBuf,
    pub language: Option<String>,
    pub include_tests: bool,
    pub max_complexity_threshold: Option<u32>,
    pub output_format: ComplexityOutputFormat,
}

/// Output format options for complexity analysis
#[derive(Debug, Clone)]
pub enum ComplexityOutputFormat {
    Json,
    Summary,
    Detailed,
}

/// Result of complexity analysis
#[derive(Debug, Clone, Serialize)]
pub struct ComplexityAnalysisResult {
    pub total_files: usize,
    pub violations: Vec<ComplexityViolation>,
    pub average_complexity: f64,
    pub max_complexity: u32,
    pub summary: String,
}

/// Individual complexity violation
#[derive(Debug, Clone, Serialize)]
pub struct ComplexityViolation {
    pub file_path: String,
    pub function_name: String,
    pub line_number: usize,
    pub complexity: u32,
    pub complexity_type: String,
}

/// Facade for complexity analysis operations
#[derive(Clone)]
pub struct ComplexityFacade {
    registry: Arc<ServiceRegistry>,
}

impl ComplexityFacade {
    /// Create a new complexity facade
    #[must_use]
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    pub fn new(registry: Arc<ServiceRegistry>) -> Self {
        Self { registry }
    }

    /// Perform complexity analysis on a project
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    pub async fn analyze_project(
        &self,
        request: ComplexityAnalysisRequest,
    ) -> Result<ComplexityAnalysisResult> {
        // Wired to the same analyzer `pmat analyze complexity` uses.
        //
        // This returned a fixed `example_function` at line 42 with complexity
        // 15 for every project — two unrelated crates produced byte-identical
        // "analysis". `analyze comprehensive` reaches this facade through the
        // orchestrator, so that fabricated violation was what users and CI
        // gates actually saw.
        use crate::cli::analysis_utilities::analyze_project_files;
        use crate::services::complexity::{aggregate_results_with_thresholds, Violation};

        const MAX_CYCLOMATIC: u16 = 20;
        const MAX_COGNITIVE: u16 = 15;

        let file_metrics = analyze_project_files(
            &request.path,
            request.language.as_deref(),
            &[],
            MAX_CYCLOMATIC,
            MAX_COGNITIVE,
        )
        .await?;

        let total_files = file_metrics.len();
        let report = aggregate_results_with_thresholds(
            file_metrics,
            Some(MAX_CYCLOMATIC),
            Some(MAX_COGNITIVE),
        );

        let violations: Vec<ComplexityViolation> = report
            .violations
            .iter()
            .map(|v| {
                let (file, function, value, line, kind) = match v {
                    Violation::Error {
                        file,
                        function,
                        value,
                        line,
                        rule,
                        ..
                    }
                    | Violation::Warning {
                        file,
                        function,
                        value,
                        line,
                        rule,
                        ..
                    } => (file, function, *value, *line, rule),
                };
                ComplexityViolation {
                    file_path: file.clone(),
                    function_name: function.clone().unwrap_or_else(|| "<file>".to_string()),
                    line_number: line as usize,
                    complexity: value as u32,
                    complexity_type: kind.clone(),
                }
            })
            .collect();

        let max_complexity = violations.iter().map(|v| v.complexity).max().unwrap_or(0);
        let average_complexity = mean_cyclomatic(&report.files);

        Ok(ComplexityAnalysisResult {
            total_files,
            summary: format!(
                "Analyzed {} file(s) in {} with {} violation(s)",
                total_files,
                request.path.display(),
                violations.len()
            ),
            violations,
            average_complexity,
            max_complexity,
        })
    }

    /// Analyze a single file for complexity
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
    pub async fn analyze_file<P: AsRef<Path>>(
        &self,
        path: P,
        language: Option<&str>,
    ) -> Result<ComplexityAnalysisResult> {
        let request = ComplexityAnalysisRequest {
            path: path.as_ref().to_path_buf(),
            language: language.map(std::string::ToString::to_string),
            include_tests: true,
            max_complexity_threshold: Some(20),
            output_format: ComplexityOutputFormat::Detailed,
        };

        self.analyze_project(request).await
    }

    /// Get complexity thresholds for different languages
    #[must_use]
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    pub fn get_language_thresholds(&self, language: &str) -> ComplexityThresholds {
        match language {
            "rust" => ComplexityThresholds {
                warning: 15,
                error: 25,
                max_acceptable: 20,
            },
            "typescript" | "javascript" => ComplexityThresholds {
                warning: 10,
                error: 20,
                max_acceptable: 15,
            },
            "python" => ComplexityThresholds {
                warning: 12,
                error: 20,
                max_acceptable: 15,
            },
            _ => ComplexityThresholds {
                warning: 10,
                error: 20,
                max_acceptable: 15,
            },
        }
    }

    /// Check if complexity violations exceed thresholds
    #[must_use]
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    pub fn validate_complexity(
        &self,
        result: &ComplexityAnalysisResult,
        language: &str,
    ) -> ValidationResult {
        let thresholds = self.get_language_thresholds(language);

        let warnings = result
            .violations
            .iter()
            .filter(|v| v.complexity >= thresholds.warning && v.complexity < thresholds.error)
            .count();

        let errors = result
            .violations
            .iter()
            .filter(|v| v.complexity >= thresholds.error)
            .count();

        ValidationResult {
            passed: errors == 0,
            warnings,
            errors,
            max_complexity: result.max_complexity,
            threshold_exceeded: result.max_complexity > thresholds.max_acceptable,
        }
    }
}

/// Mean cyclomatic complexity over every analysed function, including class
/// methods — the same population `summary.total_functions` counts.
///
/// `average_complexity` used to be assigned `summary.median_cyclomatic`, so a
/// crate of nine trivial functions plus one function of cyclomatic 81 published
/// an "average" of 1.0 where the mean is 9.0. A median published under the name
/// `average` is a mislabelled number, not an approximation of one.
fn mean_cyclomatic(files: &[crate::services::complexity::FileComplexityMetrics]) -> f64 {
    let mut sum: u64 = 0;
    let mut count: u64 = 0;

    for file in files {
        for func in &file.functions {
            sum += u64::from(func.metrics.cyclomatic);
            count += 1;
        }
        for class in &file.classes {
            for method in &class.methods {
                sum += u64::from(method.metrics.cyclomatic);
                count += 1;
            }
        }
    }

    if count == 0 {
        0.0
    } else {
        sum as f64 / count as f64
    }
}

/// Complexity thresholds for different severity levels
#[derive(Debug, Clone)]
pub struct ComplexityThresholds {
    pub warning: u32,
    pub error: u32,
    pub max_acceptable: u32,
}

/// Result of complexity validation
#[derive(Debug, Clone)]
pub struct ValidationResult {
    pub passed: bool,
    pub warnings: usize,
    pub errors: usize,
    pub max_complexity: u32,
    pub threshold_exceeded: bool,
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;
    use crate::services::service_registry::ServiceRegistry;

    #[tokio::test]
    async fn test_complexity_facade_creation() {
        let registry = Arc::new(ServiceRegistry::new());
        let facade = ComplexityFacade::new(registry);

        // Test basic facade functionality
        let thresholds = facade.get_language_thresholds("rust");
        assert_eq!(thresholds.warning, 15);
        assert_eq!(thresholds.error, 25);
    }

    #[tokio::test]
    async fn test_complexity_validation() {
        let registry = Arc::new(ServiceRegistry::new());
        let facade = ComplexityFacade::new(registry);

        let result = ComplexityAnalysisResult {
            total_files: 1,
            violations: vec![ComplexityViolation {
                file_path: "test.rs".to_string(),
                function_name: "test_fn".to_string(),
                line_number: 1,
                complexity: 30,
                complexity_type: "cyclomatic".to_string(),
            }],
            average_complexity: 30.0,
            max_complexity: 30,
            summary: "Test".to_string(),
        };

        let validation = facade.validate_complexity(&result, "rust");
        assert!(!validation.passed);
        assert_eq!(validation.errors, 1);
        assert!(validation.threshold_exceeded);
    }

    #[test]
    fn test_mean_cyclomatic_is_the_mean_not_the_median() {
        use crate::services::complexity::{
            ComplexityMetrics, FileComplexityMetrics, FunctionComplexity,
        };

        fn metrics(cyclomatic: u16) -> ComplexityMetrics {
            ComplexityMetrics {
                cyclomatic,
                cognitive: 0,
                nesting_max: 0,
                lines: 1,
                halstead: None,
            }
        }

        // The cx2 repro: nine trivial functions plus one of cyclomatic 81.
        // Median 1.0, mean 9.0 — `average_complexity` published the median.
        let mut functions: Vec<FunctionComplexity> = (0..9)
            .map(|i| FunctionComplexity {
                name: format!("triv{i}"),
                line_start: i,
                line_end: i,
                metrics: metrics(1),
            })
            .collect();
        functions.push(FunctionComplexity {
            name: "nasty".to_string(),
            line_start: 10,
            line_end: 90,
            metrics: metrics(81),
        });

        let file = FileComplexityMetrics {
            path: "src/lib.rs".to_string(),
            total_complexity: metrics(90),
            functions,
            classes: vec![],
        };

        let mean = mean_cyclomatic(std::slice::from_ref(&file));
        assert!(
            (mean - 9.0).abs() < f64::EPSILON,
            "expected the mean 9.0, got {mean}"
        );
    }

    #[test]
    fn test_mean_cyclomatic_counts_class_methods_and_handles_empty() {
        use crate::services::complexity::{
            ClassComplexity, ComplexityMetrics, FileComplexityMetrics, FunctionComplexity,
        };

        let m = |c: u16| ComplexityMetrics {
            cyclomatic: c,
            cognitive: 0,
            nesting_max: 0,
            lines: 1,
            halstead: None,
        };

        assert_eq!(mean_cyclomatic(&[]), 0.0);

        let file = FileComplexityMetrics {
            path: "src/lib.rs".to_string(),
            total_complexity: m(9),
            functions: vec![FunctionComplexity {
                name: "free".to_string(),
                line_start: 1,
                line_end: 2,
                metrics: m(3),
            }],
            classes: vec![ClassComplexity {
                name: "C".to_string(),
                line_start: 3,
                line_end: 9,
                metrics: m(6),
                methods: vec![FunctionComplexity {
                    name: "method".to_string(),
                    line_start: 4,
                    line_end: 5,
                    metrics: m(7),
                }],
            }],
        };

        assert_eq!(mean_cyclomatic(std::slice::from_ref(&file)), 5.0);
    }
}