mago 1.20.1

A comprehensive suite of PHP tooling inspired by Rust’s approach, providing parsing, linting, formatting, and more through a unified CLI and library interface.
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
//! Configuration management for Mago CLI.
//!
//! This module provides a comprehensive configuration system that supports multiple
//! sources and precedence levels. Configuration can be loaded from TOML files,
//! environment variables, and command-line arguments, with each source overriding
//! the previous one.
//!
//! # Configuration Sources
//!
//! Configuration is loaded and merged from the following sources, in order of precedence
//! (highest to lowest):
//!
//! 1. **Environment Variables**: Prefixed with `MAGO_` (e.g., `MAGO_THREADS=4`)
//! 2. **Workspace Config**: `mago.toml` in the workspace directory
//! 3. **Global Config**: `mago.toml` in `$XDG_CONFIG_HOME` or `$HOME`
//! 4. **Explicit File**: Specified via `--config` flag (bypasses workspace/global)
//! 5. **Defaults**: Built-in defaults for all settings
//!
//! # Configuration Structure
//!
//! The configuration is organized into several sub-configurations:
//!
//! - **Source**: Defines workspace paths and file discovery patterns
//! - **Linter**: Controls linting behavior and rule sets
//! - **Formatter**: Defines code formatting style preferences
//! - **Analyzer**: Controls static type analysis settings
//! - **Guard**: Configures the guard service for continuous monitoring
//!
//! # Normalization and Validation
//!
//! After loading, configurations are normalized to ensure valid values:
//!
//! - Thread count defaults to logical CPU count if zero
//! - Stack size is clamped between minimum and maximum bounds
//! - PHP version compatibility is validated
//! - Source paths are resolved and validated
//!
//! # Environment Variables
//!
//! All configuration options can be set via environment variables using the
//! `MAGO_` prefix and kebab-case conversion. For nested options, use underscores:
//!
//! - `MAGO_THREADS` → `threads`
//! - `MAGO_PHP_VERSION` → `php_version`
//! - `MAGO_SOURCE_WORKSPACE` → `source.workspace`

use std::env::home_dir;
use std::fmt::Debug;
use std::path::Path;
use std::path::PathBuf;

use config::Case;
use config::Config;
use config::Environment;
use config::File;
use config::FileFormat;
use config::FileStoredFormat;
use config::Value;
use config::ValueKind;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

use mago_php_version::PHPVersion;

use crate::config::analyzer::AnalyzerConfiguration;
use crate::config::formatter::FormatterConfiguration;
use crate::config::guard::GuardConfiguration;
use crate::config::linter::LinterConfiguration;
use crate::config::parser::ParserConfiguration;
use crate::config::source::SourceConfiguration;
use crate::consts::*;
use crate::error::Error;

pub mod analyzer;
pub mod formatter;
pub mod guard;
pub mod linter;
pub mod parser;
pub mod source;

/// Default value for threads configuration field.
fn default_threads() -> usize {
    *LOGICAL_CPUS
}

/// Default value for stack_size configuration field.
fn default_stack_size() -> usize {
    DEFAULT_STACK_SIZE
}

/// Default value for php_version configuration field.
fn default_php_version() -> PHPVersion {
    DEFAULT_PHP_VERSION
}

/// Default value for source configuration field.
fn default_source_configuration() -> SourceConfiguration {
    SourceConfiguration::from_workspace(CURRENT_DIR.clone())
}

