mir-php 0.16.1

Fast PHP static analyzer
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/// Project-level configuration parsed from `mir.xml`.
use std::collections::HashMap;
use std::path::{Path, PathBuf};

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

/// Per-issue severity override from `<issueHandlers>`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorLevel {
    Error,
    Warning,
    Info,
    Suppress,
}

impl ErrorLevel {
    fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "error" => Some(Self::Error),
            "warning" | "warn" => Some(Self::Warning),
            "info" | "notice" => Some(Self::Info),
            "suppress" | "none" => Some(Self::Suppress),
            _ => None,
        }
    }
}

/// Parsed contents of `mir.xml`.
#[derive(Debug, Clone)]
pub struct Config {
    /// Source directories to analyze (from `<projectFiles>`).
    pub project_dirs: Vec<String>,
    /// Directories/files to skip (from `<ignoreFiles>`).
    pub ignore_dirs: Vec<String>,
    /// Per-issue-kind severity overrides from `<issueHandlers>`.
    pub issue_handlers: HashMap<String, ErrorLevel>,
    /// Global error level 1–8 (lower = stricter). 1 = errors only, 2 = +warnings, 3+ = +info.
    pub error_level: u8,
    /// Target PHP version string (e.g. `"8.2"`). Accepts both root attribute and child element.
    pub php_version: Option<String>,
    /// Whether dead-code detection is enabled.
    pub find_unused_code: bool,
    /// Whether unused-variable checking is enabled.
    pub find_unused_variables: bool,
    /// External stub files to load (from `<stubs><file name="..."/>`).
    pub stub_files: Vec<String>,
    /// External stub directories to load (from `<stubs><directory name="..."/>`).
    pub stub_dirs: Vec<String>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            project_dirs: Vec::new(),
            ignore_dirs: Vec::new(),
            issue_handlers: HashMap::new(),
            error_level: 2,
            php_version: None,
            find_unused_code: false,
            find_unused_variables: false,
            stub_files: Vec::new(),
            stub_dirs: Vec::new(),
        }
    }
}

/// Errors that can occur when loading configuration.
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
    #[error("cannot read config file: {0}")]
    Io(String),
    #[error("XML parse error: {0}")]
    Parse(String),
}

// ---------------------------------------------------------------------------
// Config impl
// ---------------------------------------------------------------------------

impl Config {
    /// Walk from `start_dir` upward looking for `mir.xml` (or `psalm.xml` as a compatibility fallback).
    /// Returns the path if found.
    pub fn find(start_dir: &Path) -> Option<PathBuf> {
        let mut dir = start_dir.to_path_buf();
        loop {
            let mir = dir.join("mir.xml");
            if mir.exists() {
                return Some(mir);
            }
            let psalm = dir.join("psalm.xml");
            if psalm.exists() {
                return Some(psalm);
            }
            if !dir.pop() {
                return None;
            }
        }
    }

    /// Load and parse `mir.xml` at the given path.
    pub fn from_file(path: &Path) -> Result<Self, ConfigError> {
        let xml = std::fs::read_to_string(path).map_err(|e| ConfigError::Io(e.to_string()))?;
        Self::parse(&xml)
    }

    /// Parse `mir.xml` from a string.
    pub fn parse(xml: &str) -> Result<Self, ConfigError> {
        parse_xml(xml)
    }
}

// ---------------------------------------------------------------------------
// XML parser (quick-xml event API)
// ---------------------------------------------------------------------------

