aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/command/base_command.rs
blob: 6b673eda883aa6fda3afed711d36285ba5502130 (plain)
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
//! ref: composer/src/Composer/Command/BaseCommand.php
//! ref: composer/vendor/symfony/console/Command/Command.php

use anyhow::Result;
use indexmap::IndexMap;
use shirabe_external_packages::symfony::component::console::Terminal;
use shirabe_external_packages::symfony::component::console::helper::Table;
use shirabe_external_packages::symfony::component::console::helper::TableSeparator;
use shirabe_external_packages::symfony::component::console::input::InputDefinition;
use shirabe_external_packages::symfony::component::console::input::InputInterface;
use shirabe_external_packages::symfony::component::console::output::OutputInterface;
use shirabe_php_shim::{
    InvalidArgumentException, LogicException, PhpMixed, RuntimeException, UnexpectedValueException,
    count, explode, in_array, is_string, max,
};
use std::cell::RefCell;
use std::rc::Rc;

use crate::advisory::AuditConfig;
use crate::advisory::Auditor;
use crate::command::SelfUpdateCommand;
use crate::composer::PartialComposerHandle;
use crate::config::Config;
use crate::console::Application;
use crate::console::input::InputArgument;
use crate::console::input::InputDefinitionItem;
use crate::console::input::InputOption;
use crate::factory::Factory;
use crate::filter::platform_requirement_filter::PlatformRequirementFilterFactory;
use crate::filter::platform_requirement_filter::PlatformRequirementFilterInterface;
use crate::io::IOInterface;
use crate::io::IOInterfaceImmutable;
use crate::io::NullIO;
use crate::package::version::VersionParser;
use crate::plugin::PluginEvents;
use crate::plugin::PreCommandRunEvent;
use crate::util::Platform;

pub const SUCCESS: i64 = 0;
pub const FAILURE: i64 = 1;
pub const INVALID: i64 = 2;

/// \Composer\Composer\Command\BaseCommand + \Symfony\Component\Console\Command\Command
pub trait BaseCommand {
    fn new(_name: Option<&str>) -> Self
    where
        Self: Sized,
    {
        todo!()
    }

    fn get_name(&self) -> Option<String> {
        todo!()
    }

    fn set_name(&mut self, _name: &str) -> &mut Self
    where
        Self: Sized,
    {
        todo!()
    }

    fn get_description(&self) -> String {
        todo!()
    }

    fn set_description(&mut self, _description: &str) -> &mut Self
    where
        Self: Sized,
    {
        todo!()
    }

    fn set_help(&mut self, _help: &str) -> &mut Self
    where
        Self: Sized,
    {
        todo!()
    }

    fn set_definition(&mut self, _definition: &[InputDefinitionItem]) -> &mut Self
    where
        Self: Sized,
    {
        todo!()
    }

    fn get_definition(&self) -> &InputDefinition {
        todo!()
    }

    fn add_argument(
        &mut self,
        _name: &str,
        _mode: Option<i64>,
        _description: &str,
        _default: PhpMixed,
    ) -> &mut Self
    where
        Self: Sized,
    {
        todo!()
    }

    fn add_option(
        &mut self,
        _name: &str,
        _shortcut: Option<&str>,
        _mode: Option<i64>,
        _description: &str,
        _default: PhpMixed,
    ) -> &mut Self
    where
        Self: Sized,
    {
        todo!()
    }

    fn set_aliases(&mut self, _aliases: &[String]) -> &mut Self
    where
        Self: Sized,
    {
        todo!()
    }

    fn get_aliases(&self) -> Vec<String> {
        todo!()
    }

    fn set_hidden(&mut self, _hidden: bool) -> &mut Self
    where
        Self: Sized,
    {
        todo!()
    }

    fn is_hidden(&self) -> bool {
        todo!()
    }

    fn run(
        &mut self,
        _input: &mut dyn InputInterface,
        _output: &mut dyn OutputInterface,
    ) -> anyhow::Result<i64> {
        todo!()
    }

    fn get_helper(&self, _name: &str) -> PhpMixed {
        todo!()
    }