/// The main configuration structure for Mago CLI.
///
/// This struct aggregates all configuration settings for Mago, including global options
/// like threading and PHP version, as well as specialized configurations for each service
/// (linter, analyzer, formatter, guard).
///
/// Configuration values are loaded from multiple sources with the following precedence
/// (from highest to lowest):
///
/// 1. Environment variables (`MAGO_*`)
/// 2. Workspace `mago.toml` file
/// 3. Global `mago.toml` file (`$XDG_CONFIG_HOME` or `$HOME`)
/// 4. Built-in defaults
///
/// The struct uses serde for deserialization from TOML files and environment variables,
/// with strict validation via `deny_unknown_fields` to catch configuration errors early.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct Configuration {
    /// The mago version this project is pinned to.
    ///
    /// Accepts three pin levels:
    ///
    /// - `"1"`: major pin; any `1.x.y` satisfies it. `mago init` emits this
    ///   by default. Minor/patch drift within major 1 is a warning; a bump to
    ///   `2.x` is a hard error.
    /// - `"1.19"`: minor pin; any `1.19.y` satisfies it.
    /// - `"1.19.3"`: exact pin; any drift is a warning, and this is the only
    ///   form that `mago self-update --to-project-version` can target without
    ///   ambiguity.
    ///
    /// Empty / missing is currently a no-op; a future mago release is likely
    /// to start warning when the pin is absent, to prepare projects for 2.0.
    ///
    /// # Compatibility invariant (**do not break**)
    ///
    /// This field's location (top-level) and type (string) are a load-bearing
    /// contract for cross-major config compatibility: a future mago 2.x must
    /// be able to read a mago 1.x `mago.toml` via a permissive top-level TOML
    /// pass, find this field, and refuse to run with
    /// "this config is pinned to mago 1" *before* it ever hits its own strict
    /// schema. That means:
    ///
    /// - never move this field into a `[metadata]` section,
    /// - never rename it,
    /// - never change it from a string,
    /// - never change the pin grammar from `major[.minor[.patch]]`.
    #[serde(default)]
    pub version: Option<String>,

    /// Number of worker threads for parallel processing.
    ///
    /// Controls the thread pool size used by Rayon for parallel operations.
    /// If set to 0, defaults to the number of logical CPUs available.
    /// Can be overridden via `MAGO_THREADS` environment variable or `--threads` CLI flag.
    #[serde(default = "default_threads")]
    pub threads: usize,

    /// Stack size for each worker thread in bytes.
    ///
    /// Determines the maximum stack size allocated for each thread in the thread pool.
    /// Must be between `MINIMUM_STACK_SIZE` and `MAXIMUM_STACK_SIZE`.
    /// If set to 0, uses `MAXIMUM_STACK_SIZE`. Values outside the valid range are
    /// automatically clamped during normalization.
    #[serde(default = "default_stack_size")]
    pub stack_size: usize,

    /// Target PHP version for parsing and analysis.
    ///
    /// Specifies which PHP version to assume when parsing code and performing analysis.
    /// This affects syntax parsing rules, available built-in functions, and type checking.
    /// Can be overridden via `MAGO_PHP_VERSION` environment variable or `--php-version` CLI flag.
    #[serde(default = "default_php_version")]
    pub php_version: PHPVersion,

    /// Whether to allow PHP versions not officially supported by Mago.
    ///
    /// When enabled, Mago will not fail if the specified PHP version is outside the
    /// officially supported range. Use with caution as behavior may be unpredictable.
    /// Can be enabled via `MAGO_ALLOW_UNSUPPORTED_PHP_VERSION` environment variable
    /// or `--allow-unsupported-php-version` CLI flag.
    #[serde(default)]
    pub allow_unsupported_php_version: bool,

    /// Whether to silence the project version drift warning.
    ///
    /// Affects only the minor / patch drift warning emitted when the installed
    /// mago binary does not match the `version` pinned in `mago.toml`. A major
    /// drift is always fatal and is *not* affected by this flag; the whole
    /// point of a major pin is to stop runs across incompatible config schemas.
    ///
    /// Can be enabled via `MAGO_NO_VERSION_CHECK` environment variable or
    /// `--no-version-check` CLI flag.
    #[serde(default)]
    pub no_version_check: bool,

    /// Source discovery and workspace configuration.
    ///
    /// Defines the workspace root, source paths to scan, and exclusion patterns.
    /// This configuration determines which PHP files are loaded into the database
    /// for analysis, linting, or formatting.
    #[serde(default = "default_source_configuration")]
    pub source: SourceConfiguration,

    /// Linter service configuration.
    ///
    /// Controls linting behavior, including enabled/disabled rules, rule-specific
    /// settings, and reporting preferences. Defaults to an empty configuration
    /// if not specified in the config file.
    #[serde(default)]
    pub linter: LinterConfiguration,

    /// Parser configuration.
    ///
    /// Controls how PHP code is parsed, including lexer-level settings
    /// like short open tag support. Defaults to standard PHP parsing behavior
    /// if not specified in the config file.
    #[serde(default)]
    pub parser: ParserConfiguration,

    /// Formatter service configuration.
    ///
    /// Defines code formatting style preferences such as indentation, line width,
    /// brace placement, and spacing rules. Defaults to standard formatting settings
    /// if not specified in the config file.
    #[serde(default)]
    pub formatter: FormatterConfiguration,

    /// Type analyzer service configuration.
    ///
    /// Controls static type analysis behavior, including strictness levels,
    /// inference settings, and type-related rules. Defaults to an empty configuration
    /// if not specified in the config file.
    #[serde(default)]
    pub analyzer: AnalyzerConfiguration,

    /// Guard service configuration for continuous monitoring.
    ///
    /// Defines settings for the guard/watch mode, including file watching behavior,
    /// debouncing, and incremental analysis. Defaults to an empty configuration
    /// if not specified in the config file.
    #[serde(default)]
    pub guard: GuardConfiguration,

    /// Log filter for tracing output.
    ///
    /// This field exists solely to prevent errors when `MAGO_LOG` environment
    /// variable is set. Due to `deny_unknown_fields`, without this field serde
    /// would reject the configuration when `MAGO_LOG` is present in the environment.
    ///
    /// This is not a user-facing configuration option and is never serialized.
    #[serde(default, skip_serializing)]
    #[schemars(skip)]
    #[allow(dead_code)]
    log: Value,

    /// Editor URL template for OSC 8 terminal hyperlinks on file paths in diagnostics.
    ///
    /// When set, file paths in diagnostic output become clickable links in terminals
    /// that support OSC 8 hyperlinks (e.g., iTerm2, Wezterm, Kitty, Windows Terminal).
    ///
    /// Supported placeholders:
    /// - `%file%` — absolute file path
    /// - `%line%` — line number
    /// - `%column%` — column number
    ///
    /// Can be set via `MAGO_EDITOR_URL` environment variable or `editor-url` in `mago.toml`.
    #[serde(default)]
    pub editor_url: Option<String>,

    /// The path to the configuration file that was loaded, if any.
    ///
    /// This is set during configuration loading and is not user-configurable.
    /// It is used by watch mode to monitor the configuration file for changes.
    #[serde(default, skip_serializing)]
    #[schemars(skip)]
    pub config_file: Option<PathBuf>,

    /// Whether the configuration file was explicitly provided via `--config` CLI flag.
    ///
    /// When `true`, the config file path is pinned and won't be re-discovered on reload.
    /// When `false`, the config file is auto-discovered and may change between reloads.
    #[serde(default, skip_serializing)]
    #[schemars(skip)]
    pub config_file_is_explicit: bool,
}

