diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-18 08:37:30 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-18 08:37:30 +0900 |
| commit | 4856f05e870ec5d2daaa74bf9a86a1519a813614 (patch) | |
| tree | ab81b7a9d59340c4d05ecaf3b6b1a81106cc8355 /crates/shirabe | |
| parent | 5aaa92f760a975c6617b5d1fb7af8e58fed127b7 (diff) | |
| download | php-shirabe-4856f05e870ec5d2daaa74bf9a86a1519a813614.tar.gz php-shirabe-4856f05e870ec5d2daaa74bf9a86a1519a813614.tar.zst php-shirabe-4856f05e870ec5d2daaa74bf9a86a1519a813614.zip | |
fix(input): return a bool|string|string[]|null enum from get_option
`InputInterface::get_option` returned `PhpMixed`, so the negation branch
of `Input::get_option` reproduced PHP's `return !$value;` as
`!value.as_bool().unwrap_or(false)`, which inverts the result for a
string value instead of leaving it `false`. It now returns
`InputOptionValue`, whose `to_bool` is PHP's truthiness cast.
The narrowing also removes the `Vec<PhpMixed>` element handling the
commands carried for array options: `as_array` hands back `&[String]`,
so the `filter_map(|v| v.as_string())` chains at eight call sites
collapse. Its other accessors keep `PhpMixed`'s names and meanings
(`is_null`, `as_bool`, `as_string`, `to_bool`), and
`From<InputOptionValue> for PhpMixed` covers the callers that feed the
value back into an `IndexMap<String, PhpMixed>` or a `PhpMixed`
parameter.
`Input` keeps its parsed options and `InputOption` its defaults as
`PhpMixed`, so `Input::get_option` is where the narrowing happens and
where a value outside the domain panics.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe')
| -rw-r--r-- | crates/shirabe/src/command/audit_command.rs | 8 | ||||
| -rw-r--r-- | crates/shirabe/src/command/base_command.rs | 15 | ||||
| -rw-r--r-- | crates/shirabe/src/command/create_project_command.rs | 15 | ||||
| -rw-r--r-- | crates/shirabe/src/command/init_command.rs | 34 | ||||
| -rw-r--r-- | crates/shirabe/src/command/outdated_command.rs | 12 | ||||
| -rw-r--r-- | crates/shirabe/src/command/reinstall_command.rs | 10 | ||||
| -rw-r--r-- | crates/shirabe/src/command/show_command.rs | 12 | ||||
| -rw-r--r-- | crates/shirabe/src/command/update_command.rs | 11 | ||||
| -rw-r--r-- | crates/shirabe/tests/installer_test.rs | 6 |
9 files changed, 50 insertions, 73 deletions
diff --git a/crates/shirabe/src/command/audit_command.rs b/crates/shirabe/src/command/audit_command.rs index b6a3a150..d067e074 100644 --- a/crates/shirabe/src/command/audit_command.rs +++ b/crates/shirabe/src/command/audit_command.rs @@ -243,12 +243,8 @@ impl Command for AuditCommand { let mut ignore_severities: indexmap::IndexMap<String, Option<String>> = indexmap::IndexMap::new(); let cli_severities = input.borrow().get_option("ignore-severity")?; - if let Some(list) = cli_severities.as_list() { - for sev in list { - if let Some(s) = sev.as_string() { - ignore_severities.insert(s.to_string(), None); - } - } + for severity in cli_severities.as_array().unwrap_or_default() { + ignore_severities.insert(severity.clone(), None); } for (k, v) in audit_config.ignore_severity_for_audit.clone() { ignore_severities.insert(k, v); diff --git a/crates/shirabe/src/command/base_command.rs b/crates/shirabe/src/command/base_command.rs index d7f935c1..8ec9f350 100644 --- a/crates/shirabe/src/command/base_command.rs +++ b/crates/shirabe/src/command/base_command.rs @@ -20,7 +20,7 @@ use crate::util::Platform; use indexmap::IndexMap; use shirabe_php_shim::{ InvalidArgumentException, LogicException, PhpClass, PhpMixed, RuntimeException, - UnexpectedValueException, count, explode, in_array_strict, is_string, + UnexpectedValueException, count, explode, in_array_strict, }; use shirabe_symfony_console::Terminal; use shirabe_symfony_console::command::{Command, CommandData, SetDefinitionArg}; @@ -445,7 +445,11 @@ impl BaseCommand for BaseCommandData { } if input.borrow().has_option("prefer-install") - && is_string(&input.borrow().get_option("prefer-install")?) + && input + .borrow() + .get_option("prefer-install")? + .as_string() + .is_some() { if input .borrow() @@ -556,7 +560,7 @@ impl BaseCommand for BaseCommandData { return Ok(PlatformRequirementFilterFactory::ignore_all()); } - let ignores = input.borrow().get_option("ignore-platform-req")?; + let ignores: PhpMixed = input.borrow().get_option("ignore-platform-req")?.into(); if count(&ignores) > 0 { return PlatformRequirementFilterFactory::from_bool_or_list(ignores); } @@ -906,8 +910,9 @@ pub fn base_command_initialize( { let ignore_platform_req_env = Platform::get_env("COMPOSER_IGNORE_PLATFORM_REQ"); let ignore_str = ignore_platform_req_env.clone().unwrap_or_default(); - if 0 == count(&input.borrow().get_option("ignore-platform-req")?) - && ignore_platform_req_env.is_some() + if 0 == count(&PhpMixed::from( + input.borrow().get_option("ignore-platform-req")?, + )) && ignore_platform_req_env.is_some() && !ignore_str.is_empty() { input.borrow_mut().set_option( diff --git a/crates/shirabe/src/command/create_project_command.rs b/crates/shirabe/src/command/create_project_command.rs index b2842d9d..eb23ffff 100644 --- a/crates/shirabe/src/command/create_project_command.rs +++ b/crates/shirabe/src/command/create_project_command.rs @@ -926,15 +926,12 @@ impl Command for CreateProjectCommand { let repository_opt = input.borrow().get_option("repository")?; let repository_url_opt = input.borrow().get_option("repository-url")?; - let repositories = if repository_opt - .as_list() - .map(|l| !l.is_empty()) - .unwrap_or(false) - { - Some(repository_opt) - } else { - Some(repository_url_opt) - }; + let repositories: Option<PhpMixed> = + if repository_opt.as_array().is_some_and(|l| !l.is_empty()) { + Some(repository_opt.into()) + } else { + Some(repository_url_opt.into()) + }; self.install_project( io, diff --git a/crates/shirabe/src/command/init_command.rs b/crates/shirabe/src/command/init_command.rs index 2205b852..d1b5dbe5 100644 --- a/crates/shirabe/src/command/init_command.rs +++ b/crates/shirabe/src/command/init_command.rs @@ -527,12 +527,8 @@ impl Command for InitCommand { let repositories: Vec<String> = input .borrow() .get_option("repository")? - .as_list() - .map(|l| { - l.iter() - .filter_map(|v| v.as_string().map(|s| s.to_string())) - .collect() - }) + .as_array() + .map(<[String]>::to_vec) .unwrap_or_default(); if (repositories.len() as i64) > 0 { let config = std::rc::Rc::new(std::cell::RefCell::new(Factory::create_config( @@ -790,12 +786,8 @@ impl Command for InitCommand { let repositories: Vec<String> = input .borrow() .get_option("repository")? - .as_list() - .map(|l| { - l.iter() - .filter_map(|v| v.as_string().map(|s| s.to_string())) - .collect() - }) + .as_array() + .map(<[String]>::to_vec) .unwrap_or_default(); if (repositories.len() as i64) > 0 { let config = std::rc::Rc::new(std::cell::RefCell::new(Factory::create_config( @@ -1048,7 +1040,7 @@ impl Command for InitCommand { "Package Type (e.g. library, project, metapackage, composer-plugin) [<comment>{}</comment>]: ", type_str ), - type_val, + type_val.into(), )?; if type_value.as_string() == Some("") || matches!(type_value, PhpMixed::Bool(false)) { type_value = PhpMixed::Null; @@ -1126,12 +1118,8 @@ impl Command for InitCommand { let require: Vec<String> = input .borrow() .get_option("require")? - .as_list() - .map(|l| { - l.iter() - .filter_map(|v| v.as_string().map(|s| s.to_string())) - .collect() - }) + .as_array() + .map(<[String]>::to_vec) .unwrap_or_default(); let requirements = if (require.len() as i64) > 0 || io.ask_confirmation(question, true) { @@ -1156,12 +1144,8 @@ impl Command for InitCommand { let require_dev: Vec<String> = input .borrow() .get_option("require-dev")? - .as_list() - .map(|l| { - l.iter() - .filter_map(|v| v.as_string().map(|s| s.to_string())) - .collect() - }) + .as_array() + .map(<[String]>::to_vec) .unwrap_or_default(); let dev_requirements = if (require_dev.len() as i64) > 0 || io.ask_confirmation(question, true) { diff --git a/crates/shirabe/src/command/outdated_command.rs b/crates/shirabe/src/command/outdated_command.rs index 3994ab64..a89a1cd8 100644 --- a/crates/shirabe/src/command/outdated_command.rs +++ b/crates/shirabe/src/command/outdated_command.rs @@ -187,7 +187,7 @@ impl Command for OutdatedCommand { } args.insert( "--ignore-platform-req".to_string(), - input.borrow().get_option("ignore-platform-req")?, + input.borrow().get_option("ignore-platform-req")?.into(), ); if input .borrow() @@ -197,8 +197,14 @@ impl Command for OutdatedCommand { { args.insert("--ignore-platform-reqs".to_string(), PhpMixed::Bool(true)); } - args.insert("--format".to_string(), input.borrow().get_option("format")?); - args.insert("--ignore".to_string(), input.borrow().get_option("ignore")?); + args.insert( + "--format".to_string(), + input.borrow().get_option("format")?.into(), + ); + args.insert( + "--ignore".to_string(), + input.borrow().get_option("ignore")?.into(), + ); let input = ArrayInput::new( args.into_iter() diff --git a/crates/shirabe/src/command/reinstall_command.rs b/crates/shirabe/src/command/reinstall_command.rs index 010d2b5d..43978191 100644 --- a/crates/shirabe/src/command/reinstall_command.rs +++ b/crates/shirabe/src/command/reinstall_command.rs @@ -91,7 +91,7 @@ impl Command for ReinstallCommand { let mut package_names_to_reinstall: Vec<String> = vec![]; let type_option = input.borrow().get_option("type")?; - let type_count = type_option.as_list().map_or(0, |l| l.len()); + let type_count = type_option.as_array().map_or(0, <[String]>::len); let packages_arg = input.borrow().get_argument("packages")?; let packages_count = packages_arg.as_list().map_or(0, |l| l.len()); @@ -104,12 +104,8 @@ impl Command for ReinstallCommand { .into()); } let filter_types: Vec<String> = type_option - .as_list() - .map(|l| { - l.iter() - .filter_map(|v| v.as_string().map(|s| s.to_string())) - .collect() - }) + .as_array() + .map(<[String]>::to_vec) .unwrap_or_default(); for package in local_repo.get_canonical_packages()? { if filter_types.contains(&package.get_type()) { diff --git a/crates/shirabe/src/command/show_command.rs b/crates/shirabe/src/command/show_command.rs index 34b84fd8..3cc80b4c 100644 --- a/crates/shirabe/src/command/show_command.rs +++ b/crates/shirabe/src/command/show_command.rs @@ -1489,8 +1489,8 @@ impl Command for ShowCommand { } else if input .borrow() .get_option("ignore")? - .as_list() - .map_or(0, |l| l.len()) + .as_array() + .map_or(0, <[String]>::len) > 0 { self.get_io().write_error("<warning>You are using the option \"ignore\" for action other than \"outdated\", it will be ignored.</warning>"); @@ -2180,12 +2180,8 @@ impl Command for ShowCommand { &input .borrow() .get_option("ignore")? - .as_list() - .map(|l| { - l.iter() - .filter_map(|v| v.as_string().map(strtolower)) - .collect::<Vec<_>>() - }) + .as_array() + .map(|l| l.iter().map(|v| strtolower(v)).collect::<Vec<_>>()) .unwrap_or_default(), "{^(?:%s)$}iD", ); diff --git a/crates/shirabe/src/command/update_command.rs b/crates/shirabe/src/command/update_command.rs index 213ac248..d2e380ac 100644 --- a/crates/shirabe/src/command/update_command.rs +++ b/crates/shirabe/src/command/update_command.rs @@ -365,12 +365,8 @@ impl Command for UpdateCommand { input .borrow() .get_option("with")? - .as_list() - .map(|l| { - l.iter() - .filter_map(|v| v.as_string().map(|s| s.to_string())) - .collect() - }) + .as_array() + .map(<[String]>::to_vec) .unwrap_or_default(), )?; @@ -708,7 +704,8 @@ impl Command for UpdateCommand { .as_bool() .unwrap_or(false) { - let mut bump_after_update = input.borrow().get_option("bump-after-update")?; + let mut bump_after_update: PhpMixed = + input.borrow().get_option("bump-after-update")?.into(); // PHP: false === $bumpAfterUpdate (strict) if matches!(bump_after_update, PhpMixed::Bool(false)) { bump_after_update = composer.get_config().borrow().get("bump-after-update"); diff --git a/crates/shirabe/tests/installer_test.rs b/crates/shirabe/tests/installer_test.rs index 05776557..4d24186f 100644 --- a/crates/shirabe/tests/installer_test.rs +++ b/crates/shirabe/tests/installer_test.rs @@ -52,6 +52,7 @@ use shirabe_symfony_console::command::CommandData; use shirabe_symfony_console::input::InputArgument; use shirabe_symfony_console::input::InputInterface; use shirabe_symfony_console::input::InputOption; +use shirabe_symfony_console::input::InputOptionValue; use shirabe_symfony_console::input::StringInput; use shirabe_symfony_console::output::StreamOutput; use shirabe_symfony_console::output::{OutputInterface, VERBOSITY_NORMAL}; @@ -828,10 +829,9 @@ fn ignore_platform_reqs_value(input: &dyn InputInterface) -> PhpMixed { } let list = input .get_option("ignore-platform-req") - .unwrap_or(PhpMixed::Bool(false)); + .unwrap_or(InputOptionValue::Bool(false)); match &list { - PhpMixed::List(items) if !items.is_empty() => list, - PhpMixed::Array(map) if !map.is_empty() => list, + InputOptionValue::Array(items) if !items.is_empty() => list.into(), _ => PhpMixed::Bool(false), } } |
