aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/console/command/command.rs
blob: 1640689a602e4a2aef88553ce0d2d30a6e9503fe (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
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
use crate::symfony::console::application::Application;
use crate::symfony::console::completion::completion_input::CompletionInput;
use crate::symfony::console::completion::completion_suggestions::CompletionSuggestions;
use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException;
use crate::symfony::console::exception::logic_exception::LogicException;
use crate::symfony::console::helper::helper_set::HelperSet;
use crate::symfony::console::input::input_argument::InputArgument;
use crate::symfony::console::input::input_definition::InputDefinition;
use crate::symfony::console::input::input_interface::InputInterface;
use crate::symfony::console::input::input_option::InputOption;
use crate::symfony::console::output::output_interface::{self, OutputInterface};
use indexmap::IndexMap;
use shirabe_php_shim::PhpMixed;
use std::cell::RefCell;
use std::rc::Rc;

/// Base class for all commands.
///
/// Phase B: the PHP `Command` class is split into the polymorphic `Command` trait
/// (defined below) and this concrete `BaseCommand` struct holding the base-class
/// state and behavior. Subclasses embed a `BaseCommand` and implement `Command`.
pub struct BaseCommand {
    application: Option<Rc<RefCell<dyn Application>>>,
    name: Option<String>,
    process_title: Option<String>,
    aliases: Vec<String>,
    definition: Option<InputDefinition>,
    hidden: bool,
    help: String,
    description: String,
    full_definition: Option<InputDefinition>,
    ignore_validation_errors: bool,
    // A callable(InputInterface, OutputInterface) -> i64.
    code: Option<Box<dyn Fn(&mut dyn InputInterface, &mut dyn OutputInterface) -> PhpMixed>>,
    synopsis: IndexMap<String, String>,
    usages: Vec<String>,
    helper_set: Option<Rc<RefCell<HelperSet>>>,
}

impl BaseCommand {
    // see https://tldp.org/LDP/abs/html/exitcodes.html
    pub const SUCCESS: i64 = 0;
    pub const FAILURE: i64 = 1;
    pub const INVALID: i64 = 2;

    /// The default command name.
    // NOTE: PHP `protected static $defaultName`; static late-binding property.
    pub const DEFAULT_NAME: Option<&'static str> = None;

    /// The default command description.
    // NOTE: PHP `protected static $defaultDescription`; static late-binding property.
    pub const DEFAULT_DESCRIPTION: Option<&'static str> = None;

    pub fn get_default_name() -> Option<String> {
        // TODO(review): PHP uses ReflectionClass to read the #[AsCommand] attribute
        // and ReflectionProperty to check that `$defaultName` is declared on the late-static
        // class itself (not inherited). Reflection-based late static binding cannot be
        // reproduced in Phase A; human review needed for the porting strategy.
        todo!()
    }

    pub fn get_default_description() -> Option<String> {
        // TODO(review): same Reflection/late-static-binding concern as get_default_name().
        todo!()
    }

    /// `$name` is the name of the command; passing None means it must be set in configure().
    ///
    /// Throws LogicException when the command name is empty.
    pub fn __construct(name: Option<String>) -> anyhow::Result<Self> {
        let mut this = BaseCommand {
            application: None,
            name: None,
            process_title: None,
            aliases: Vec::new(),
            definition: Some(InputDefinition::new(Vec::new())?),
            hidden: false,
            help: String::new(),
            description: String::new(),
            full_definition: None,
            ignore_validation_errors: false,
            code: None,
            synopsis: IndexMap::new(),
            usages: Vec::new(),
            helper_set: None,
        };

        let mut name = name;
        if name.is_none() {
            name = Self::get_default_name();
            if let Some(n) = name.clone() {
                let mut aliases: Vec<String> = n.split('|').map(|s| s.to_string()).collect();

                let first = if aliases.is_empty() {
                    None
                } else {
                    Some(aliases.remove(0))
                };
                name = first;
                if name.as_deref() == Some("") {
                    this.set_hidden(true);
                    name = if aliases.is_empty() {
                        None
                    } else {
                        Some(aliases.remove(0))
                    };
                }

                this.set_aliases(aliases)?;
            }
        }

        if let Some(n) = name {
            this.set_name(&n)?;
        }

        if this.description.is_empty() {
            this.set_description(&Self::get_default_description().unwrap_or_default());
        }

        this.configure();

        Ok(this)
    }

    /// Ignores validation errors.
    ///
    /// This is mainly useful for the help command.
    pub fn ignore_validation_errors(&mut self) {
        self.ignore_validation_errors = true;
    }

    pub fn set_application(&mut self, application: Option<Rc<RefCell<dyn Application>>>) {
        self.application = application.clone();
        if let Some(application) = application {
            self.set_helper_set(application.borrow_mut().get_helper_set());
        } else {
            self.helper_set = None;
        }

        self.full_definition = None;
    }

    pub fn set_helper_set(&mut self, helper_set: Rc<RefCell<HelperSet>>) {
        self.helper_set = Some(helper_set);
    }

    /// Gets the helper set.
    pub fn get_helper_set(&self) -> Option<Rc<RefCell<HelperSet>>> {
        self.helper_set.clone()
    }

    /// Gets the application instance for this command.
    pub fn get_application(&self) -> Option<Rc<RefCell<dyn Application>>> {
        self.application.clone()
    }

    /// Checks whether the command is enabled or not in the current environment.
    ///
    /// Override this to check for x or y and return false if the command cannot
    /// run properly under the current conditions.
    pub fn is_enabled(&self) -> bool {
        true
    }

    /// Configures the current command.
    pub fn configure(&mut self) {}

    /// Executes the current command.
    ///
    /// This method is not abstract because you can use this class
    /// as a concrete class. In this case, instead of defining the
    /// execute() method, you set the code to execute by passing
    /// a Closure to the set_code() method.
    ///
    /// Returns 0 if everything went fine, or an exit code.
    ///
    /// Throws LogicException when this abstract method is not implemented.
    pub fn execute(
        &mut self,
        _input: &mut dyn InputInterface,
        _output: &mut dyn OutputInterface,
    ) -> anyhow::Result<Result<i64, LogicException>> {
        Ok(Err(LogicException(shirabe_php_shim::LogicException {
            message: "You must override the execute() method in the concrete command class."
                .to_string(),
            code: 0,
        })))
    }

    /// Interacts with the user.
    ///
    /// This method is executed before the InputDefinition is validated.
    /// This means that this is the only place where the command can
    /// interactively ask for values of missing required arguments.
    pub fn interact(&mut self, _input: &mut dyn InputInterface, _output: &mut dyn OutputInterface) {
    }

    /// Initializes the command after the input has been bound and before the input
    /// is validated.
    ///
    /// This is mainly useful when a lot of commands extends one main command
    /// where some things need to be initialized based on the input arguments and options.
    pub fn initialize(
        &mut self,
        _input: &mut dyn InputInterface,
        _output: &mut dyn OutputInterface,
    ) {
    }

    /// Runs the command.
    ///
    /// The code to execute is either defined directly with the
    /// set_code() method or by overriding the execute() method
    /// in a sub-class.
    ///
    /// Returns the command exit code.
    ///
    /// Throws ExceptionInterface when input binding fails. Bypass this by calling ignore_validation_errors().
    pub fn run(
        &mut self,
        input: &mut dyn InputInterface,
        output: &mut dyn OutputInterface,
    ) -> anyhow::Result<i64> {
        // add the application arguments and options
        self.merge_application_definition(true);

        // bind the input against the command specific arguments/options
        match input.bind(self.get_definition()) {
            Ok(()) => {}
            Err(e) => {
                if !self.ignore_validation_errors {
                    return Err(e);
                }
            }
        }

        self.initialize(input, output);

        if let Some(process_title) = &self.process_title {
            // TODO: PHP probes for cli_set_process_title / setproctitle availability.
            if shirabe_php_shim::function_exists("cli_set_process_title") {
                if !shirabe_php_shim::cli_set_process_title(process_title) {
                    if shirabe_php_shim::PHP_OS == "Darwin" {
                        output.writeln(
                            &["<comment>Running \"cli_set_process_title\" as an unprivileged user is not supported on MacOS.</comment>".to_string()],
                            output_interface::VERBOSITY_VERY_VERBOSE,
                        );
                    } else {
                        shirabe_php_shim::cli_set_process_title(process_title);
                    }
                }
            } else if shirabe_php_shim::function_exists("setproctitle") {
                shirabe_php_shim::setproctitle(process_title);
            } else if output.get_verbosity() == output_interface::VERBOSITY_VERY_VERBOSE {
                output.writeln(
                    &["<comment>Install the proctitle PECL to be able to change the process title.</comment>".to_string()],
                    output_interface::OUTPUT_NORMAL,
                );
            }
        }

        if input.is_interactive() {
            self.interact(input, output);
        }

        // The command name argument is often omitted when a command is executed directly with its run() method.
        // It would fail the validation if we didn't make sure the command argument is present,
        // since it's required by the application.
        if input.has_argument("command") && matches!(input.get_argument("command")?, PhpMixed::Null)
        {
            input.set_argument("command", PhpMixed::from(self.get_name()))?;
        }

        input.validate()?;

        let status_code: PhpMixed;
        if let Some(code) = &self.code {
            status_code = code(input, output);
        } else {
            let executed = self.execute(input, output)?;
            let executed = match executed {
                Ok(v) => v,
                Err(e) => return Err(anyhow::Error::new(e)),
            };
            status_code = PhpMixed::from(executed);
            // PHP also raises \TypeError when execute() does not return int; in this
            // strongly-typed port execute() already returns an int, so the check is moot.
        }

        // is_numeric($statusCode) ? (int) $statusCode : 0
        Ok(shirabe_php_shim::is_numeric_to_int(&status_code))
    }

    /// Adds suggestions to `suggestions` for the current completion input (e.g. option or argument).
    pub fn complete(&self, _input: &CompletionInput, _suggestions: &mut CompletionSuggestions) {}

    /// Sets the code to execute when running this command.
    ///
    /// If this method is used, it overrides the code defined
    /// in the execute() method.
    ///
    /// `$code` is a callable(InputInterface, OutputInterface).
    ///
    /// Throws InvalidArgumentException.
    pub fn set_code(
        &mut self,
        code: Box<dyn Fn(&mut dyn InputInterface, &mut dyn OutputInterface) -> PhpMixed>,
    ) -> &mut Self {
        // TODO: PHP rebinds an unbound Closure's $this to the command instance via
        // ReflectionFunction/Closure::bind. Rust closures have no `$this` rebinding;
        // the closure is stored as-is.
        self.code = Some(code);

        self
    }

    /// Merges the application definition with the command definition.
    ///
    /// This method is not part of public API and should not be used directly.
    ///
    /// `$mergeArgs` is whether to merge or not the Application definition arguments to Command definition arguments.
    pub fn merge_application_definition(&mut self, merge_args: bool) {
        let _application = match &self.application {
            None => return,
            Some(application) => application.clone(),
        };

        // TODO: InputDefinition stores options/arguments as `Rc<InputOption>` /
        // `Rc<InputArgument>` but its setters (`set_options`/`set_arguments`) take owned
        // `Vec<InputOption>` / `Vec<InputArgument>`. Merging the application and command
        // definitions therefore requires an agreed-upon ownership model for the
        // definition entries (Phase C). Left as todo!() pending that design.
        let _ = merge_args;
        todo!()
    }

    /// Sets an array of argument and option instances.
    ///
    /// `$definition` is an array of argument and option instances or a definition instance.
    pub fn set_definition(&mut self, definition: SetDefinitionArg) -> &mut Self {
        match definition {
            SetDefinitionArg::Definition(definition) => {
                self.definition = Some(definition);
            }
            SetDefinitionArg::Array(definition) => {
                let _ = self.definition.as_mut().unwrap().set_definition(definition);
            }
        }

        self.full_definition = None;

        self
    }

    /// Gets the InputDefinition attached to this Command.
    pub fn get_definition(&self) -> &InputDefinition {
        match &self.full_definition {
            Some(full_definition) => full_definition,
            None => self.get_native_definition(),
        }
    }

    /// Gets the InputDefinition to be used to create representations of this Command.
    ///
    /// Can be overridden to provide the original command representation when it would otherwise
    /// be changed by merging with the application InputDefinition.
    ///
    /// This method is not part of public API and should not be used directly.
    pub fn get_native_definition(&self) -> &InputDefinition {
        match &self.definition {
            None => {
                // TODO(review): PHP throws LogicException here, but get_native_definition()
                // returns InputDefinition (no Result). In this port `definition` is set in
                // the constructor, so None should not occur; treated as a programming error.
                panic!(
                    "Command class is not correctly initialized. You probably forgot to call the parent constructor."
                );
            }
            Some(definition) => definition,
        }
    }

    /// Adds an argument.
    ///
    /// `$mode` is the argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL.
    /// `$default` is the default value (for InputArgument::OPTIONAL mode only).
    ///
    /// Throws InvalidArgumentException when argument mode is not valid.
    pub fn add_argument(
        &mut self,
        name: &str,
        mode: Option<i64>,
        description: &str,
        default: PhpMixed,
    ) -> anyhow::Result<&mut Self> {
        self.definition
            .as_mut()
            .unwrap()
            .add_argument(InputArgument::new(
                name.to_string(),
                mode,
                description.to_string(),
                default.clone(),
            )?)?;
        if self.full_definition.is_some() {
            self.full_definition
                .as_mut()
                .unwrap()
                .add_argument(InputArgument::new(
                    name.to_string(),
                    mode,
                    description.to_string(),
                    default,
                )?)?;
        }

        Ok(self)
    }

    /// Adds an option.
    ///
    /// `$shortcut` is the shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts.
    /// `$mode` is the option mode: One of the InputOption::VALUE_* constants.
    /// `$default` is the default value (must be null for InputOption::VALUE_NONE).
    ///
    /// Throws InvalidArgumentException if option mode is invalid or incompatible.
    pub fn add_option(
        &mut self,
        name: &str,
        shortcut: PhpMixed,
        mode: Option<i64>,
        description: &str,
        default: PhpMixed,
    ) -> anyhow::Result<&mut Self> {
        self.definition
            .as_mut()
            .unwrap()
            .add_option(InputOption::new(
                name,
                shortcut.clone(),
                mode,
                description.to_string(),
                default.clone(),
            )?)?;
        if self.full_definition.is_some() {
            self.full_definition
                .as_mut()
                .unwrap()
                .add_option(InputOption::new(
                    name,
                    shortcut,
                    mode,
                    description.to_string(),
                    default,
                )?)?;
        }

        Ok(self)
    }

    /// Sets the name of the command.
    ///
    /// This method can set both the namespace and the name if
    /// you separate them by a colon (:)
    ///
    ///     command.set_name("foo:bar");
    ///
    /// Throws InvalidArgumentException when the name is invalid.
    pub fn set_name(&mut self, name: &str) -> anyhow::Result<&mut Self> {
        if let Err(e) = self.validate_name(name)? {
            return Err(e.into());
        }

        self.name = Some(name.to_string());

        Ok(self)
    }

    /// Sets the process title of the command.
    ///
    /// This feature should be used only when creating a long process command,
    /// like a daemon.
    pub fn set_process_title(&mut self, title: &str) -> &mut Self {
        self.process_title = Some(title.to_string());

        self
    }

    /// Returns the command name.
    pub fn get_name(&self) -> Option<String> {
        self.name.clone()
    }

    /// `$hidden` is whether or not the command should be hidden from the list of commands.
    pub fn set_hidden(&mut self, hidden: bool) -> &mut Self {
        self.hidden = hidden;

        self
    }

    /// Returns whether the command should be publicly shown or not.
    pub fn is_hidden(&self) -> bool {
        self.hidden
    }

    /// Sets the description for the command.
    pub fn set_description(&mut self, description: &str) -> &mut Self {
        self.description = description.to_string();

        self
    }

    /// Returns the description for the command.
    pub fn get_description(&self) -> String {
        self.description.clone()
    }

    /// Sets the help for the command.
    pub fn set_help(&mut self, help: &str) -> &mut Self {
        self.help = help.to_string();

        self
    }

    /// Returns the help for the command.
    pub fn get_help(&self) -> String {
        self.help.clone()
    }

    /// Returns the processed help for the command replacing the %command.name% and
    /// %command.full_name% patterns with the real values dynamically.
    pub fn get_processed_help(&self) -> String {
        let name = self.name.clone();
        let is_single_command = match &self.application {
            Some(application) => application.borrow().is_single_command(),
            None => false,
        };

        let placeholders = [
            "%command.name%".to_string(),
            "%command.full_name%".to_string(),
        ];
        let php_self = shirabe_php_shim::server("PHP_SELF");
        let replacements = [
            name.clone().unwrap_or_default(),
            if is_single_command {
                php_self.clone()
            } else {
                format!("{} {}", php_self, name.unwrap_or_default())
            },
        ];

        let help = self.get_help();
        let subject = if help.is_empty() {
            self.get_description()
        } else {
            help
        };

        shirabe_php_shim::str_replace_array(&placeholders, &replacements, &subject)
    }

    /// Sets the aliases for the command.
    ///
    /// `$aliases` is an array of aliases for the command.
    ///
    /// Throws InvalidArgumentException when an alias is invalid.
    pub fn set_aliases(&mut self, aliases: Vec<String>) -> anyhow::Result<&mut Self> {
        let mut list = Vec::new();

        for alias in &aliases {
            if let Err(e) = self.validate_name(alias)? {
                return Err(e.into());
            }
            list.push(alias.clone());
        }

        // PHP: `\is_array($aliases) ? $aliases : $list`. Here `aliases` is always an
        // array (Vec), so the result is `aliases`; `list` mirrors the validation loop.
        self.aliases = aliases;

        Ok(self)
    }

    /// Returns the aliases for the command.
    pub fn get_aliases(&self) -> Vec<String> {
        self.aliases.clone()
    }

    /// Returns the synopsis for the command.
    ///
    /// `$short` is whether to show the short version of the synopsis (with options folded) or not.
    pub fn get_synopsis(&mut self, short: bool) -> String {
        let key = if short { "short" } else { "long" }.to_string();

        if !self.synopsis.contains_key(&key) {
            let value = format!(
                "{} {}",
                self.name.clone().unwrap_or_default(),
                self.definition.as_ref().unwrap().get_synopsis(short)
            )
            .trim()
            .to_string();
            self.synopsis.insert(key.clone(), value);
        }

        self.synopsis[&key].clone()
    }

    /// Add a command usage example, it'll be prefixed with the command name.
    pub fn add_usage(&mut self, usage: &str) -> &mut Self {
        let mut usage = usage.to_string();
        let name = self.name.clone().unwrap_or_default();
        if !usage.starts_with(&name) {
            usage = format!("{} {}", name, usage);
        }

        self.usages.push(usage);

        self
    }

    /// Returns alternative usages of the command.
    pub fn get_usages(&self) -> Vec<String> {
        self.usages.clone()
    }

    /// Gets a helper instance by name.
    ///
    /// Throws LogicException if no HelperSet is defined.
    /// Throws InvalidArgumentException if the helper is not defined.
    pub fn get_helper(&self, name: &str) -> anyhow::Result<Result<PhpMixed, LogicException>> {
        let helper_set = match &self.helper_set {
            None => {
                return Ok(Err(LogicException(shirabe_php_shim::LogicException {
                    message: format!(
                        "Cannot retrieve helper \"{}\" because there is no HelperSet defined. Did you forget to add your command to the application or to set the application on the command using the setApplication() method? You can also set the HelperSet directly using the setHelperSet() method.",
                        name
                    ),
                    code: 0,
                })));
            }
            Some(helper_set) => helper_set,
        };

        // TODO(review): HelperSet::get() returns `Rc<RefCell<dyn HelperInterface>>`, but
        // Command::getHelper() is typed `mixed` (PhpMixed) here and PhpMixed cannot hold a
        // helper instance. The helper return modelling needs a dedicated type (Phase C).
        let _ = helper_set;
        todo!()
    }

    /// Validates a command name.
    ///
    /// It must be non-empty and parts can optionally be separated by ":".
    ///
    /// Throws InvalidArgumentException when the name is invalid.
    fn validate_name(&self, name: &str) -> anyhow::Result<Result<(), InvalidArgumentException>> {
        let mut matches: Vec<Option<String>> = Vec::new();
        if shirabe_php_shim::preg_match(r"/^[^\:]++(\:[^\:]++)*$/", name, &mut matches) == 0 {
            return Ok(Err(InvalidArgumentException(
                shirabe_php_shim::InvalidArgumentException {
                    message: format!("Command name \"{}\" is invalid.", name),
                    code: 0,
                },
            )));
        }

        Ok(Ok(()))
    }
}

/// The argument of Command::set_definition(), which accepts either an array of
/// argument/option instances or an InputDefinition.
#[derive(Debug)]
pub enum SetDefinitionArg {
    Array(Vec<crate::symfony::console::input::input_definition::DefinitionItem>),
    Definition(InputDefinition),
}

/// Polymorphic interface for all commands (PHP's `Command` base class as seen by
/// callers that hold a command of unknown concrete type).
///
/// Phase B: default methods are `todo!()`; the concrete behavior lives on
/// `BaseCommand`'s inherent methods. Object-safe so `dyn Command` works.
pub trait Command: std::fmt::Debug + shirabe_php_shim::AsAny {
    fn clone_box(&self) -> Box<dyn Command> {
        todo!()
    }

    fn configure(&mut self) {
        todo!()
    }

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

    fn complete(&self, _input: &CompletionInput, _suggestions: &mut CompletionSuggestions) {
        todo!()
    }

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

    fn set_application(&mut self, _application: Option<Rc<RefCell<dyn Application>>>) {
        todo!()
    }

    fn get_application(&self) -> Option<Rc<RefCell<dyn Application>>> {
        todo!()
    }

    fn set_helper_set(&mut self, _helper_set: Rc<RefCell<HelperSet>>) {
        todo!()
    }

    fn get_helper_set(&self) -> Option<Rc<RefCell<HelperSet>>> {
        todo!()
    }

    fn merge_application_definition(&mut self, _merge_args: bool) {
        todo!()
    }

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

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

    fn set_name(&mut self, _name: &str) -> anyhow::Result<()> {
        todo!()
    }

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

    fn set_hidden(&mut self, _hidden: bool) {
        todo!()
    }

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

    fn set_description(&mut self, _description: &str) {
        todo!()
    }

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

    fn set_help(&mut self, _help: &str) {
        todo!()
    }

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

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

    fn set_aliases(&mut self, _aliases: Vec<String>) -> anyhow::Result<()> {
        todo!()
    }

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

    fn get_synopsis(&mut self, _short: bool) -> String {
        todo!()
    }

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

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

    fn ignore_validation_errors(&mut self) {
        todo!()
    }
}

impl Command for BaseCommand {}

impl std::fmt::Debug for BaseCommand {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BaseCommand")
            .field("name", &self.name)
            .field("aliases", &self.aliases)
            .field("hidden", &self.hidden)
            .field("description", &self.description)
            .finish_non_exhaustive()
    }
}