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) --- crates/shirabe/src/io/console_io.rs | 21 ++++----- .../src/question/strict_confirmation_question.rs | 53 ++++++++++++++++++++++ 2 files changed, 61 insertions(+), 13 deletions(-) (limited to 'crates/shirabe/src') diff --git a/crates/shirabe/src/io/console_io.rs b/crates/shirabe/src/io/console_io.rs index d6e10da..c83308c 100644 --- a/crates/shirabe/src/io/console_io.rs +++ b/crates/shirabe/src/io/console_io.rs @@ -16,6 +16,7 @@ use shirabe_external_packages::symfony::console::output::output_interface::{ }; use shirabe_external_packages::symfony::console::question::ChoiceQuestion; use shirabe_external_packages::symfony::console::question::Question; +use shirabe_external_packages::symfony::console::question::QuestionInterface; use shirabe_php_shim::{ AsAny, PhpMixed, array_search, function_exists, implode, in_array, is_array, is_string, mb_check_encoding, mb_convert_encoding, microtime, sprintf, str_repeat, strip_tags, strlen, @@ -368,7 +369,7 @@ impl ConsoleIO { /// unrecoverable error here is a PHP fatal. The double `Result` is collapsed: the outer /// `anyhow::Result` (fatal) and the inner `MissingInputException` (unhandled, hence also fatal /// in PHP) both abort. - fn ask_question(&self, question: &Question) -> PhpMixed { + fn ask_question(&self, question: &impl QuestionInterface) -> PhpMixed { let error_output = self.get_error_output(); let mut question_helper = self.question_helper.borrow_mut(); let mut input = self.input.borrow_mut(); @@ -479,7 +480,7 @@ impl IOInterfaceImmutable for ConsoleIO { "/^no?$/i".to_string(), ); - let result = self.ask_question(question.inner()); + let result = self.ask_question(&question); result.as_bool().unwrap_or(false) } @@ -579,24 +580,18 @@ impl IOInterfaceImmutable for ConsoleIO { ChoiceQuestion::new(sanitized_question, sanitized_choices, sanitized_default) .expect("select() always provides at least one choice"); // PHP: IOInterface requires false, and Question requires null or int - let _max_attempts = match attempts { + let max_attempts = match attempts { PhpMixed::Bool(false) => None, PhpMixed::Int(i) => Some(i), _ => None, }; - // TODO(phase-c): PHP calls $question->setMaxAttempts($attempts), inherited from Question. - // ChoiceQuestion (shirabe-external-packages) keeps its inner Question private and exposes - // no setMaxAttempts passthrough, so the attempts cannot be propagated here yet. See report. + question + .set_max_attempts(max_attempts) + .expect("select() max attempts must be a positive value"); question.set_error_message(error_message); question.set_multiselect(multiselect); - // TODO(phase-c): QuestionHelper::ask takes a concrete `&Question`, but PHP passes a - // ChoiceQuestion whose getValidator/getDefault/autocompleter overrides drive the answer - // handling via polymorphism. Passing the inner Question would silently drop that behaviour. - // Faithful support needs the Symfony Question hierarchy modelled as a trait (or ask taking a - // trait object) so ChoiceQuestion's overrides dispatch — unlike StrictConfirmationQuestion, - // whose behaviour lives entirely on its inner Question (see ask_confirmation above). - let result: PhpMixed = todo!("call QuestionHelper::ask with a polymorphic ChoiceQuestion"); + let result: PhpMixed = self.ask_question(&question); // PHP: $isAssoc = (bool) \count(array_filter(array_keys($choices), 'is_string')); let choice_keys: Vec = match &choices { diff --git a/crates/shirabe/src/question/strict_confirmation_question.rs b/crates/shirabe/src/question/strict_confirmation_question.rs index 8d4239b..a464d65 100644 --- a/crates/shirabe/src/question/strict_confirmation_question.rs +++ b/crates/shirabe/src/question/strict_confirmation_question.rs @@ -3,8 +3,10 @@ use shirabe_external_packages::composer::pcre::Preg; use shirabe_external_packages::symfony::console::exception::InvalidArgumentException; use shirabe_external_packages::symfony::console::question::Question; +use shirabe_external_packages::symfony::console::question::QuestionInterface; use shirabe_php_shim::{PhpMixed, empty, is_bool}; +#[derive(Debug)] pub struct StrictConfirmationQuestion { inner: Question, true_answer_regex: String, @@ -80,3 +82,54 @@ impl StrictConfirmationQuestion { }) } } + +// PHP: `class StrictConfirmationQuestion extends Question` (not ConfirmationQuestion), +// so it is not an instanceof ChoiceQuestion/ConfirmationQuestion and keeps the default +// downcasts returning None. +impl QuestionInterface for StrictConfirmationQuestion { + fn get_question(&self) -> &str { + self.inner.get_question() + } + + fn get_default(&self) -> PhpMixed { + self.inner.get_default() + } + + fn is_multiline(&self) -> bool { + self.inner.is_multiline() + } + + fn is_hidden(&self) -> bool { + self.inner.is_hidden() + } + + fn is_hidden_fallback(&self) -> bool { + self.inner.is_hidden_fallback() + } + + fn get_autocompleter_values(&self) -> Option> { + self.inner.get_autocompleter_values() + } + + fn get_autocompleter_callback(&self) -> Option<&(dyn Fn(&str) -> Option>)> { + self.inner.get_autocompleter_callback() + } + + fn get_validator( + &self, + ) -> Option<&(dyn Fn(Option) -> Result)> { + self.inner.get_validator() + } + + fn get_max_attempts(&self) -> Option { + self.inner.get_max_attempts() + } + + fn get_normalizer(&self) -> Option<&(dyn Fn(PhpMixed) -> PhpMixed)> { + self.inner.get_normalizer() + } + + fn is_trimmable(&self) -> bool { + self.inner.is_trimmable() + } +} -- cgit v1.3.1