fn parse_xml(xml: &str) -> Result<Config, ConfigError> {
    use quick_xml::events::Event;
    use quick_xml::Reader;

    let mut reader = Reader::from_str(xml);
    reader.config_mut().trim_text(true);

    let mut config = Config::default();
    // Element path stack, e.g. ["mir", "projectFiles"]
    let mut path: Vec<String> = Vec::new();
    // Accumulated text content for the current element
    let mut text_buf = String::new();

    loop {
        match reader.read_event() {
            Ok(Event::Start(e)) => {
                let name = bytes_to_string(e.name().as_ref());

                // phpVersion as attribute on the root element: <mir phpVersion="8.2">
                if path.is_empty() && (name == "mir" || name == "psalm") {
                    for attr in e.attributes().flatten() {
                        if bytes_to_string(attr.key.as_ref()) == "phpVersion"
                            && config.php_version.is_none()
                        {
                            let val = bytes_to_string(&attr.value);
                            if !val.is_empty() {
                                config.php_version = Some(val);
                            }
                        }
                    }
                }

                // Issue handler: <SomeIssueKind errorLevel="..." />  inside <issueHandlers>
                if path.last().is_some_and(|s: &String| s == "issueHandlers") {
                    for attr in e.attributes().flatten() {
                        if bytes_to_string(attr.key.as_ref()) == "errorLevel" {
                            if let Some(level) = ErrorLevel::from_str(&bytes_to_string(&attr.value))
                            {
                                config.issue_handlers.insert(name.clone(), level);
                            }
                        }
                    }
                }

                // <directory name="..."> inside <projectFiles> or <ignoreFiles>
                if name == "directory" {
                    collect_directory(&e, &path, &mut config);
                }

                // <file name="..."> or <directory name="..."> inside <stubs>
                if name == "file" || name == "directory" {
                    collect_stub_entry(&e, &path, &mut config);
                }

                text_buf.clear();
                path.push(name);
            }

            // Self-closing elements like <UndefinedVariable errorLevel="suppress" />
            Ok(Event::Empty(e)) => {
                let name = bytes_to_string(e.name().as_ref());

                if path.last().is_some_and(|s: &String| s == "issueHandlers") {
                    for attr in e.attributes().flatten() {
                        if bytes_to_string(attr.key.as_ref()) == "errorLevel" {
                            if let Some(level) = ErrorLevel::from_str(&bytes_to_string(&attr.value))
                            {
                                config.issue_handlers.insert(name.clone(), level);
                            }
                        }
                    }
                }

                if name == "directory" {
                    collect_directory(&e, &path, &mut config);
                }

                // <file name="..."/> or <directory name="..."/> inside <stubs>
                if name == "file" || name == "directory" {
                    collect_stub_entry(&e, &path, &mut config);
                }
            }

            Ok(Event::Text(t)) => {
                text_buf = t
                    .xml_content()
                    .map_err(|e| ConfigError::Parse(e.to_string()))?
                    .to_string();
            }

            Ok(Event::End(_)) => {
                let key = path.pop().unwrap_or_default();
                let parent = path.last().map_or("", |s| s.as_str());
                match (key.as_str(), parent) {
                    ("phpVersion", _) if !text_buf.is_empty() => {
                        config.php_version = Some(text_buf.clone());
                    }
                    ("errorLevel", "mir") => {
                        if let Ok(n) = text_buf.parse::<u8>() {
                            config.error_level = n.clamp(1, 8);
                        }
                    }
                    ("findUnusedCode", _) => {
                        config.find_unused_code = text_buf == "true";
                    }
                    ("findUnusedVariables", _) => {
                        config.find_unused_variables = text_buf == "true";
                    }
                    _ => {}
                }
                text_buf.clear();
            }

            Ok(Event::Eof) => break,
            Err(e) => return Err(ConfigError::Parse(e.to_string())),
            _ => {}
        }
    }

    Ok(config)
}

/// Extract `name` attribute from a `<directory name="..."/>` element and push to the
/// right list based on the current element path.
fn collect_directory<'a>(
    e: &quick_xml::events::BytesStart<'a>,
    path: &[String],
    config: &mut Config,
) {
    let parent = path.last().map_or("", |s| s.as_str());
    for attr in e.attributes().flatten() {
        if bytes_to_string(attr.key.as_ref()) == "name" {
            let val = bytes_to_string(&attr.value);
            match parent {
                "projectFiles" => config.project_dirs.push(val),
                "ignoreFiles" => config.ignore_dirs.push(val),
                _ => {}
            }
        }
    }
}

/// Handle `<file name="..."/>` and `<directory name="..."/>` inside `<stubs>`.
fn collect_stub_entry<'a>(
    e: &quick_xml::events::BytesStart<'a>,
    path: &[String],
    config: &mut Config,
) {
    if path.last().map_or("", |s| s.as_str()) != "stubs" {
        return;
    }
    let elem = bytes_to_string(e.name().as_ref());
    for attr in e.attributes().flatten() {
        if bytes_to_string(attr.key.as_ref()) == "name" {
            let val = bytes_to_string(&attr.value);
            match elem.as_str() {
                "file" => config.stub_files.push(val),
                "directory" => config.stub_dirs.push(val),
                _ => {}
            }
        }
    }
}