impl Configuration {
    /// Loads and merges configuration from multiple sources.
    ///
    /// This method orchestrates the complete configuration loading process, combining
    /// settings from files, environment variables, and CLI arguments with proper
    /// precedence handling. The configuration is then normalized and validated.
    ///
    /// # Loading Process
    ///
    /// The method follows this workflow:
    ///
    /// 1. **Create Base Configuration**: Initialize with workspace-specific defaults
    /// 2. **Load Global Configs**: Merge `$XDG_CONFIG_HOME/mago.toml` and `$HOME/mago.toml`
    ///    (skipped if explicit config file is provided)
    /// 3. **Load Workspace Config**: Merge `<workspace>/mago.toml`
    ///    (skipped if explicit config file is provided)
    /// 4. **Load Explicit Config**: If `file` is provided, load it as the sole config file
    /// 5. **Apply Environment Variables**: Override with `MAGO_*` environment variables
    /// 6. **Apply CLI Overrides**: Apply specific CLI argument overrides
    /// 7. **Normalize and Validate**: Ensure all values are within valid ranges
    ///
    /// # Precedence Order
    ///
    /// Settings are merged with the following precedence (highest to lowest):
    ///
    /// 1. CLI arguments (`php_version`, `threads`, `allow_unsupported_php_version`)
    /// 2. Environment variables (`MAGO_*`)
    /// 3. Workspace config file (`<workspace>/mago.toml`)
    /// 4. Global config files (`$XDG_CONFIG_HOME/mago.toml`, `$HOME/mago.toml`)
    /// 5. Explicit config file (if provided via `file` parameter, bypasses 3 and 4)
    /// 6. Default values
    ///
    /// # Arguments
    ///
    /// * `workspace` - Optional workspace directory path. If not provided, uses current directory.
    ///   This directory is scanned for source files and may contain a `mago.toml` file.
    /// * `file` - Optional explicit path to a configuration file. When provided, global and
    ///   workspace config files are not loaded. The specified file must exist.
    /// * `php_version` - Optional PHP version override. Takes precedence over all config sources.
    /// * `threads` - Optional thread count override. Takes precedence over all config sources.
    /// * `allow_unsupported_php_version` - If `true`, enables support for PHP versions outside
    ///   the officially supported range. Only overrides the config if `true`.
    /// * `no_version_check` - If `true`, silences the project version drift warning emitted
    ///   on minor/patch mismatch against the `version` pin in `mago.toml`. Only overrides
    ///   the config if `true`. Does not affect the fatal behaviour on major-version drift.
    ///
    /// # Returns
    ///
    /// - `Ok(Configuration)` - Successfully loaded and validated configuration
    /// - `Err(Error::Configuration)` - Failed to parse TOML or deserialize configuration
    /// - `Err(Error::IOError)` - Failed to read configuration file (when `file` is provided)
    ///
    /// # Errors
    ///
    /// This method may fail if:
    /// - The specified config file does not exist or cannot be read (when `file` is provided)
    /// - Configuration files contain invalid TOML syntax
    /// - Configuration values fail validation (e.g., unknown field names due to `deny_unknown_fields`)
    /// - Required configuration fields are missing
    /// - Normalization fails (e.g., invalid source paths)
    pub fn load(
        workspace: Option<PathBuf>,
        file: Option<&Path>,
        php_version: Option<PHPVersion>,
        threads: Option<usize>,
        allow_unsupported_php_version: bool,
        no_version_check: bool,
    ) -> Result<Configuration, Error> {
        let workspace_dir = workspace.clone().unwrap_or_else(|| CURRENT_DIR.to_path_buf());

        let mut builder = Config::builder();

        let resolved_config_file;
        let config_file_is_explicit;
        if let Some(file) = file {
            tracing::debug!("Sourcing configuration from {}.", file.display());

            resolved_config_file = Some(file.to_path_buf());
            config_file_is_explicit = true;
            builder = builder.add_source(File::from(file).required(true));
        } else {
            let formats = [FileFormat::Toml, FileFormat::Yaml, FileFormat::Json];
            // Check workspace first, then XDG, then ~/.config, then ~ (workspace has highest precedence)
            let fallback_roots = [
                std::env::var_os("XDG_CONFIG_HOME").map(PathBuf::from),
                home_dir().map(|h| h.join(".config")),
                home_dir(),
            ];

            if let Some((config_file, format)) = Self::find_config_files(&workspace_dir, &fallback_roots, &formats) {
                tracing::debug!("Sourcing configuration from {}.", config_file.display());
                resolved_config_file = Some(config_file.clone());
                builder = builder.add_source(File::from(config_file).format(format).required(false));
            } else {
                tracing::debug!("No configuration file found, using defaults and environment variables.");
                resolved_config_file = None;
            }

            config_file_is_explicit = false;
        }

        let mut configuration: Configuration = builder
            .add_source(Environment::with_prefix(ENVIRONMENT_PREFIX).convert_case(Case::Kebab))
            .build()?
            .try_deserialize::<Configuration>()?;

        configuration.config_file = resolved_config_file;
        configuration.config_file_is_explicit = config_file_is_explicit;

        if allow_unsupported_php_version && !configuration.allow_unsupported_php_version {
            tracing::warn!("Allowing unsupported PHP versions.");

            configuration.allow_unsupported_php_version = true;
        }

        if no_version_check && !configuration.no_version_check {
            tracing::info!("Silencing project version drift warning.");

            configuration.no_version_check = true;
        }

        if let Some(php_version) = php_version {
            tracing::info!("Overriding PHP version with {}.", php_version);

            configuration.php_version = php_version;
        }

        if let Some(threads) = threads {
            tracing::info!("Overriding thread count with {}.", threads);

            configuration.threads = threads;
        }

        if let Some(workspace) = workspace {
            tracing::info!("Overriding workspace directory with {}.", workspace.display());

            configuration.source.workspace = workspace;
        }

        if configuration.editor_url.is_none() {
            configuration.editor_url = detect_editor_url();
        }

        configuration.normalize()?;

        Ok(configuration)
    }

