From 27c961ee7bee5c775e1902e59f74a9871d31321d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 24 Jun 2026 00:49:51 +0900 Subject: feat(question): model Question hierarchy via QuestionInterface trait Introduce a QuestionInterface trait that the base Question and all its subclasses (ChoiceQuestion, ConfirmationQuestion, StrictConfirmationQuestion) implement, with as_choice/as_confirmation downcasts standing in for PHP's instanceof. Consumers (QuestionHelper, SymfonyQuestionHelper, SymfonyStyle, ConsoleIO) now take a QuestionInterface boundary generically. This fixes the instanceof emulation, which previously went through as_any().downcast_ref on a concrete &Question and always returned None, and unblocks the select/confirm/choice paths that were left as todo!() because a polymorphic ChoiceQuestion/ConfirmationQuestion could not be passed to ask. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../src/symfony/console/helper/question_helper.rs | 29 +++++++++------------- .../console/helper/symfony_question_helper.rs | 22 +++++----------- 2 files changed, 18 insertions(+), 33 deletions(-) (limited to 'crates/shirabe-external-packages/src/symfony/console/helper') diff --git a/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs b/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs index 9eb8fff..1fc90bd 100644 --- a/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs +++ b/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs @@ -15,8 +15,8 @@ use crate::symfony::console::output::console_output_interface::ConsoleOutputInte use crate::symfony::console::output::console_section_output::ConsoleSectionOutput; use crate::symfony::console::output::output_interface; use crate::symfony::console::output::output_interface::OutputInterface; -use crate::symfony::console::question::choice_question::ChoiceQuestion; -use crate::symfony::console::question::question::Question; +use crate::symfony::console::question::ChoiceQuestion; +use crate::symfony::console::question::QuestionInterface; use crate::symfony::console::terminal::Terminal; use crate::symfony::string::s; use shirabe_php_shim::AsAny; @@ -48,7 +48,7 @@ impl QuestionHelper { &mut self, input: &mut dyn InputInterface, output: Rc>, - question: &Question, + question: &impl QuestionInterface, ) -> anyhow::Result> { let mut output = output; let error_output = { @@ -115,7 +115,7 @@ impl QuestionHelper { fn do_ask( &self, output: Rc>, - question: &Question, + question: &impl QuestionInterface, ) -> anyhow::Result> { self.write_prompt(Rc::clone(&output), question); @@ -220,7 +220,7 @@ impl QuestionHelper { } /// @return mixed - fn get_default_answer(&self, question: &Question) -> PhpMixed { + fn get_default_answer(&self, question: &impl QuestionInterface) -> PhpMixed { let default = question.get_default(); if matches!(default, PhpMixed::Null) { @@ -230,7 +230,7 @@ impl QuestionHelper { if let Some(validator) = question.get_validator() { // call_user_func($question->getValidator(), $default) return validator(Some(default)).unwrap(); - } else if let Some(choice_question) = question_as_choice_question(question) { + } else if let Some(choice_question) = question.as_choice() { let choices = choice_question.get_choices(); if !choice_question.is_multiselect() { @@ -262,11 +262,11 @@ impl QuestionHelper { pub(crate) fn write_prompt( &self, output: Rc>, - question: &Question, + question: &impl QuestionInterface, ) { let mut message = question.get_question().to_string(); - if let Some(choice_question) = question_as_choice_question(question) { + if let Some(choice_question) = question.as_choice() { let mut lines = vec![question.get_question().to_string()]; lines.extend(self.format_choice_question_choices(choice_question, "info")); output @@ -339,7 +339,7 @@ impl QuestionHelper { fn autocomplete( &self, output: Rc>, - question: &Question, + question: &impl QuestionInterface, input_stream: &shirabe_php_shim::PhpResource, autocomplete: &dyn Fn(&str) -> Vec, ) -> String { @@ -518,7 +518,7 @@ impl QuestionHelper { let mut temp_ret = ret.clone(); - if let Some(choice_question) = question_as_choice_question(question) + if let Some(choice_question) = question.as_choice() && choice_question.is_multiselect() { temp_ret = self.most_recently_entered_value(&full_choice); @@ -680,7 +680,7 @@ impl QuestionHelper { &self, interviewer: &dyn Fn() -> anyhow::Result>, output: Rc>, - question: &Question, + question: &impl QuestionInterface, ) -> anyhow::Result> { let mut error: Option = None; let mut attempts = question.get_max_attempts(); @@ -752,7 +752,7 @@ impl QuestionHelper { fn read_input( &self, input_stream: &shirabe_php_shim::PhpResource, - question: &Question, + question: &impl QuestionInterface, ) -> PhpMixed { if !question.is_multiline() { let cp = self.set_io_codepage(); @@ -869,11 +869,6 @@ impl QuestionHelper { } } -/// Downcasts a Question to a ChoiceQuestion if applicable. -fn question_as_choice_question(question: &Question) -> Option<&ChoiceQuestion> { - question.as_any().downcast_ref::() -} - // PHP `__FILE__` magic constant. The shim's `file()` is PHP's file() function, // not the magic constant, and there is no `__FILE__` shim yet (see report). fn magic_file() -> String { diff --git a/crates/shirabe-external-packages/src/symfony/console/helper/symfony_question_helper.rs b/crates/shirabe-external-packages/src/symfony/console/helper/symfony_question_helper.rs index c1d8455..9940896 100644 --- a/crates/shirabe-external-packages/src/symfony/console/helper/symfony_question_helper.rs +++ b/crates/shirabe-external-packages/src/symfony/console/helper/symfony_question_helper.rs @@ -4,9 +4,7 @@ use crate::symfony::console::formatter::output_formatter::OutputFormatter; use crate::symfony::console::helper::question_helper::QuestionHelper; use crate::symfony::console::output::output_interface; use crate::symfony::console::output::output_interface::OutputInterface; -use crate::symfony::console::question::choice_question::ChoiceQuestion; -use crate::symfony::console::question::confirmation_question::ConfirmationQuestion; -use crate::symfony::console::question::question::Question; +use crate::symfony::console::question::QuestionInterface; use crate::symfony::console::style::symfony_style::SymfonyStyle; use shirabe_php_shim::AsAny; use shirabe_php_shim::PhpMixed; @@ -28,7 +26,7 @@ impl SymfonyQuestionHelper { pub(crate) fn write_prompt( &self, output: Rc>, - question: &Question, + question: &impl QuestionInterface, ) { let mut text = OutputFormatter::escape_trailing_backslash(question.get_question()); let default = question.get_default(); @@ -43,11 +41,7 @@ impl SymfonyQuestionHelper { // switch (true) if matches!(default, PhpMixed::Null) { text = format!(" {}:", PhpMixed::String(text)); - } else if question - .as_any() - .downcast_ref::() - .is_some() - { + } else if question.as_confirmation().is_some() { text = format!( " {} (yes/no) [{}]:", PhpMixed::String(text), @@ -60,11 +54,7 @@ impl SymfonyQuestionHelper { .to_string(), ), ); - } else if let Some(choice_question) = question - .as_any() - .downcast_ref::() - .filter(|q| q.is_multiselect()) - { + } else if let Some(choice_question) = question.as_choice().filter(|q| q.is_multiselect()) { let choices = choice_question.get_choices(); let default_parts = shirabe_php_shim::explode(",", &default.to_string()); @@ -83,7 +73,7 @@ impl SymfonyQuestionHelper { PhpMixed::String(text), PhpMixed::String(OutputFormatter::escape(&resolved.join(", ")).unwrap()), ); - } else if let Some(choice_question) = question.as_any().downcast_ref::() { + } else if let Some(choice_question) = question.as_choice() { let choices = choice_question.get_choices(); text = format!( " {} [{}]:", @@ -113,7 +103,7 @@ impl SymfonyQuestionHelper { let mut prompt = " > ".to_string(); - if let Some(choice_question) = question.as_any().downcast_ref::() { + if let Some(choice_question) = question.as_choice() { output.borrow().writeln( &self .inner -- cgit v1.3.1