    fn get_helper_set(&self) -> PhpMixed {
        todo!()
    }

    /// Gets the application instance for this command.
    fn get_application(&self) -> Result<Application>;

    /// @deprecated since Composer 2.3.0 use requireComposer or tryComposer depending on whether you have $required set to true or false
    fn get_composer(
        &mut self,
        required: bool,
        disable_plugins: Option<bool>,
        disable_scripts: Option<bool>,
    ) -> Result<Option<PartialComposerHandle>>;

    /// Retrieves the default Composer\Composer instance or throws
    fn require_composer(
        &mut self,
        disable_plugins: Option<bool>,
        disable_scripts: Option<bool>,
    ) -> Result<PartialComposerHandle>;

    /// Retrieves the default Composer\Composer instance or null
    fn try_composer(
        &mut self,
        disable_plugins: Option<bool>,
        disable_scripts: Option<bool>,
    ) -> Option<PartialComposerHandle>;

    fn set_composer(&mut self, composer: PartialComposerHandle);

    /// Removes the cached composer instance
    fn reset_composer(&mut self) -> Result<()>;

    /// Whether or not this command is meant to call another command.
    fn is_proxy_command(&self) -> bool;

    fn get_io(&mut self) -> Rc<RefCell<dyn IOInterface>>;

    fn set_io(&mut self, io: Rc<RefCell<dyn IOInterface>>);

    // TODO(cli-completion): fn complete(&self, input: &CompletionInput, suggestions: &mut CompletionSuggestions);

    /// @inheritDoc
    fn initialize(
        &mut self,
        input: &mut dyn InputInterface,
        output: &mut dyn OutputInterface,
    ) -> Result<()>;

    /// Calls {@see Factory::create()} with the given arguments, taking into account flags and default states for disabling scripts and plugins
    fn create_composer_instance(
        &self,
        input: &dyn InputInterface,
        io: std::rc::Rc<std::cell::RefCell<dyn IOInterface>>,
        config: Option<IndexMap<String, PhpMixed>>,
        disable_plugins: bool,
        disable_scripts: Option<bool>,
    ) -> Result<PartialComposerHandle>;

    /// Returns preferSource and preferDist values based on the configuration.
    fn get_preferred_install_options(
        &self,
        config: &Config,
        input: &dyn InputInterface,
        keep_vcs_requires_prefer_source: bool,
    ) -> Result<(bool, bool)>;

    fn get_platform_requirement_filter(
        &self,
        input: &dyn InputInterface,
    ) -> Result<Box<dyn PlatformRequirementFilterInterface>>;

    /// @param array<string> $requirements
    ///
    /// @return array<string, string>
    fn format_requirements(&self, requirements: Vec<String>) -> Result<IndexMap<String, String>>;

    /// @param array<string> $requirements
    ///
    /// @return list<array{name: string, version?: string}>
    fn normalize_requirements(
        &self,
        requirements: Vec<String>,
    ) -> Result<Vec<IndexMap<String, String>>>;

    /// @param array<TableSeparator|mixed[]> $table
    fn render_table(&self, table: Vec<PhpMixed>, output: &dyn OutputInterface);

    fn get_terminal_width(&self) -> i64;

    /// @internal
    /// @param 'format'|'audit-format' $optName
    /// @return Auditor::FORMAT_*
    fn get_audit_format(&self, input: &dyn InputInterface, opt_name: &str) -> Result<String>;

    /// Creates an AuditConfig from the Config object, optionally overriding security blocking based on input options
    fn create_audit_config(
        &self,
        config: &mut Config,
        input: &dyn InputInterface,
    ) -> Result<AuditConfig>;
}

#[derive(Debug)]
pub struct BaseCommandData {
    pub(crate) composer: Option<PartialComposerHandle>,
    pub(crate) io: Option<Rc<RefCell<dyn IOInterface>>>,
}

pub trait HasBaseCommandData {
    fn base_command_data(&self) -> &BaseCommandData;
    fn base_command_data_mut(&mut self) -> &mut BaseCommandData;

    fn composer(&self) -> Option<PartialComposerHandle> {
        self.base_command_data().composer.clone()
    }