fn bytes_to_string(b: &[u8]) -> String {
    String::from_utf8_lossy(b).into_owned()
}

// ---------------------------------------------------------------------------
// Baseline
// ---------------------------------------------------------------------------

/// Parsed contents of a baseline XML (`baseline.xml` / `psalm-baseline.xml`).
///
/// Structure: `file_path → issue_kind → [code_snippets]`
///
/// A code snippet is the trimmed source text of the flagged expression — the
/// `<code>` element inside a baseline entry.  Matching is done by
/// (file, issue_kind, snippet) so that refactors that change line numbers
/// do not invalidate the baseline.
#[derive(Debug, Clone, Default)]
pub struct Baseline {
    /// Outer key: source-relative file path (e.g. `"application/server/Foo.php"`).
    /// Inner key: issue kind name (e.g. `"InvalidArgument"`).
    /// Value: sorted vec of code snippets to consume (each entry is used once).
    pub entries: HashMap<String, HashMap<String, Vec<String>>>,
}

impl Baseline {
    /// Load a baseline from a file path.
    pub fn from_file(path: &std::path::Path) -> Result<Self, ConfigError> {
        let xml = std::fs::read_to_string(path).map_err(|e| ConfigError::Io(e.to_string()))?;
        Self::parse(&xml)
    }

    /// Parse a baseline XML string.
    pub fn parse(xml: &str) -> Result<Self, ConfigError> {
        parse_baseline_xml(xml)
    }

    /// Return true if the given (file, issue_kind, snippet) triple is present
    /// in the baseline.  Each matching entry is consumed once so duplicate
    /// suppressions work correctly.
    pub fn consume(&mut self, file: &str, issue_kind: &str, snippet: &str) -> bool {
        if let Some(by_kind) = self.entries.get_mut(file) {
            if let Some(snippets) = by_kind.get_mut(issue_kind) {
                if let Some(pos) = snippets.iter().position(|s| s == snippet) {
                    snippets.remove(pos);
                    return true;
                }
            }
        }
        false
    }

    /// Return true if the (file, issue_kind) pair exists in the baseline
    /// regardless of snippet.  Used as a fallback when no snippet is available.
    #[allow(dead_code)]
    pub fn contains_kind(&self, file: &str, issue_kind: &str) -> bool {
        self.entries
            .get(file)
            .and_then(|m| m.get(issue_kind))
            .is_some_and(|v| !v.is_empty())
    }

    /// Serialize this baseline to a Psalm-compatible XML file.
    pub fn write(&self, path: &std::path::Path) -> Result<(), ConfigError> {
        let mut out = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<files>\n");

        let mut files: Vec<&String> = self.entries.keys().collect();
        files.sort_unstable();

        for file in files {
            let by_kind = &self.entries[file];
            let mut kinds: Vec<&String> = by_kind.keys().collect();
            kinds.sort_unstable();

            out.push_str(&format!("  <file src=\"{}\">\n", xml_escape_attr(file)));
            for kind in kinds {
                let snippets = &by_kind[kind];
                out.push_str(&format!("    <{kind}>\n"));
                for snippet in snippets {
                    out.push_str(&format!("      <code><![CDATA[{snippet}]]></code>\n"));
                }
                out.push_str(&format!("    </{kind}>\n"));
            }
            out.push_str("  </file>\n");
        }

        out.push_str("</files>\n");

        std::fs::write(path, out).map_err(|e| ConfigError::Io(e.to_string()))
    }
}