    /// Searches for configuration files in a project directory, falling back to global config locations.
    ///
    /// This function attempts to load at most one configuration file per supported config type.
    /// It first searches the given `root_dir` (typically the workspace/project directory).
    /// If any configuration file is found there, those results are returned immediately and no
    /// fallback locations are checked.
    ///
    /// If no config files are found in `root_dir`, the function searches each directory in
    /// `fallback_roots` in order. The first matching format for each config file name is returned.
    ///
    /// # Arguments
    ///
    /// * `root_dir` - The primary directory to search (project root)
    /// * `fallback_roots` - A list of additional directories to search if `root_dir` contains no matches
    /// * `file_formats` - Supported configuration formats (`toml`, `yaml`, etc.), each with possible extensions
    ///
    /// # Returns
    ///
    /// A vector of `(PathBuf, FileFormat)` pairs, where:
    /// * The path is the resolved config file
    /// * The format indicates which file format identified it
    ///
    /// # Behavior Summary
    ///
    /// 1. Try to resolve `<root_dir>/<name>.<ext>` for each supported format
    /// 2. Stop and return immediately if any matches are found
    /// 3. Otherwise, search each directory in `fallback_roots` in order
    /// 4. The first match (by name and directory order) wins
    ///
    /// This prevents global configuration files from overriding project-local configuration.
    fn find_config_files(
        root_dir: &Path,
        fallback_roots: &[Option<PathBuf>],
        file_formats: &[FileFormat],
    ) -> Option<(PathBuf, FileFormat)> {
        let config_files = [CONFIGURATION_FILE_NAME, CONFIGURATION_DIST_FILE_NAME];

        for name in config_files.iter() {
            let base = root_dir.join(name);

            for format in file_formats.iter() {
                if let Some(ext) = format.file_extensions().iter().find(|ext| base.with_added_extension(ext).exists()) {
                    return Some((base.with_added_extension(ext), *format));
                }
            }
        }

        for root in fallback_roots.iter().flatten() {
            let base = root.join(CONFIGURATION_FILE_NAME);

            for format in file_formats.iter() {
                if let Some(ext) = format.file_extensions().iter().find(|ext| base.with_added_extension(ext).exists()) {
                    return Some((base.with_added_extension(ext), *format));
                }
            }
        }

        None
    }