    fn composer_mut(&mut self) -> &mut Option<PartialComposerHandle> {
        &mut self.base_command_data_mut().composer
    }

    fn io(&self) -> Option<Rc<RefCell<dyn IOInterface>>> {
        self.base_command_data().io.clone()
    }

    fn io_mut(&mut self) -> &mut Option<Rc<RefCell<dyn IOInterface>>> {
        &mut self.base_command_data_mut().io
    }
}

impl<C: HasBaseCommandData> BaseCommand for C {
    fn get_application(&self) -> Result<Application> {
        // TODO(phase-b): requires inner Symfony Command access
        todo!()
    }

    fn get_composer(
        &mut self,
        required: bool,
        disable_plugins: Option<bool>,
        disable_scripts: Option<bool>,
    ) -> Result<Option<PartialComposerHandle>> {
        if required {
            return Ok(Some(
                self.require_composer(disable_plugins, disable_scripts)?,
            ));
        }

        Ok(self.try_composer(disable_plugins, disable_scripts))
    }

    fn require_composer(
        &mut self,
        _disable_plugins: Option<bool>,
        _disable_scripts: Option<bool>,
    ) -> Result<PartialComposerHandle> {
        // TODO(phase-b): depends on Application::get_composer, which is still stubbed.
        let _ = RuntimeException {
            message: String::new(),
            code: 0,
        };
        todo!("require_composer pending Application::get_composer")
    }

    fn try_composer(
        &mut self,
        _disable_plugins: Option<bool>,
        _disable_scripts: Option<bool>,
    ) -> Option<PartialComposerHandle> {
        // TODO(phase-b): depends on Application::get_composer, which is still stubbed.
        todo!("try_composer pending Application::get_composer")
    }

    fn set_composer(&mut self, composer: PartialComposerHandle) {
        *self.composer_mut() = Some(composer);
    }

    fn reset_composer(&mut self) -> Result<()> {
        *self.composer_mut() = None;
        self.get_application()?.reset_composer();
        Ok(())
    }

    fn is_proxy_command(&self) -> bool {
        false
    }

    fn get_io(&mut self) -> Rc<RefCell<dyn IOInterface>> {
        if self.io().is_none() {
            // TODO(phase-b): requires inner Symfony Application access
            *self.io_mut() = Some(Rc::new(RefCell::new(NullIO::new())));
        }

        self.io().unwrap()
    }

    fn set_io(&mut self, io: Rc<RefCell<dyn IOInterface>>) {
        *self.io_mut() = Some(io);
    }

    // TODO(cli-completion): fn complete(&self, input: &CompletionInput, suggestions: &mut CompletionSuggestions)

