diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-19 23:45:37 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-19 23:45:37 +0900 |
| commit | 0800c90a7ec0bc7a3d4c13063dfe6d2298a557bc (patch) | |
| tree | ba5602549ce20b8d71136423e85dffc25deafff4 /crates/shirabe-symfony-console/src/input/input.rs | |
| parent | 6ed3a85dd636366d194c9810fd777db7f25e263f (diff) | |
| download | php-shirabe-0800c90a7ec0bc7a3d4c13063dfe6d2298a557bc.tar.gz php-shirabe-0800c90a7ec0bc7a3d4c13063dfe6d2298a557bc.tar.zst php-shirabe-0800c90a7ec0bc7a3d4c13063dfe6d2298a557bc.zip | |
fix(input): thread a typed InputValue through the input layer
Options and arguments were stored and passed as PhpMixed even though
Symfony only ever puts a string, a bool, a list of strings or null in
one. get_option already narrowed to InputOptionValue at the boundary;
this widens that enum into InputValue and pushes it through
InputInterface, InputOption/InputArgument defaults, the Input storage,
ArgvInput/ArrayInput/StringInput/CompletionInput, Command::add_option
and add_argument, and the Composer-side wrappers.
Two neighbouring string|int unions get types of their own:
InputDefinition::{get_argument,has_argument} take an ArgumentName, and
ArrayInput keys its parameters by ParameterName. has_parameter_option
and get_parameter_option take the values they look for as &[&str],
which is what PHP's `(array) $values` cast produced anyway.
Two behaviours change along the way. Input::set_option on a negated
option now negates with PHP's loose bool cast rather than treating a
non-bool as false, matching `!$value`. ArrayInput::parse now resolves
an integer key to an argument position instead of looking up an
argument literally named "0".
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-symfony-console/src/input/input.rs')
| -rw-r--r-- | crates/shirabe-symfony-console/src/input/input.rs | 47 |
1 files changed, 20 insertions, 27 deletions
diff --git a/crates/shirabe-symfony-console/src/input/input.rs b/crates/shirabe-symfony-console/src/input/input.rs index 795e9c81..44519698 100644 --- a/crates/shirabe-symfony-console/src/input/input.rs +++ b/crates/shirabe-symfony-console/src/input/input.rs @@ -2,10 +2,11 @@ use crate::exception::InvalidArgumentException; use crate::exception::RuntimeException; +use crate::input::ArgumentName; use crate::input::InputDefinition; -use crate::input::InputOptionValue; +use crate::input::InputValue; use indexmap::IndexMap; -use shirabe_php_shim::{PhpMixed, PhpResource, php_regex, preg_is_match}; +use shirabe_php_shim::{PhpResource, php_regex, preg_is_match}; /// Input is the base class for all concrete Input classes. /// @@ -18,8 +19,8 @@ use shirabe_php_shim::{PhpMixed, PhpResource, php_regex, preg_is_match}; pub struct Input { pub(crate) definition: InputDefinition, stream: Option<PhpResource>, - pub(crate) options: IndexMap<String, PhpMixed>, - pub(crate) arguments: IndexMap<String, PhpMixed>, + pub(crate) options: IndexMap<String, InputValue>, + pub(crate) arguments: IndexMap<String, InputValue>, interactive: bool, } @@ -74,7 +75,7 @@ impl Input { |argument: &String| { !given_arguments.contains_key(argument) && definition - .get_argument(&PhpMixed::String(argument.clone())) + .get_argument(&ArgumentName::of(argument)) .map(|a| a.is_required()) .unwrap_or(false) }, @@ -99,18 +100,15 @@ impl Input { self.interactive = interactive; } - pub fn get_arguments(&self) -> IndexMap<String, PhpMixed> { + pub fn get_arguments(&self) -> IndexMap<String, InputValue> { shirabe_php_shim::array_merge_map( self.definition.get_argument_defaults(), self.arguments.clone(), ) } - pub fn get_argument(&self, name: &str) -> anyhow::Result<PhpMixed> { - if !self - .definition - .has_argument(&PhpMixed::String(name.to_string())) - { + pub fn get_argument(&self, name: &str) -> anyhow::Result<InputValue> { + if !self.definition.has_argument(&ArgumentName::of(name)) { return Err(InvalidArgumentException::new(format!( "The \"{}\" argument does not exist.", name @@ -122,17 +120,14 @@ impl Input { Some(value) => value.clone(), None => self .definition - .get_argument(&PhpMixed::String(name.to_string()))? + .get_argument(&ArgumentName::of(name))? .get_default() .clone(), }) } - pub fn set_argument(&mut self, name: &str, value: PhpMixed) -> anyhow::Result<()> { - if !self - .definition - .has_argument(&PhpMixed::String(name.to_string())) - { + pub fn set_argument(&mut self, name: &str, value: InputValue) -> anyhow::Result<()> { + if !self.definition.has_argument(&ArgumentName::of(name)) { return Err(InvalidArgumentException::new(format!( "The \"{}\" argument does not exist.", name @@ -146,25 +141,24 @@ impl Input { } pub fn has_argument(&self, name: &str) -> bool { - self.definition - .has_argument(&PhpMixed::String(name.to_string())) + self.definition.has_argument(&ArgumentName::of(name)) } - pub fn get_options(&self) -> IndexMap<String, PhpMixed> { + pub fn get_options(&self) -> IndexMap<String, InputValue> { shirabe_php_shim::array_merge_map( self.definition.get_option_defaults(), self.options.clone(), ) } - pub fn get_option(&self, name: &str) -> anyhow::Result<InputOptionValue> { + pub fn get_option(&self, name: &str) -> anyhow::Result<InputValue> { if self.definition.has_negation(name) { let value = self.get_option(&self.definition.negation_to_name(name)?)?; if value.is_null() { return Ok(value); } - return Ok(InputOptionValue::Bool(!value.to_bool())); + return Ok(InputValue::Bool(!value.to_bool())); } if !self.definition.has_option(name) { @@ -176,18 +170,17 @@ impl Input { } Ok(if let Some(value) = self.options.get(name) { - InputOptionValue::from_php_mixed(value) + value.clone() } else { - let option = self.definition.get_option(name)?; - InputOptionValue::from_php_mixed(option.get_default()) + self.definition.get_option(name)?.get_default().clone() }) } - pub fn set_option(&mut self, name: &str, value: PhpMixed) -> anyhow::Result<()> { + pub fn set_option(&mut self, name: &str, value: InputValue) -> anyhow::Result<()> { if self.definition.has_negation(name) { let negated = self.definition.negation_to_name(name)?; self.options - .insert(negated, PhpMixed::Bool(!value.as_bool().unwrap_or(false))); + .insert(negated, InputValue::Bool(!value.to_bool())); return Ok(()); } else if !self.definition.has_option(name) { |
