From efe5bdb1987411a473d4af15451a376d20928245 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 12 Jun 2026 03:19:34 +0900 Subject: refactor(php-shim): replace literal sprintf calls with format! Convert every sprintf() call with a compile-time literal format string to format!, implementing Display for PhpMixed (delegating to php_to_string) so PhpMixed values render with PHP string semantics through {}. Also merge the format!-wrapped and conditional-literal dynamic sites into single format! calls. Genuinely runtime format strings (table styles, configurable error messages, command synopsis, progress-bar modifiers, regex-built messages) still go through sprintf. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../src/event_dispatcher/event_dispatcher.rs | 128 ++++++++------------- 1 file changed, 50 insertions(+), 78 deletions(-) (limited to 'crates/shirabe/src/event_dispatcher') diff --git a/crates/shirabe/src/event_dispatcher/event_dispatcher.rs b/crates/shirabe/src/event_dispatcher/event_dispatcher.rs index d209e1e..c54df20 100644 --- a/crates/shirabe/src/event_dispatcher/event_dispatcher.rs +++ b/crates/shirabe/src/event_dispatcher/event_dispatcher.rs @@ -347,12 +347,10 @@ impl EventDispatcher { "?".to_string() }; self.io.write_error3( - &sprintf( - "> %s: %s", - &[ - PhpMixed::String(formatted_event_name_with_args.clone()), - PhpMixed::String(format!("{}->{}", prefix, method_name)), - ], + &format!( + "> {}: {}", + PhpMixed::String(formatted_event_name_with_args.clone()), + PhpMixed::String(format!("{}->{}", prefix, method_name)), ), true, crate::io::VERBOSE, @@ -364,12 +362,10 @@ impl EventDispatcher { match callable { Callable::String(ref callable_str) if self.is_composer_script(callable_str) => { self.io.write_error3( - &sprintf( - "> %s: %s", - &[ - PhpMixed::String(formatted_event_name_with_args.clone()), - PhpMixed::String(callable_str.clone()), - ], + &format!( + "> {}: {}", + PhpMixed::String(formatted_event_name_with_args.clone()), + PhpMixed::String(callable_str.clone()), ), true, crate::io::VERBOSE, @@ -424,15 +420,11 @@ impl EventDispatcher { ); let exit_code = self.execute_tty(&exec)?; if exit_code != 0 { - self.io.write_error3(&sprintf( - &format!( - "Script %s handling the %s event returned with error code {}", - exit_code - ), - &[ - PhpMixed::String(callable_str.clone()), - PhpMixed::String(event.get_name().to_string()), - ], + self.io.write_error3(&format!( + "Script {} handling the {} event returned with error code {}", + PhpMixed::String(callable_str.clone()), + PhpMixed::String(event.get_name().to_string()), + exit_code ), true, crate::io::QUIET); return Err(anyhow::anyhow!(ScriptExecutionException( @@ -454,9 +446,9 @@ impl EventDispatcher { )) .is_empty() { - self.io.write_error3(&sprintf( - "You made a reference to a non-existent script %s", - &[PhpMixed::String(callable_str.clone())], + self.io.write_error3(&format!( + "You made a reference to a non-existent script {}", + PhpMixed::String(callable_str.clone()), ), true, crate::io::QUIET); } @@ -481,12 +473,10 @@ impl EventDispatcher { Err(e) => { if e.downcast_ref::().is_some() { self.io.write_error3( - &sprintf( - "Script %s was called via %s", - &[ - PhpMixed::String(callable_str.clone()), - PhpMixed::String(event.get_name().to_string()), - ], + &format!( + "Script {} was called via {}", + PhpMixed::String(callable_str.clone()), + PhpMixed::String(event.get_name().to_string()), ), true, crate::io::QUIET, @@ -526,17 +516,13 @@ impl EventDispatcher { r#return = if let PhpMixed::Bool(false) = v { 1 } else { 0 }; } Err(e) => { - let message = - "Script %s handling the %s event terminated with an exception"; self.io.write_error3( &format!( "{}", - sprintf( - message, - &[ - PhpMixed::String(callable_str.clone()), - PhpMixed::String(event.get_name().to_string()), - ], + format!( + "Script {} handling the {} event terminated with an exception", + PhpMixed::String(callable_str.clone()), + PhpMixed::String(event.get_name().to_string()), ) ), true, @@ -630,17 +616,13 @@ impl EventDispatcher { match result { Ok(v) => r#return = v, Err(e) => { - let message = - "Script %s handling the %s event terminated with an exception"; self.io.write_error3( &format!( "{}", - sprintf( - message, - &[ - PhpMixed::String(callable_str.clone()), - PhpMixed::String(event.get_name().to_string()), - ], + format!( + "Script {} handling the {} event terminated with an exception", + PhpMixed::String(callable_str.clone()), + PhpMixed::String(event.get_name().to_string()), ) ), true, @@ -676,19 +658,17 @@ impl EventDispatcher { if self.io.is_verbose() { self.io.write_error3( - &sprintf( - "> %s: %s", - &[ - PhpMixed::String(event.get_name().to_string()), - PhpMixed::String(exec.clone()), - ], + &format!( + "> {}: {}", + PhpMixed::String(event.get_name().to_string()), + PhpMixed::String(exec.clone()), ), true, crate::io::NORMAL, ); } else if self.event_needs_to_output(event) { self.io.write_error3( - &sprintf("> %s", &[PhpMixed::String(exec.clone())]), + &format!("> {}", PhpMixed::String(exec.clone())), true, crate::io::NORMAL, ); @@ -815,15 +795,11 @@ impl EventDispatcher { let exit_code = self.execute_tty(&exec)?; if exit_code != 0 { - self.io.write_error3(&sprintf( - &format!( - "Script %s handling the %s event returned with error code {}", - exit_code - ), - &[ - PhpMixed::String(callable_str.clone()), - PhpMixed::String(event.get_name().to_string()), - ], + self.io.write_error3(&format!( + "Script {} handling the {} event returned with error code {}", + PhpMixed::String(callable_str.clone()), + PhpMixed::String(event.get_name().to_string()), + exit_code ), true, crate::io::QUIET); return Err(anyhow::anyhow!(ScriptExecutionException( @@ -921,25 +897,21 @@ impl EventDispatcher { ) -> anyhow::Result { if self.io.is_verbose() { self.io.write_error3( - &sprintf( - "> %s: %s::%s", - &[ - PhpMixed::String(event.get_name().to_string()), - PhpMixed::String(class_name.to_string()), - PhpMixed::String(method_name.to_string()), - ], + &format!( + "> {}: {}::{}", + PhpMixed::String(event.get_name().to_string()), + PhpMixed::String(class_name.to_string()), + PhpMixed::String(method_name.to_string()), ), true, crate::io::NORMAL, ); } else if self.event_needs_to_output(event) { self.io.write_error3( - &sprintf( - "> %s::%s", - &[ - PhpMixed::String(class_name.to_string()), - PhpMixed::String(method_name.to_string()), - ], + &format!( + "> {}::{}", + PhpMixed::String(class_name.to_string()), + PhpMixed::String(method_name.to_string()), ), true, crate::io::NORMAL, @@ -1111,9 +1083,9 @@ impl EventDispatcher { let event_name = event.get_name().to_string(); if self.event_stack.iter().any(|n| n == &event_name) { return Err(anyhow::anyhow!(RuntimeException { - message: sprintf( - "Circular call to script handler '%s' detected", - &[PhpMixed::String(event_name)], + message: format!( + "Circular call to script handler '{}' detected", + PhpMixed::String(event_name), ), code: 0, })); -- cgit v1.3.1