    fn initialize(
        &mut self,
        input: &mut dyn InputInterface,
        output: &mut dyn OutputInterface,
    ) -> Result<()> {
        // initialize a plugin-enabled Composer instance, either local or global
        let mut disable_plugins = input.has_parameter_option(&["--no-plugins"], false);
        let mut disable_scripts = input.has_parameter_option(&["--no-scripts"], false);

        // TODO(phase-b): requires inner Symfony Application access for disable_plugins_by_default / disable_scripts_by_default
        // TODO(phase-b): `$this instanceof SelfUpdateCommand` not representable

        let composer = self.try_composer(Some(disable_plugins), Some(disable_scripts));
        let io = self.get_io();

        let disable_plugins_kind = if disable_plugins {
            crate::factory::DisablePlugins::All
        } else {
            crate::factory::DisablePlugins::None
        };
        let composer = if composer.is_none() {
            Factory::create_global(io.clone(), disable_plugins_kind, disable_scripts)
        } else {
            composer
        };
        if let Some(composer) = composer.as_ref() {
            // TODO(phase-b): requires inner Symfony Command get_name access
            let command_name: String = todo!();
            let pre_command_run_event = PreCommandRunEvent::new(
                PluginEvents::PRE_COMMAND_RUN.to_string(),
                input,
                command_name,
            );
            // TODO(phase-b): event_dispatcher.dispatch expects Option<Event>; need wrapper from
            // PreCommandRunEvent.
            let _ = composer.borrow_partial().get_event_dispatcher();
            let _ = pre_command_run_event.get_name();
        }

        if input.has_parameter_option(&["--no-ansi"], false) && input.has_option("no-progress") {
            input.set_option("no-progress", PhpMixed::Bool(true));
        }

        let env_options: IndexMap<&str, Vec<&str>> = [
            ("COMPOSER_NO_AUDIT", vec!["no-audit"]),
            ("COMPOSER_NO_DEV", vec!["no-dev", "update-no-dev"]),
            ("COMPOSER_PREFER_STABLE", vec!["prefer-stable"]),
            ("COMPOSER_PREFER_LOWEST", vec!["prefer-lowest"]),
            ("COMPOSER_MINIMAL_CHANGES", vec!["minimal-changes"]),
            ("COMPOSER_WITH_DEPENDENCIES", vec!["with-dependencies"]),
            (
                "COMPOSER_WITH_ALL_DEPENDENCIES",
                vec!["with-all-dependencies"],
            ),
            (
                "COMPOSER_NO_SECURITY_BLOCKING",
                vec!["no-security-blocking"],
            ),
        ]
        .into_iter()
        .collect();
        for (env_name, option_names) in &env_options {
            for option_name in option_names {
                if true == input.has_option(option_name) {
                    if false == input.get_option(option_name).as_bool().unwrap_or(false)
                        && Platform::get_env(env_name).map_or(false, |s| !s.is_empty() && s != "0")
                    {
                        input.set_option(option_name, PhpMixed::Bool(true));
                    }
                }
            }
        }

        if true == input.has_option("ignore-platform-reqs") {
            if !input
                .get_option("ignore-platform-reqs")
                .as_bool()
                .unwrap_or(false)
                && Platform::get_env("COMPOSER_IGNORE_PLATFORM_REQS")
                    .map_or(false, |s| !s.is_empty() && s != "0")
            {
                input.set_option("ignore-platform-reqs", PhpMixed::Bool(true));

                io.write_error("<warning>COMPOSER_IGNORE_PLATFORM_REQS is set. You may experience unexpected errors.</warning>");
            }
        }

        if true == input.has_option("ignore-platform-req")
            && (!input.has_option("ignore-platform-reqs")
                || !input
                    .get_option("ignore-platform-reqs")
                    .as_bool()
                    .unwrap_or(false))
        {
            let ignore_platform_req_env = Platform::get_env("COMPOSER_IGNORE_PLATFORM_REQ");
            let ignore_str = ignore_platform_req_env.clone().unwrap_or_default();
            if 0 == count(&input.get_option("ignore-platform-req"))
                && ignore_platform_req_env.is_some()
                && "" != ignore_str
            {
                input.set_option(
                    "ignore-platform-req",
                    PhpMixed::List(
                        explode(",", &ignore_str)
                            .into_iter()
                            .map(|s| Box::new(PhpMixed::String(s)))
                            .collect(),
                    ),
                );

                io.write_error(&format!(
                    "<warning>COMPOSER_IGNORE_PLATFORM_REQ is set to ignore {}. You may experience unexpected errors.</warning>",
                    ignore_str
                ));
            }
        }

        // TODO(phase-b): requires inner Symfony Command initialize
        Ok(())
    }

    fn create_composer_instance(
        &self,
        input: &dyn InputInterface,
        io: std::rc::Rc<std::cell::RefCell<dyn IOInterface>>,
        config: Option<IndexMap<String, PhpMixed>>,
        disable_plugins: bool,
        disable_scripts: Option<bool>,
    ) -> Result<PartialComposerHandle> {
        let disable_plugins =
            disable_plugins || input.has_parameter_option(&["--no-plugins"], false);
        let disable_scripts = disable_scripts.unwrap_or(false)
            || input.has_parameter_option(&["--no-scripts"], false);

        // TODO(phase-b): requires inner Symfony Application access for disable_plugins_by_default / disable_scripts_by_default
        let disable_plugins_kind = if disable_plugins {
            crate::factory::DisablePlugins::All
        } else {
            crate::factory::DisablePlugins::None
        };
        // TODO(phase-b): Option<IndexMap<String, PhpMixed>> -> Option<LocalConfigInput> conversion
        let _ = config;
        Factory::create(io, None, disable_plugins_kind, disable_scripts)
    }