    /// Creates a default configuration anchored to a specific workspace directory.
    ///
    /// This constructor initializes a configuration with sensible defaults suitable
    /// for immediate use. All service-specific configurations (linter, formatter,
    /// analyzer, guard) use their default settings.
    ///
    /// # Default Values
    ///
    /// - **threads**: Number of logical CPUs available on the system
    /// - **stack_size**: `DEFAULT_STACK_SIZE` (typically 8MB)
    /// - **php_version**: `DEFAULT_PHP_VERSION` (latest stable PHP version)
    /// - **allow_unsupported_php_version**: `false`
    /// - **source**: Workspace-specific source configuration
    /// - **linter**: Default linter configuration
    /// - **formatter**: Default formatter configuration
    /// - **analyzer**: Default analyzer configuration
    /// - **guard**: Default guard configuration
    ///
    /// This method is primarily used as the starting point for configuration loading,
    /// with values subsequently overridden by config files and environment variables.
    ///
    /// # Arguments
    ///
    /// * `workspace` - The root directory of the workspace to analyze. This directory
    ///   serves as the base path for relative source paths and is where the database
    ///   will look for PHP files.
    ///
    /// # Returns
    ///
    /// A new `Configuration` instance with default values and the specified workspace.
    pub fn from_workspace(workspace: PathBuf) -> Self {
        Self {
            version: None,
            threads: *LOGICAL_CPUS,
            stack_size: DEFAULT_STACK_SIZE,
            php_version: DEFAULT_PHP_VERSION,
            allow_unsupported_php_version: false,
            no_version_check: false,
            source: SourceConfiguration::from_workspace(workspace),
            linter: LinterConfiguration::default(),
            parser: ParserConfiguration::default(),
            formatter: FormatterConfiguration::default(),
            analyzer: AnalyzerConfiguration::default(),
            guard: GuardConfiguration::default(),
            log: Value::new(None, ValueKind::Nil),
            editor_url: None,
            config_file: None,
            config_file_is_explicit: false,
        }
    }
}

