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
407
408
409
410
411
//! Core analysis route handlers
//!
//! Handles: Bottleneck, Complexity, Churn, DeadCode, Defects, Dag, Satd

use crate::cli::{self, AnalyzeCommands};
use anyhow::Result;
use std::path::PathBuf;

/// Route bottleneck analysis command
pub(super) async fn route_bottleneck_analysis(cmd: AnalyzeCommands) -> Result<()> {
    if let AnalyzeCommands::Bottleneck {
        path,
        period,
        threshold,
        format,
        output,
    } = cmd
    {
        crate::cli::handlers::bottleneck_handler::handle_bottleneck(
            &path,
            &format,
            period,
            threshold,
            output.as_deref(),
        )
        .await
    } else {
        unreachable!("Expected Bottleneck command")
    }
}

/// Route complexity analysis command
pub(super) async fn route_complexity_analysis(cmd: AnalyzeCommands) -> Result<()> {
    if let AnalyzeCommands::Complexity {
        path,
        project_path,
        file,
        files,
        toolchain,
        format,
        output,
        max_cyclomatic,
        max_cognitive,
        include,
        watch,
        top_files,
        fail_on_violation,
        timeout,
        ml,
    } = cmd
    {
        // GH-97: the ML scorer is not wired into this handler. The flag used to
        // be destructured and thrown away (`ml: _`), so `analyze complexity
        // --ml` returned byte-identical JSON to a plain run — the same
        // heuristic numbers, presented under a banner promising "trained ML
        // models instead of heuristic formulas". Refuse rather than relabel.
        // The refusal is shared with `analyze tdg --ml`, which had the same bug.
        super::reject_unimplemented_ml(ml, "analyze complexity", "complexity scores")?;

        route_complexity_command(
            path,
            project_path,
            file,
            files,
            toolchain,
            format,
            output,
            max_cyclomatic,
            max_cognitive,
            include,
            watch,
            top_files,
            fail_on_violation,
            timeout,
        )
        .await
    } else {
        unreachable!("Expected Complexity command")
    }
}

/// Route churn analysis command
pub(super) async fn route_churn_analysis(cmd: AnalyzeCommands) -> Result<()> {
    if let AnalyzeCommands::Churn {
        path,
        project_path,
        days,
        format,
        output,
        top_files,
        include,
        exclude,
    } = cmd
    {
        let path = project_path.unwrap_or(path);
        crate::cli::handlers::complexity_handlers::handle_analyze_churn(
            path, days, format, output, top_files, include, exclude,
        )
        .await
    } else {
        unreachable!("Expected Churn command")
    }
}

/// Route dead code analysis command
pub(super) async fn route_dead_code_analysis(cmd: AnalyzeCommands) -> Result<()> {
    if let AnalyzeCommands::DeadCode {
        path,
        format,
        top_files,
        include_unreachable,
        min_dead_lines,
        include_tests,
        output,
        fail_on_violation,
        max_percentage,
        timeout,
        include,
        exclude,
        max_depth,
    } = cmd
    {
        crate::cli::handlers::dead_code_handlers::handle_analyze_dead_code(
            path,
            format,
            top_files,
            include_unreachable,
            min_dead_lines,
            include_tests,
            output,
            fail_on_violation,
            max_percentage,
            timeout,
            include,
            exclude,
            max_depth,
        )
        .await
    } else {
        unreachable!("Expected DeadCode command")
    }
}

/// Route defects analysis command
pub(super) async fn route_defects_analysis(cmd: AnalyzeCommands) -> Result<()> {
    use crate::cli::handlers::analyze_defects_handler::{handle_analyze_defects, OutputFormat};

    if let AnalyzeCommands::Defects {
        path,
        file,
        severity,
        format,
        output: _,
    } = cmd
    {
        // Convert format enum to handler's OutputFormat
        let output_format = match format {
            cli::DefectsOutputFormat::Text => OutputFormat::Text,
            cli::DefectsOutputFormat::Json => OutputFormat::Json,
            cli::DefectsOutputFormat::Junit => OutputFormat::Junit,
        };

        // Parse severity filter if provided
        let severity_filter = parse_defect_severity_filter(severity.as_deref())?;

        let exit_code = handle_analyze_defects(
            path.as_deref(),
            file.as_deref(),
            severity_filter,
            output_format,
        )
        .await?;

        // Exit with the handler's exit code (1 if critical defects found)
        if exit_code != 0 {
            std::process::exit(exit_code);
        }

        Ok(())
    } else {
        unreachable!("Expected Defects command")
    }
}

/// Parse `analyze defects --severity` into a filter.
///
/// `--severity` is an `Option<String>` on the clap side, so clap cannot reject a
/// bad value for us. The match used to end in `_ => None`, which is the SAME
/// value as "the flag was not given": `--severity bogus` printed the complete
/// unfiltered report and exited as if the filter had been honoured. A filter
/// nobody applied must be an error, not a silent no-op.
fn parse_defect_severity_filter(
    severity: Option<&str>,
) -> Result<Option<crate::services::defect_detector::Severity>> {
    use crate::services::defect_detector::Severity;

    let Some(raw) = severity else {
        return Ok(None);
    };
    match raw.to_lowercase().as_str() {
        "critical" => Ok(Some(Severity::Critical)),
        "high" => Ok(Some(Severity::High)),
        "medium" => Ok(Some(Severity::Medium)),
        "low" => Ok(Some(Severity::Low)),
        _ => {
            anyhow::bail!("invalid --severity '{raw}': expected one of critical, high, medium, low")
        }
    }
}

