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) --- .../src/input/input_value.rs | 146 +++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 crates/shirabe-symfony-console/src/input/input_value.rs (limited to 'crates/shirabe-symfony-console/src/input/input_value.rs') diff --git a/crates/shirabe-symfony-console/src/input/input_value.rs b/crates/shirabe-symfony-console/src/input/input_value.rs new file mode 100644 index 00000000..288c55d6 --- /dev/null +++ b/crates/shirabe-symfony-console/src/input/input_value.rs @@ -0,0 +1,146 @@ +//! ref: composer/vendor/symfony/console/Input/InputInterface.php + +/// The value an option or an argument can hold. +/// +/// Symfony declares the domain as `string|bool|int|float|array|null` and only ever narrows it in +/// PHPDoc; the arrays it stores are lists of strings. +#[derive(Debug, Clone, PartialEq)] +pub enum InputValue { + Null, + Bool(bool), + String(String), + Array(Vec), +} + +impl InputValue { + /// Narrows a PHP value to this domain. + /// + /// TODO(type-model): the question helpers and the plugin bridge still hand back a + /// `PhpMixed`, so a value outside this domain can only be rejected here. + pub fn from_php_mixed(value: &shirabe_php_shim::PhpMixed) -> Self { + use shirabe_php_shim::PhpMixed; + match value { + PhpMixed::Null => Self::Null, + PhpMixed::Bool(b) => Self::Bool(*b), + PhpMixed::String(s) => Self::String(s.clone()), + PhpMixed::List(_) | PhpMixed::Array(_) => Self::Array( + value + .values() + .into_iter() + .map(|item| match item { + PhpMixed::String(s) => s.clone(), + other => panic!("an input array holds {:?}, not a string", other), + }) + .collect(), + ), + other => panic!( + "an input value holds {:?}, not a bool, string, array or null", + other + ), + } + } + + pub fn is_null(&self) -> bool { + matches!(self, Self::Null) + } + + pub fn is_array(&self) -> bool { + matches!(self, Self::Array(_)) + } + + pub fn as_bool(&self) -> Option { + match self { + Self::Bool(b) => Some(*b), + _ => None, + } + } + + pub fn as_string(&self) -> Option<&str> { + match self { + Self::String(s) => Some(s.as_str()), + _ => None, + } + } + + pub fn as_array(&self) -> Option<&[String]> { + match self { + Self::Array(items) => Some(items), + _ => None, + } + } + + /// PHP's `(bool) $value`. + pub fn to_bool(&self) -> bool { + match self { + Self::Null => false, + Self::Bool(b) => *b, + Self::String(s) => !s.is_empty() && s != "0", + Self::Array(items) => !items.is_empty(), + } + } + + /// The value as a PHP `mixed`, for the shim functions that take one. + pub fn to_php_mixed(&self) -> shirabe_php_shim::PhpMixed { + self.clone().into() + } + + /// PHP's `(string) $value`, which is a fatal error for an array. + pub fn to_php_string(&self) -> String { + match self { + Self::Null => String::new(), + Self::Bool(b) => { + if *b { + "1".to_string() + } else { + String::new() + } + } + Self::String(s) => s.clone(), + Self::Array(_) => panic!("array to string conversion"), + } + } +} + +impl From for shirabe_php_shim::PhpMixed { + fn from(value: InputValue) -> Self { + match value { + InputValue::Null => Self::Null, + InputValue::Bool(b) => Self::Bool(b), + InputValue::String(s) => Self::String(s), + InputValue::Array(items) => Self::List(items.into_iter().map(Self::String).collect()), + } + } +} + +impl From<&str> for InputValue { + fn from(value: &str) -> Self { + Self::String(value.to_string()) + } +} + +impl From for InputValue { + fn from(value: String) -> Self { + Self::String(value) + } +} + +impl From for InputValue { + fn from(value: bool) -> Self { + Self::Bool(value) + } +} + +impl From> for InputValue { + fn from(value: Option) -> Self { + match value { + Some(value) => Self::String(value), + None => Self::Null, + } + } +} + +impl From> for InputValue { + fn from(value: Vec) -> Self { + Self::Array(value) + } +} -- cgit v1.3.1-4-g156e