impl Configuration {
    /// Returns a filtered version of the configuration suitable for display.
    ///
    /// This method excludes linter rules that don't match the configured integrations,
    /// so that only applicable rules are shown in the output.
    #[must_use]
    pub fn to_filtered_value(&self) -> serde_json::Value {
        serde_json::json!({
            "version": self.version,
            "threads": self.threads,
            "stack-size": self.stack_size,
            "php-version": self.php_version,
            "allow-unsupported-php-version": self.allow_unsupported_php_version,
            "no-version-check": self.no_version_check,
            "source": self.source,
            "linter": self.linter.to_filtered_value(self.php_version),
            "parser": self.parser,
            "formatter": self.formatter.to_value(),
            "analyzer": self.analyzer,
            "guard": self.guard,
        })
    }

    /// Normalizes and validates configuration values.
    ///
    /// This method ensures that all configuration values are within acceptable ranges
    /// and makes sensible adjustments where needed. It is called automatically by
    /// [`load`](Self::load) after merging all configuration sources.
    ///
    /// # Normalization Rules
    ///
    /// ## Thread Count
    ///
    /// - If set to `0`: Defaults to the number of logical CPUs
    /// - Otherwise: Uses the configured value as-is
    ///
    /// ## Stack Size
    ///
    /// - If set to `0`: Uses `MAXIMUM_STACK_SIZE`
    /// - If greater than `MAXIMUM_STACK_SIZE`: Clamped to `MAXIMUM_STACK_SIZE`
    /// - If less than `MINIMUM_STACK_SIZE`: Clamped to `MINIMUM_STACK_SIZE`
    /// - Otherwise: Uses the configured value as-is
    ///
    /// ## Source Configuration
    ///
    /// Delegates to [`SourceConfiguration::normalize`] to validate workspace paths
    /// and resolve relative paths.
    ///
    /// # Returns
    ///
    /// - `Ok(())` if normalization succeeded
    /// - `Err(Error)` if source normalization failed (e.g., invalid workspace path)
    ///
    /// # Side Effects
    ///
    /// This method logs warnings and informational messages when values are adjusted,
    /// helping users understand how their configuration was interpreted.
    fn normalize(&mut self) -> Result<(), Error> {
        match self.threads {
            0 => {
                tracing::info!("Thread configuration is zero, using the number of logical CPUs: {}.", *LOGICAL_CPUS);

                self.threads = *LOGICAL_CPUS;
            }
            _ => {
                tracing::debug!("Configuration specifies {} threads.", self.threads);
            }
        }

        match self.stack_size {
            0 => {
                tracing::info!(
                    "Stack size configuration is zero, using the maximum size of {} bytes.",
                    MAXIMUM_STACK_SIZE
                );

                self.stack_size = MAXIMUM_STACK_SIZE;
            }
            s if s > MAXIMUM_STACK_SIZE => {
                tracing::warn!(
                    "Stack size configuration is too large, reducing to maximum size of {} bytes.",
                    MAXIMUM_STACK_SIZE
                );

                self.stack_size = MAXIMUM_STACK_SIZE;
            }
            s if s < MINIMUM_STACK_SIZE => {
                tracing::warn!(
                    "Stack size configuration is too small, increasing to minimum size of {} bytes.",
                    MINIMUM_STACK_SIZE
                );

                self.stack_size = MINIMUM_STACK_SIZE;
            }
            _ => {
                tracing::debug!("Configuration specifies a stack size of {} bytes.", self.stack_size);
            }
        }

        self.source.normalize()?;

        if let Some(b) = self.analyzer.baseline.take() {
            self.analyzer.baseline = Some(if b.is_relative() { self.source.workspace.join(&b) } else { b });
        }

        if let Some(b) = self.linter.baseline.take() {
            self.linter.baseline = Some(if b.is_relative() { self.source.workspace.join(&b) } else { b });
        }

        if let Some(b) = self.guard.baseline.take() {
            self.guard.baseline = Some(if b.is_relative() { self.source.workspace.join(&b) } else { b });
        }

        Ok(())
    }
}

