From 0800c90a7ec0bc7a3d4c13063dfe6d2298a557bc Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 19 Aug 2026 23:45:37 +0900 Subject: 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) --- crates/shirabe-symfony-console/src/input/input.rs | 47 ++++++++++------------- 1 file changed, 20 insertions(+), 27 deletions(-) (limited to 'crates/shirabe-symfony-console/src/input/input.rs') 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, - pub(crate) options: IndexMap, - pub(crate) arguments: IndexMap, + pub(crate) options: IndexMap, + pub(crate) arguments: IndexMap, 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 { + pub fn get_arguments(&self) -> IndexMap { shirabe_php_shim::array_merge_map( self.definition.get_argument_defaults(), self.arguments.clone(), ) } - pub fn get_argument(&self, name: &str) -> anyhow::Result { - if !self - .definition - .has_argument(&PhpMixed::String(name.to_string())) - { + pub fn get_argument(&self, name: &str) -> anyhow::Result { + 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 { + pub fn get_options(&self) -> IndexMap { shirabe_php_shim::array_merge_map( self.definition.get_option_defaults(), self.options.clone(), ) } - pub fn get_option(&self, name: &str) -> anyhow::Result { + pub fn get_option(&self, name: &str) -> anyhow::Result { 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) { -- cgit v1.3.1-4-g156e