fn xml_escape_attr(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('"', "&quot;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

fn parse_baseline_xml(xml: &str) -> Result<Baseline, ConfigError> {
    use quick_xml::events::Event;
    use quick_xml::Reader;

    let mut reader = Reader::from_str(xml);
    reader.config_mut().trim_text(true);

    let mut baseline = Baseline::default();
    // Stack: ["files", "file", "IssuKind"]
    let mut path: Vec<String> = Vec::new();
    let mut current_file: Option<String> = None;
    let mut current_kind: Option<String> = None;
    let mut text_buf = String::new();

    loop {
        match reader.read_event() {
            Ok(Event::Start(e)) => {
                let name = bytes_to_string(e.name().as_ref());
                match name.as_str() {
                    "file" => {
                        for attr in e.attributes().flatten() {
                            if bytes_to_string(attr.key.as_ref()) == "src" {
                                current_file = Some(bytes_to_string(&attr.value));
                            }
                        }
                        current_kind = None;
                    }
                    "files" => {}
                    _ if path.last().is_some_and(|s: &String| s == "file") => {
                        // Direct child of <file> is an issue-kind element
                        current_kind = Some(name.clone());
                    }
                    _ => {}
                }
                text_buf.clear();
                path.push(name);
            }
            Ok(Event::Empty(e)) => {
                // Self-closing <file> or <code/> — handled below
                let name = bytes_to_string(e.name().as_ref());
                if name == "file" {
                    for attr in e.attributes().flatten() {
                        if bytes_to_string(attr.key.as_ref()) == "src" {
                            current_file = Some(bytes_to_string(&attr.value));
                        }
                    }
                }
            }
            Ok(Event::CData(cd)) => {
                text_buf = String::from_utf8_lossy(cd.as_ref()).trim().to_string();
            }
            Ok(Event::Text(t)) => {
                let s = t
                    .xml_content()
                    .map_err(|e| ConfigError::Parse(e.to_string()))?;
                let trimmed = s.trim().to_string();
                if !trimmed.is_empty() {
                    text_buf = trimmed;
                }
            }
            Ok(Event::End(e)) => {
                let name = bytes_to_string(e.name().as_ref());
                match name.as_str() {
                    "code" => {
                        // Record this snippet
                        if let (Some(file), Some(kind)) = (&current_file, &current_kind) {
                            let snippet = std::mem::take(&mut text_buf);
                            baseline
                                .entries
                                .entry(file.clone())
                                .or_default()
                                .entry(kind.clone())
                                .or_default()
                                .push(snippet);
                        }
                    }
                    "file" => {
                        current_file = None;
                        current_kind = None;
                    }
                    _ if Some(&name) == current_kind.as_ref() => {
                        current_kind = None;
                    }
                    _ => {}
                }
                path.pop();
                text_buf.clear();
            }
            Ok(Event::Eof) => break,
            Err(e) => return Err(ConfigError::Parse(e.to_string())),
            _ => {}
        }
    }

    Ok(baseline)
}

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

    #[test]
    fn parses_php_version_child_element() {
        let cfg = Config::parse(r#"<mir><phpVersion>8.1</phpVersion></mir>"#).unwrap();
        assert_eq!(cfg.php_version.as_deref(), Some("8.1"));
    }

    #[test]
    fn parses_php_version_root_attribute() {
        let cfg = Config::parse(r#"<mir phpVersion="8.2"></mir>"#).unwrap();
        assert_eq!(cfg.php_version.as_deref(), Some("8.2"));
    }

    #[test]
    fn root_attribute_does_not_override_cli_override() {
        // Simulate: config file has attribute, CLI flag would overwrite in main.rs.
        // The XML parser itself should accept the attribute form.
        let cfg = Config::parse(r#"<psalm phpVersion="7.4"></psalm>"#).unwrap();
        assert_eq!(cfg.php_version.as_deref(), Some("7.4"));
    }

    #[test]
    fn parses_stubs_file_entries() {
        let cfg = Config::parse(
            r#"<mir>
                <stubs>
                    <file name="stubs/helpers.php"/>
                    <file name="stubs/ide.php"/>
                </stubs>
            </mir>"#,
        )
        .unwrap();
        assert_eq!(cfg.stub_files, vec!["stubs/helpers.php", "stubs/ide.php"]);
        assert!(cfg.stub_dirs.is_empty());
    }

    #[test]
    fn parses_stubs_directory_entries() {
        let cfg = Config::parse(
            r#"<mir>
                <stubs>
                    <directory name="stubs/doctrine"/>
                </stubs>
            </mir>"#,
        )
        .unwrap();
        assert_eq!(cfg.stub_dirs, vec!["stubs/doctrine"]);
        assert!(cfg.stub_files.is_empty());
    }

    #[test]
    fn stubs_directory_does_not_pollute_project_dirs() {
        let cfg = Config::parse(
            r#"<mir>
                <projectFiles>
                    <directory name="src"/>
                </projectFiles>
                <stubs>
                    <directory name="stubs/ext"/>
                </stubs>
            </mir>"#,
        )
        .unwrap();
        assert_eq!(cfg.project_dirs, vec!["src"]);
        assert_eq!(cfg.stub_dirs, vec!["stubs/ext"]);
    }
}