#[cfg(all(test, not(target_os = "windows")))]
mod tests {
    use core::str;
    use std::fs;

    use pretty_assertions::assert_eq;
    use tempfile::env::temp_dir;

    use super::*;

    #[test]
    fn test_take_defaults() {
        let workspace_path = temp_dir().join("workspace-0");
        std::fs::create_dir_all(&workspace_path).unwrap();

        let config = temp_env::with_vars(
            [
                ("HOME", temp_dir().to_str()),
                ("MAGO_THREADS", None),
                ("MAGO_PHP_VERSION", None),
                ("MAGO_ALLOW_UNSUPPORTED_PHP_VERSION", None),
            ],
            || Configuration::load(Some(workspace_path), None, None, None, false, false).unwrap(),
        );

        assert_eq!(config.threads, *LOGICAL_CPUS)
    }

    #[test]
    fn test_toml_has_precedence_when_multiple_configs_present() {
        let workspace_path = temp_dir().join("workspace-with-multiple-configs");
        std::fs::create_dir_all(&workspace_path).unwrap();

        create_tmp_file("threads = 3", &workspace_path, "toml");
        create_tmp_file("threads: 2\nphp-version: \"7.4.0\"", &workspace_path, "yaml");
        create_tmp_file("{\"threads\": 1,\"php-version\":\"8.1.0\"}", &workspace_path, "json");

        let config = Configuration::load(Some(workspace_path), None, None, None, false, false).unwrap();

        assert_eq!(config.threads, 3);
        assert_eq!(config.php_version.to_string(), DEFAULT_PHP_VERSION.to_string())
    }

    #[test]
    fn test_env_config_override_all_others() {
        let workspace_path = temp_dir().join("workspace-1");
        let config_path = temp_dir().join("config-1");

        std::fs::create_dir_all(&workspace_path).unwrap();
        std::fs::create_dir_all(&config_path).unwrap();

        let config_file_path = create_tmp_file("threads = 1", &config_path, "toml");
        create_tmp_file("threads = 2", &workspace_path, "toml");

        let config = temp_env::with_vars(
            [
                ("HOME", None),
                ("MAGO_THREADS", Some("3")),
                ("MAGO_PHP_VERSION", None),
                ("MAGO_ALLOW_UNSUPPORTED_PHP_VERSION", None),
            ],
            || Configuration::load(Some(workspace_path), Some(&config_file_path), None, None, false, false).unwrap(),
        );

        assert_eq!(config.threads, 3);
    }

    #[test]
    fn test_config_cancel_workspace() {
        let workspace_path = temp_dir().join("workspace-2");
        let config_path = temp_dir().join("config-2");

        std::fs::create_dir_all(&workspace_path).unwrap();
        std::fs::create_dir_all(&config_path).unwrap();

        create_tmp_file("threads = 2\nphp-version = \"7.4.0\"", &workspace_path, "toml");

        let config_file_path = create_tmp_file("threads = 1", &config_path, "toml");
        let config = temp_env::with_vars(
            [
                ("HOME", None::<&str>),
                ("MAGO_THREADS", None),
                ("MAGO_PHP_VERSION", None),
                ("MAGO_ALLOW_UNSUPPORTED_PHP_VERSION", None),
            ],
            || Configuration::load(Some(workspace_path), Some(&config_file_path), None, None, false, false).unwrap(),
        );

        assert_eq!(config.threads, 1);
        assert_eq!(config.php_version.to_string(), DEFAULT_PHP_VERSION.to_string());
    }

