diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-08 22:14:12 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-08 22:14:12 +0900 |
| commit | f4cad2123b2af0de72bda4ce039e16e74f163f4e (patch) | |
| tree | 21803308c5ff41e23c9d3b117433eea16b4ff663 /crates/shirabe-external-packages/src/symfony/console/input | |
| parent | 0209f63210e5b547b5c6b73367bb80ea86c255ec (diff) | |
| download | php-shirabe-f4cad2123b2af0de72bda4ce039e16e74f163f4e.tar.gz php-shirabe-f4cad2123b2af0de72bda4ce039e16e74f163f4e.tar.zst php-shirabe-f4cad2123b2af0de72bda4ce039e16e74f163f4e.zip | |
feat(php-shim): give ported exceptions PHP's class hierarchy
Ported exceptions were flat structs reached with `downcast_ref`, so
Composer's `catch (\RuntimeException $e)` only matched the exact leaf
type and `get_class($e)` had nothing to report. Each exception now
embeds an instance of the class it extends and travels inside an
`AnyThrowable`; `Catch::catch`/`catch_mut` walk that chain, and
`PhpClass::php_class_name` yields the PHP FQCN.
Dropping the `std::error::Error` impls from the exception types leaves
`AnyThrowable` as the only route into an `anyhow::Error`, so the walk
cannot be bypassed. A `no_exception_downcast` linter catches the
`downcast::<X>()` calls that would now silently answer `None`.
Three sites change behavior as a result: the `TransportException`
exit-code override reaches `MaxFileSizeExceededException`, the
`catch (\LogicException)` in findSimilar() reaches its subclasses, and
rendered exception titles carry the real class name rather than a
guess. `get_class_err()` is no longer a `todo!()`, which re-enables
FilesystemRepositoryTest::testCorruptedRepositoryFile.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-external-packages/src/symfony/console/input')
7 files changed, 150 insertions, 220 deletions
diff --git a/crates/shirabe-external-packages/src/symfony/console/input/argv_input.rs b/crates/shirabe-external-packages/src/symfony/console/input/argv_input.rs index fdc2dcc2..65ab9053 100644 --- a/crates/shirabe-external-packages/src/symfony/console/input/argv_input.rs +++ b/crates/shirabe-external-packages/src/symfony/console/input/argv_input.rs @@ -169,10 +169,10 @@ impl ArgvInput { shirabe_php_shim::mb_substr(name, i, Some(1), Some(&encoding)) } }; - return Err(RuntimeException(shirabe_php_shim::RuntimeException { - message: format!("The \"-{}\" option does not exist.", bad), - code: 0, - }) + return Err(RuntimeException::new(format!( + "The \"-{}\" option does not exist.", + bad + )) .into()); } @@ -292,9 +292,7 @@ impl ArgvInput { format!("No arguments expected, got \"{}\".", token) }; - return Err( - RuntimeException(shirabe_php_shim::RuntimeException { message, code: 0 }).into(), - ); + return Err(RuntimeException::new(message).into()); } Ok(()) @@ -303,10 +301,10 @@ impl ArgvInput { /// Adds a short option value. fn add_short_option(&mut self, shortcut: &str, value: PhpMixed) -> anyhow::Result<()> { if !self.inner.definition.has_shortcut(shortcut) { - return Err(RuntimeException(shirabe_php_shim::RuntimeException { - message: format!("The \"-{}\" option does not exist.", shortcut), - code: 0, - }) + return Err(RuntimeException::new(format!( + "The \"-{}\" option does not exist.", + shortcut + )) .into()); } @@ -323,19 +321,19 @@ impl ArgvInput { fn add_long_option(&mut self, name: &str, mut value: PhpMixed) -> anyhow::Result<()> { if !self.inner.definition.has_option(name) { if !self.inner.definition.has_negation(name) { - return Err(RuntimeException(shirabe_php_shim::RuntimeException { - message: format!("The \"--{}\" option does not exist.", name), - code: 0, - }) + return Err(RuntimeException::new(format!( + "The \"--{}\" option does not exist.", + name + )) .into()); } let option_name = self.inner.definition.negation_to_name(name)?; if !matches!(value, PhpMixed::Null) { - return Err(RuntimeException(shirabe_php_shim::RuntimeException { - message: format!("The \"--{}\" option does not accept a value.", name), - code: 0, - }) + return Err(RuntimeException::new(format!( + "The \"--{}\" option does not accept a value.", + name + )) .into()); } self.inner @@ -348,10 +346,10 @@ impl ArgvInput { let option = self.inner.definition.get_option(name)?; if !matches!(value, PhpMixed::Null) && !option.accept_value() { - return Err(RuntimeException(shirabe_php_shim::RuntimeException { - message: format!("The \"--{}\" option does not accept a value.", name), - code: 0, - }) + return Err(RuntimeException::new(format!( + "The \"--{}\" option does not accept a value.", + name + )) .into()); } @@ -373,10 +371,10 @@ impl ArgvInput { if matches!(value, PhpMixed::Null) { if option.is_value_required() { - return Err(RuntimeException(shirabe_php_shim::RuntimeException { - message: format!("The \"--{}\" option requires a value.", name), - code: 0, - }) + return Err(RuntimeException::new(format!( + "The \"--{}\" option requires a value.", + name + )) .into()); } diff --git a/crates/shirabe-external-packages/src/symfony/console/input/array_input.rs b/crates/shirabe-external-packages/src/symfony/console/input/array_input.rs index 9a19b839..de89a340 100644 --- a/crates/shirabe-external-packages/src/symfony/console/input/array_input.rs +++ b/crates/shirabe-external-packages/src/symfony/console/input/array_input.rs @@ -154,11 +154,9 @@ impl ArrayInput { /// Adds a short option value. fn add_short_option(&mut self, shortcut: &str, value: PhpMixed) -> anyhow::Result<()> { if !self.inner.definition.has_shortcut(shortcut) { - return Err(InvalidOptionException(InvalidArgumentException( - shirabe_php_shim::InvalidArgumentException { - message: format!("The \"-{}\" option does not exist.", shortcut), - code: 0, - }, + return Err(InvalidOptionException::new(format!( + "The \"-{}\" option does not exist.", + shortcut )) .into()); } @@ -176,11 +174,9 @@ impl ArrayInput { fn add_long_option(&mut self, name: &str, mut value: PhpMixed) -> anyhow::Result<()> { if !self.inner.definition.has_option(name) { if !self.inner.definition.has_negation(name) { - return Err(InvalidOptionException(InvalidArgumentException( - shirabe_php_shim::InvalidArgumentException { - message: format!("The \"--{}\" option does not exist.", name), - code: 0, - }, + return Err(InvalidOptionException::new(format!( + "The \"--{}\" option does not exist.", + name )) .into()); } @@ -197,11 +193,9 @@ impl ArrayInput { if matches!(value, PhpMixed::Null) { if option.is_value_required() { - return Err(InvalidOptionException(InvalidArgumentException( - shirabe_php_shim::InvalidArgumentException { - message: format!("The \"--{}\" option requires a value.", name), - code: 0, - }, + return Err(InvalidOptionException::new(format!( + "The \"--{}\" option requires a value.", + name )) .into()); } @@ -219,13 +213,11 @@ impl ArrayInput { /// Adds an argument value. fn add_argument(&mut self, name: &PhpMixed, value: PhpMixed) -> anyhow::Result<()> { if !self.inner.definition.has_argument(name) { - return Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: format!("The \"{}\" argument does not exist.", name.clone()), - code: 0, - }) - .into(), - ); + return Err(InvalidArgumentException::new(format!( + "The \"{}\" argument does not exist.", + name.clone() + )) + .into()); } self.inner diff --git a/crates/shirabe-external-packages/src/symfony/console/input/input.rs b/crates/shirabe-external-packages/src/symfony/console/input/input.rs index e14ed354..ceff85f6 100644 --- a/crates/shirabe-external-packages/src/symfony/console/input/input.rs +++ b/crates/shirabe-external-packages/src/symfony/console/input/input.rs @@ -80,13 +80,10 @@ impl Input { ); if !missing_arguments.is_empty() { - return Err(RuntimeException(shirabe_php_shim::RuntimeException { - message: format!( - "Not enough arguments (missing: \"{}\").", - shirabe_php_shim::implode(", ", &missing_arguments), - ), - code: 0, - }) + return Err(RuntimeException::new(format!( + "Not enough arguments (missing: \"{}\").", + shirabe_php_shim::implode(", ", &missing_arguments), + )) .into()); } @@ -113,13 +110,11 @@ impl Input { .definition .has_argument(&PhpMixed::String(name.to_string())) { - return Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: format!("The \"{}\" argument does not exist.", name), - code: 0, - }) - .into(), - ); + return Err(InvalidArgumentException::new(format!( + "The \"{}\" argument does not exist.", + name + )) + .into()); } Ok(match self.arguments.get(name) { @@ -137,13 +132,11 @@ impl Input { .definition .has_argument(&PhpMixed::String(name.to_string())) { - return Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: format!("The \"{}\" argument does not exist.", name), - code: 0, - }) - .into(), - ); + return Err(InvalidArgumentException::new(format!( + "The \"{}\" argument does not exist.", + name + )) + .into()); } self.arguments.insert(name.to_string(), value); @@ -174,13 +167,11 @@ impl Input { } if !self.definition.has_option(name) { - return Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: format!("The \"{}\" option does not exist.", name), - code: 0, - }) - .into(), - ); + return Err(InvalidArgumentException::new(format!( + "The \"{}\" option does not exist.", + name + )) + .into()); } Ok(if self.options.contains_key(name) { @@ -198,13 +189,11 @@ impl Input { return Ok(()); } else if !self.definition.has_option(name) { - return Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: format!("The \"{}\" option does not exist.", name), - code: 0, - }) - .into(), - ); + return Err(InvalidArgumentException::new(format!( + "The \"{}\" option does not exist.", + name + )) + .into()); } self.options.insert(name.to_string(), value); diff --git a/crates/shirabe-external-packages/src/symfony/console/input/input_argument.rs b/crates/shirabe-external-packages/src/symfony/console/input/input_argument.rs index 4a732bd8..35f62389 100644 --- a/crates/shirabe-external-packages/src/symfony/console/input/input_argument.rs +++ b/crates/shirabe-external-packages/src/symfony/console/input/input_argument.rs @@ -26,13 +26,11 @@ impl InputArgument { let mode = match mode { None => Self::OPTIONAL, Some(m) if !(1..=7).contains(&m) => { - return Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: format!("Argument mode \"{}\" is not valid.", m), - code: 0, - }) - .into(), - ); + return Err(InvalidArgumentException::new(format!( + "Argument mode \"{}\" is not valid.", + m + )) + .into()); } Some(m) => m, }; @@ -63,11 +61,9 @@ impl InputArgument { pub fn set_default(&mut self, default: PhpMixed) -> anyhow::Result<()> { if self.is_required() && !matches!(default, PhpMixed::Null) { - return Err(LogicException(shirabe_php_shim::LogicException { - message: "Cannot set a default value except for InputArgument::OPTIONAL mode." - .to_string(), - code: 0, - }) + return Err(LogicException::new( + "Cannot set a default value except for InputArgument::OPTIONAL mode.".to_string(), + ) .into()); } @@ -76,11 +72,9 @@ impl InputArgument { PhpMixed::Null => PhpMixed::List(vec![]), PhpMixed::List(_) => default, _ => { - return Err(LogicException(shirabe_php_shim::LogicException { - message: "A default value for an array argument must be an array." - .to_string(), - code: 0, - }) + return Err(LogicException::new( + "A default value for an array argument must be an array.".to_string(), + ) .into()); } } diff --git a/crates/shirabe-external-packages/src/symfony/console/input/input_definition.rs b/crates/shirabe-external-packages/src/symfony/console/input/input_definition.rs index eabb7003..bd32a8f9 100644 --- a/crates/shirabe-external-packages/src/symfony/console/input/input_definition.rs +++ b/crates/shirabe-external-packages/src/symfony/console/input/input_definition.rs @@ -110,39 +110,30 @@ impl InputDefinition { let argument = std::rc::Rc::new(argument); if self.arguments.contains_key(argument.get_name()) { - return Err(LogicException(shirabe_php_shim::LogicException { - message: format!( - "An argument with name \"{}\" already exists.", - argument.get_name(), - ), - code: 0, - }) + return Err(LogicException::new(format!( + "An argument with name \"{}\" already exists.", + argument.get_name(), + )) .into()); } if let Some(last_array_argument) = &self.last_array_argument { - return Err(LogicException(shirabe_php_shim::LogicException { - message: format!( - "Cannot add a required argument \"{}\" after an array argument \"{}\".", - argument.get_name(), - last_array_argument.get_name(), - ), - code: 0, - }) + return Err(LogicException::new(format!( + "Cannot add a required argument \"{}\" after an array argument \"{}\".", + argument.get_name(), + last_array_argument.get_name(), + )) .into()); } if argument.is_required() && let Some(last_optional_argument) = &self.last_optional_argument { - return Err(LogicException(shirabe_php_shim::LogicException { - message: format!( - "Cannot add a required argument \"{}\" after an optional one \"{}\".", - argument.get_name(), - last_optional_argument.get_name(), - ), - code: 0, - }) + return Err(LogicException::new(format!( + "Cannot add a required argument \"{}\" after an optional one \"{}\".", + argument.get_name(), + last_optional_argument.get_name(), + )) .into()); } @@ -165,13 +156,11 @@ impl InputDefinition { /// Returns an InputArgument by name or by position. pub fn get_argument(&self, name: &PhpMixed) -> anyhow::Result<std::rc::Rc<InputArgument>> { if !self.has_argument(name) { - return Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: format!("The \"{}\" argument does not exist.", name.clone()), - code: 0, - }) - .into(), - ); + return Err(InvalidArgumentException::new(format!( + "The \"{}\" argument does not exist.", + name.clone() + )) + .into()); } match name { @@ -260,17 +249,17 @@ impl InputDefinition { if let Some(existing) = self.options.get(option.get_name()) && !option.equals(existing) { - return Err(LogicException(shirabe_php_shim::LogicException { - message: format!("An option named \"{}\" already exists.", option.get_name()), - code: 0, - }) + return Err(LogicException::new(format!( + "An option named \"{}\" already exists.", + option.get_name() + )) .into()); } if self.negations.contains_key(option.get_name()) { - return Err(LogicException(shirabe_php_shim::LogicException { - message: format!("An option named \"{}\" already exists.", option.get_name()), - code: 0, - }) + return Err(LogicException::new(format!( + "An option named \"{}\" already exists.", + option.get_name() + )) .into()); } @@ -279,13 +268,10 @@ impl InputDefinition { if let Some(existing_name) = self.shortcuts.get(&shortcut) && !option.equals(&self.options[existing_name]) { - return Err(LogicException(shirabe_php_shim::LogicException { - message: format!( - "An option with shortcut \"{}\" already exists.", - shortcut.clone(), - ), - code: 0, - }) + return Err(LogicException::new(format!( + "An option with shortcut \"{}\" already exists.", + shortcut.clone(), + )) .into()); } } @@ -303,10 +289,10 @@ impl InputDefinition { if option.is_negatable() { let negated_name = format!("no-{}", option.get_name()); if self.options.contains_key(&negated_name) { - return Err(LogicException(shirabe_php_shim::LogicException { - message: format!("An option named \"{}\" already exists.", negated_name), - code: 0, - }) + return Err(LogicException::new(format!( + "An option named \"{}\" already exists.", + negated_name + )) .into()); } self.negations @@ -319,13 +305,11 @@ impl InputDefinition { /// Returns an InputOption by name. pub fn get_option(&self, name: &str) -> anyhow::Result<std::rc::Rc<InputOption>> { if !self.has_option(name) { - return Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: format!("The \"--{}\" option does not exist.", name), - code: 0, - }) - .into(), - ); + return Err(InvalidArgumentException::new(format!( + "The \"--{}\" option does not exist.", + name + )) + .into()); } Ok(std::rc::Rc::clone(&self.options[name])) @@ -374,13 +358,11 @@ impl InputDefinition { /// Returns the InputOption name given a shortcut. pub fn shortcut_to_name(&self, shortcut: &str) -> anyhow::Result<String> { match self.shortcuts.get(shortcut) { - None => Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: format!("The \"-{}\" option does not exist.", shortcut), - code: 0, - }) - .into(), - ), + None => Err(InvalidArgumentException::new(format!( + "The \"-{}\" option does not exist.", + shortcut + )) + .into()), Some(name) => Ok(name.clone()), } } @@ -388,13 +370,11 @@ impl InputDefinition { /// Returns the InputOption name given a negation. pub fn negation_to_name(&self, negation: &str) -> anyhow::Result<String> { match self.negations.get(negation) { - None => Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: format!("The \"--{}\" option does not exist.", negation), - code: 0, - }) - .into(), - ), + None => Err(InvalidArgumentException::new(format!( + "The \"--{}\" option does not exist.", + negation + )) + .into()), Some(name) => Ok(name.clone()), } } diff --git a/crates/shirabe-external-packages/src/symfony/console/input/input_option.rs b/crates/shirabe-external-packages/src/symfony/console/input/input_option.rs index 3f8fdeaf..79eff582 100644 --- a/crates/shirabe-external-packages/src/symfony/console/input/input_option.rs +++ b/crates/shirabe-external-packages/src/symfony/console/input/input_option.rs @@ -34,13 +34,10 @@ impl InputOption { }; if name.is_empty() { - return Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: "An option name cannot be empty.".to_string(), - code: 0, - }) - .into(), - ); + return Err(InvalidArgumentException::new( + "An option name cannot be empty.".to_string(), + ) + .into()); } let shortcut = match shortcut { @@ -69,13 +66,11 @@ impl InputOption { let mode = match mode { None => Self::VALUE_NONE, Some(m) if !(1..(Self::VALUE_NEGATABLE << 1)).contains(&m) => { - return Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: format!("Option mode \"{}\" is not valid.", m), - code: 0, - }) - .into(), - ); + return Err(InvalidArgumentException::new(format!( + "Option mode \"{}\" is not valid.", + m + )) + .into()); } Some(m) => m, }; @@ -89,17 +84,11 @@ impl InputOption { }; if option.is_array() && !option.accept_value() { - return Err(InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: "Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.".to_string(), - code: 0, - }) + return Err(InvalidArgumentException::new("Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.".to_string()) .into()); } if option.is_negatable() && option.accept_value() { - return Err(InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: "Impossible to have an option mode VALUE_NEGATABLE if the option also accepts a value.".to_string(), - code: 0, - }) + return Err(InvalidArgumentException::new("Impossible to have an option mode VALUE_NEGATABLE if the option also accepts a value.".to_string()) .into()); } @@ -115,13 +104,10 @@ impl InputOption { shirabe_php_shim::array_filter(&parts, |s: &String| !s.is_empty()); let result = shirabe_php_shim::implode("|", &filtered); if result.is_empty() { - return Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: "An option shortcut cannot be empty.".to_string(), - code: 0, - }) - .into(), - ); + return Err(InvalidArgumentException::new( + "An option shortcut cannot be empty.".to_string(), + ) + .into()); } Ok(Some(result)) } @@ -157,11 +143,9 @@ impl InputOption { pub fn set_default(&mut self, default: PhpMixed) -> anyhow::Result<()> { if Self::VALUE_NONE == (Self::VALUE_NONE & self.mode) && !matches!(default, PhpMixed::Null) { - return Err(LogicException(shirabe_php_shim::LogicException { - message: "Cannot set a default value when using InputOption::VALUE_NONE mode." - .to_string(), - code: 0, - }) + return Err(LogicException::new( + "Cannot set a default value when using InputOption::VALUE_NONE mode.".to_string(), + ) .into()); } @@ -171,11 +155,9 @@ impl InputOption { // PHP `is_array()` accepts both list-style and associative arrays. PhpMixed::List(_) | PhpMixed::Array(_) => default, _ => { - return Err(LogicException(shirabe_php_shim::LogicException { - message: "A default value for an array option must be an array." - .to_string(), - code: 0, - }) + return Err(LogicException::new( + "A default value for an array option must be an array.".to_string(), + ) .into()); } } diff --git a/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs b/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs index 3b3c3207..0ca8c975 100644 --- a/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs +++ b/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs @@ -127,16 +127,11 @@ impl StringInput { shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or("")); } else { // should never happen - return Err( - InvalidArgumentException(shirabe_php_shim::InvalidArgumentException { - message: format!( - "Unable to parse input near \"... {} ...\".", - shirabe_php_shim::substr(input, cursor, Some(10)), - ), - code: 0, - }) - .into(), - ); + return Err(InvalidArgumentException::new(format!( + "Unable to parse input near \"... {} ...\".", + shirabe_php_shim::substr(input, cursor, Some(10)), + )) + .into()); } } |