    fn get_preferred_install_options(
        &self,
        config: &Config,
        input: &dyn InputInterface,
        keep_vcs_requires_prefer_source: bool,
    ) -> Result<(bool, bool)> {
        let mut prefer_source = false;
        let mut prefer_dist = false;

        match config.get("preferred-install").as_string().unwrap_or("") {
            "source" => {
                prefer_source = true;
            }
            "dist" => {
                prefer_dist = true;
            }
            "auto" | _ => {
                // noop
            }
        }

        if !input.has_option("prefer-dist") || !input.has_option("prefer-source") {
            return Ok((prefer_source, prefer_dist));
        }

        if input.has_option("prefer-install") && is_string(&input.get_option("prefer-install")) {
            if input.get_option("prefer-source").as_bool().unwrap_or(false) {
                return Err(InvalidArgumentException {
                    message: "--prefer-source can not be used together with --prefer-install"
                        .to_string(),
                    code: 0,
                }
                .into());
            }
            if input.get_option("prefer-dist").as_bool().unwrap_or(false) {
                return Err(InvalidArgumentException {
                    message: "--prefer-dist can not be used together with --prefer-install"
                        .to_string(),
                    code: 0,
                }
                .into());
            }
            let prefer_install = input.get_option("prefer-install");
            match prefer_install.as_string().unwrap_or("") {
                "dist" => {
                    // TODO(phase-b): InputInterface set_option needs &mut self
                    let _ = "input.set_option('prefer-dist', true)";
                }
                "source" => {
                    let _ = "input.set_option('prefer-source', true)";
                }
                "auto" => {
                    prefer_dist = false;
                    prefer_source = false;
                }
                other => {
                    return Err(UnexpectedValueException {
                        message: format!(
                            "--prefer-install accepts one of \"dist\", \"source\" or \"auto\", got {}",
                            other
                        ),
                        code: 0,
                    }
                    .into());
                }
            }
        }

        if input.get_option("prefer-source").as_bool().unwrap_or(false)
            || input.get_option("prefer-dist").as_bool().unwrap_or(false)
            || (keep_vcs_requires_prefer_source
                && input.has_option("keep-vcs")
                && input.get_option("keep-vcs").as_bool().unwrap_or(false))
        {
            prefer_source = input.get_option("prefer-source").as_bool().unwrap_or(false)
                || (keep_vcs_requires_prefer_source
                    && input.has_option("keep-vcs")
                    && input.get_option("keep-vcs").as_bool().unwrap_or(false));
            prefer_dist = input.get_option("prefer-dist").as_bool().unwrap_or(false);
        }

        Ok((prefer_source, prefer_dist))
    }

    fn get_platform_requirement_filter(
        &self,
        input: &dyn InputInterface,
    ) -> Result<Box<dyn PlatformRequirementFilterInterface>> {
        if !input.has_option("ignore-platform-reqs") || !input.has_option("ignore-platform-req") {
            return Err(LogicException {
                message:
                    "Calling getPlatformRequirementFilter from a command which does not define the --ignore-platform-req[s] flags is not permitted."
                        .to_string(),
                code: 0,
            }
            .into());
        }

        if true
            == input
                .get_option("ignore-platform-reqs")
                .as_bool()
                .unwrap_or(false)
        {
            return Ok(PlatformRequirementFilterFactory::ignore_all());
        }

        let ignores = input.get_option("ignore-platform-req");
        if count(&ignores) > 0 {
            return Ok(PlatformRequirementFilterFactory::from_bool_or_list(
                ignores,
            )?);
        }

        Ok(PlatformRequirementFilterFactory::ignore_nothing())
    }