/// Route DAG analysis command
pub(super) async fn route_dag_analysis(cmd: AnalyzeCommands) -> Result<()> {
    if let AnalyzeCommands::Dag {
        dag_type,
        path,
        project_path,
        output,
        max_depth,
        target_nodes,
        filter_external,
        show_complexity,
        include_duplicates,
        include_dead_code,
        enhanced,
    } = cmd
    {
        let path = project_path.unwrap_or(path);
        crate::cli::handlers::complexity_handlers::handle_analyze_dag(
            dag_type,
            path,
            output,
            max_depth,
            target_nodes,
            filter_external,
            show_complexity,
            include_duplicates,
            include_dead_code,
            enhanced,
        )
        .await
    } else {
        unreachable!("Expected Dag command")
    }
}
/// Route SATD analysis command
pub(super) async fn route_satd_analysis(cmd: AnalyzeCommands) -> Result<()> {
    if let AnalyzeCommands::Satd {
        path,
        format,
        severity,
        critical_only,
        include_tests,
        strict,
        evolution,
        days,
        metrics,
        output,
        top_files,
        fail_on_violation,
        timeout,
        include,
        exclude,
        extended,
    } = cmd
    {
        use crate::cli::handlers::satd_handler::SatdAnalysisConfig;

        let config = SatdAnalysisConfig {
            path,
            format,
            severity,
            critical_only,
            include_tests,
            strict,
            evolution,
            days,
            metrics,
            output,
            top_files,
            fail_on_violation,
            timeout,
            include,
            exclude,
            extended,
        };

        crate::cli::handlers::satd_handler::handle_analyze_satd(config).await
    } else {
        unreachable!("Expected Satd command")
    }
}

/// Route complexity command (cognitive complexity ≤5)
#[allow(clippy::too_many_arguments)]
async fn route_complexity_command(
    path: PathBuf,
    project_path: Option<PathBuf>,
    file: Option<PathBuf>,
    files: Vec<PathBuf>,
    toolchain: Option<String>,
    format: crate::cli::ComplexityOutputFormat,
    output: Option<PathBuf>,
    max_cyclomatic: Option<u16>,
    max_cognitive: Option<u16>,
    include: Vec<String>,
    watch: bool,
    top_files: usize,
    fail_on_violation: bool,
    timeout: u64,
) -> Result<()> {
    // Handle parameter migration: use new 'path' or deprecated 'project_path'
    // Silently accept both for backwards compatibility
    let analysis_path = project_path.unwrap_or(path);

    crate::cli::handlers::complexity_handlers::handle_analyze_complexity(
        analysis_path,
        file,
        files,
        toolchain,
        format,
        output,
        max_cyclomatic,
        max_cognitive,
        include,
        watch,
        top_files,
        fail_on_violation,
        timeout,
    )
    .await
}

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

    fn complexity_command(ml: bool) -> AnalyzeCommands {
        AnalyzeCommands::Complexity {
            path: PathBuf::from("."),
            project_path: None,
            file: None,
            files: Vec::new(),
            toolchain: None,
            format: cli::ComplexityOutputFormat::Summary,
            output: None,
            max_cyclomatic: None,
            max_cognitive: None,
            include: Vec::new(),
            watch: false,
            top_files: 10,
            fail_on_violation: false,
            timeout: 60,
            ml,
        }
    }

    /// `--ml` was destructured and discarded, so `analyze complexity --ml`
    /// produced byte-identical JSON to a plain run: the heuristic scores under
    /// a banner promising trained ML models. Refusing is honest; silently
    /// relabelling is not.
    #[tokio::test]
    async fn ml_flag_is_refused_rather_than_ignored() {
        let err = route_complexity_analysis(complexity_command(true))
            .await
            .expect_err("--ml must not silently return heuristic scores");
        let msg = err.to_string();
        assert!(
            msg.contains("--ml is not implemented"),
            "unexpected error: {msg}"
        );
    }

    // ── --severity is validated, not silently dropped ───────────────────────

    /// The reported defect: `analyze defects --severity bogus` mapped to
    /// `None`, i.e. exactly what "no --severity at all" means, so the caller
    /// got the full unfiltered report and exit 0.
    #[test]
    fn unrecognised_severity_is_rejected() {
        let err = parse_defect_severity_filter(Some("bogus"))
            .expect_err("an unknown --severity value must not mean 'no filter'");
        let msg = err.to_string();
        assert!(msg.contains("bogus"), "error must quote the value: {msg}");
        assert!(
            msg.contains("critical") && msg.contains("low"),
            "error must list the accepted values: {msg}"
        );
    }

    #[test]
    fn recognised_severities_parse_case_insensitively() {
        use crate::services::defect_detector::Severity;
        assert_eq!(
            parse_defect_severity_filter(Some("Critical")).unwrap(),
            Some(Severity::Critical)
        );
        assert_eq!(
            parse_defect_severity_filter(Some("high")).unwrap(),
            Some(Severity::High)
        );
        assert_eq!(
            parse_defect_severity_filter(Some("MEDIUM")).unwrap(),
            Some(Severity::Medium)
        );
        assert_eq!(
            parse_defect_severity_filter(Some("low")).unwrap(),
            Some(Severity::Low)
        );
        assert_eq!(parse_defect_severity_filter(None).unwrap(), None);
    }
}