aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-symfony-console/src/input/input_value.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-19 23:45:37 +0900
committernsfisis <nsfisis@gmail.com>2026-08-19 23:45:37 +0900
commit0800c90a7ec0bc7a3d4c13063dfe6d2298a557bc (patch)
treeba5602549ce20b8d71136423e85dffc25deafff4 /crates/shirabe-symfony-console/src/input/input_value.rs
parent6ed3a85dd636366d194c9810fd777db7f25e263f (diff)
downloadphp-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_value.rs')
-rw-r--r--crates/shirabe-symfony-console/src/input/input_value.rs146
1 files changed, 146 insertions, 0 deletions
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<String>),
+}
+
+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<bool> {
+ 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<InputValue> 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<String> for InputValue {
+ fn from(value: String) -> Self {
+ Self::String(value)
+ }
+}
+
+impl From<bool> for InputValue {
+ fn from(value: bool) -> Self {
+ Self::Bool(value)
+ }
+}
+
+impl From<Option<String>> for InputValue {
+ fn from(value: Option<String>) -> Self {
+ match value {
+ Some(value) => Self::String(value),
+ None => Self::Null,
+ }
+ }
+}
+
+impl From<Vec<String>> for InputValue {
+ fn from(value: Vec<String>) -> Self {
+ Self::Array(value)
+ }
+}