    #[test]
    fn test_workspace_has_precedence_over_global() {
        let home_path = temp_dir().join("home-3");
        let xdg_config_home_path = temp_dir().join("xdg-config-home-3");
        let workspace_path = temp_dir().join("workspace-3");

        // Clean up any existing directories from previous test runs
        let _ = std::fs::remove_dir_all(&home_path);
        let _ = std::fs::remove_dir_all(&xdg_config_home_path);
        let _ = std::fs::remove_dir_all(&workspace_path);

        std::fs::create_dir_all(&home_path).unwrap();
        std::fs::create_dir_all(&xdg_config_home_path).unwrap();
        std::fs::create_dir_all(&workspace_path).unwrap();

        create_tmp_file("threads: 2\nphp-version: \"8.1.0\"", &workspace_path.to_owned(), "yaml");
        create_tmp_file("threads = 3\nphp-version = \"7.4.0\"", &home_path, "toml");
        create_tmp_file("source.excludes = [\"yes\"]", &xdg_config_home_path, "toml");

        let config = temp_env::with_vars(
            [
                ("HOME", Some(home_path)),
                ("XDG_CONFIG_HOME", Some(xdg_config_home_path)),
                ("MAGO_THREADS", None),
                ("MAGO_PHP_VERSION", None),
                ("MAGO_ALLOW_UNSUPPORTED_PHP_VERSION", None),
            ],
            || Configuration::load(Some(workspace_path.clone()), None, None, None, false, false).unwrap(),
        );

        assert_eq!(config.threads, 2);
        assert_eq!(config.php_version.to_string(), "8.1.0".to_string());
        assert_eq!(config.source.excludes, Vec::<String>::new());
    }

    fn create_tmp_file(config_content: &str, folder: &PathBuf, extension: &str) -> PathBuf {
        fs::create_dir_all(folder).unwrap();
        let config_path = folder.join(CONFIGURATION_FILE_NAME).with_extension(extension);
        fs::write(&config_path, config_content).unwrap();
        config_path
    }
}

/// Auto-detect the editor URL template from environment hints.
///
/// Checks terminal environment variables to determine which editor is running,
/// and returns the appropriate URL template for clickable file paths.
fn detect_editor_url() -> Option<String> {
    if let Ok(bundle_id) = std::env::var("__CFBundleIdentifier") {
        let url = match bundle_id.as_str() {
            "com.jetbrains.PhpStorm" | "com.jetbrains.PhpStorm-EAP" => {
                "phpstorm://open?file=%file%&line=%line%&column=%column%"
            }
            "com.jetbrains.intellij" | "com.jetbrains.intellij.ce" => {
                "idea://open?file=%file%&line=%line%&column=%column%"
            }
            "com.jetbrains.WebStorm" | "com.jetbrains.WebStorm-EAP" => {
                "webstorm://open?file=%file%&line=%line%&column=%column%"
            }
            "dev.zed.Zed" | "dev.zed.Zed-Preview" => "zed://file/%file%:%line%:%column%",
            "com.microsoft.VSCode" => "vscode://file/%file%:%line%:%column%",
            "com.microsoft.VSCodeInsiders" => "vscode-insiders://file/%file%:%line%:%column%",
            "com.sublimetext.4" | "com.sublimetext.3" => "subl://open?url=file://%file%&line=%line%&column=%column%",
            _ => return None,
        };

        tracing::debug!("Auto-detected editor URL from __CFBundleIdentifier={bundle_id}");

        return Some(url.to_string());
    }

    if let Ok(term_program) = std::env::var("TERM_PROGRAM") {
        let url = match term_program.as_str() {
            "vscode" => "vscode://file/%file%:%line%:%column%",
            "zed" => "zed://file/%file%:%line%:%column%",
            _ => return None,
        };

        tracing::debug!("Auto-detected editor URL from TERM_PROGRAM={term_program}");

        return Some(url.to_string());
    }

    None
}