    fn format_requirements(&self, requirements: Vec<String>) -> Result<IndexMap<String, String>> {
        let mut requires: IndexMap<String, String> = IndexMap::new();
        let requirements = self.normalize_requirements(requirements)?;
        for requirement in requirements {
            if !requirement.contains_key("version") {
                return Err(UnexpectedValueException {
                    message: format!(
                        "Option {} is missing a version constraint, use e.g. {}:^1.0",
                        requirement.get("name").map(|s| s.as_str()).unwrap_or(""),
                        requirement.get("name").map(|s| s.as_str()).unwrap_or(""),
                    ),
                    code: 0,
                }
                .into());
            }
            requires.insert(
                requirement.get("name").cloned().unwrap_or_default(),
                requirement.get("version").cloned().unwrap_or_default(),
            );
        }

        Ok(requires)
    }

    fn normalize_requirements(
        &self,
        requirements: Vec<String>,
    ) -> Result<Vec<IndexMap<String, String>>> {
        // TODO(phase-b): VersionParser has no public `new` yet
        let parser: VersionParser = todo!("VersionParser::new()");

        parser.parse_name_version_pairs(requirements)
    }

    fn render_table(&self, table: Vec<PhpMixed>, output: &dyn OutputInterface) {
        let mut renderer = Table::new(output);
        renderer.set_style("compact");
        renderer.set_rows(table).render();
        let _ = TableSeparator::new();
    }

    fn get_terminal_width(&self) -> i64 {
        let terminal = Terminal::new();
        let mut width = terminal.get_width();

        if Platform::is_windows() {
            width -= 1;
        } else {
            width = max(80, width);
        }

        width
    }

    fn get_audit_format(&self, input: &dyn InputInterface, opt_name: &str) -> Result<String> {
        if !input.has_option(opt_name) {
            return Err(LogicException {
                message: format!(
                    "This should not be called on a Command which has no {} option defined.",
                    opt_name
                ),
                code: 0,
            }
            .into());
        }

        let val = input.get_option(opt_name);
        let formats: Vec<Box<PhpMixed>> = Auditor::FORMATS
            .iter()
            .map(|s| Box::new(PhpMixed::String(s.to_string())))
            .collect();
        if !in_array(val.clone(), &PhpMixed::List(formats), true) {
            return Err(InvalidArgumentException {
                message: format!(
                    "--{} must be one of {}.",
                    opt_name,
                    Auditor::FORMATS.join(", ")
                ),
                code: 0,
            }
            .into());
        }

        Ok(val.as_string().unwrap_or("").to_string())
    }

    fn create_audit_config(
        &self,
        config: &mut Config,
        input: &dyn InputInterface,
    ) -> Result<AuditConfig> {
        // Handle both --audit and --no-audit flags
        let audit = if input.has_option("audit") {
            input.get_option("audit").as_bool().unwrap_or(false)
        } else {
            !(input.has_option("no-audit")
                && input.get_option("no-audit").as_bool().unwrap_or(false))
        };
        let audit_format = if input.has_option("audit-format") {
            self.get_audit_format(input, "audit-format")?
        } else {
            Auditor::FORMAT_SUMMARY.to_string()
        };

        let audit_config = AuditConfig::from_config(config, audit, &audit_format)?;

        if Platform::get_env("COMPOSER_NO_SECURITY_BLOCKING")
            .map_or(false, |s| !s.is_empty() && s != "0")
            || (input.has_option("no-security-blocking")
                && input
                    .get_option("no-security-blocking")
                    .as_bool()
                    .unwrap_or(false))
        {
            let audit_config = AuditConfig::new(
                audit_config.audit,
                audit_config.audit_format,
                audit_config.audit_abandoned,
                false, // blockInsecure
                audit_config.block_abandoned,
                audit_config.ignore_unreachable,
                audit_config.ignore_list_for_audit,
                audit_config.ignore_list_for_blocking,
                audit_config.ignore_severity_for_audit,
                audit_config.ignore_severity_for_blocking,
                audit_config.ignore_abandoned_for_audit,
                audit_config.ignore_abandoned_for_blocking,
            );
            return Ok(audit_config);
        }

        Ok(audit_config)
    }
}

// TODO(phase-b): bridge BaseCommand to Symfony Command for trait-object container usage.
// Cannot blanket-impl a foreign trait for a local generic (orphan rule); each concrete
// command must impl symfony Command itself, or a wrapper type must be introduced.