From 6047bcc3e63ab84dfc67bce94f402f1bfa3f58d5 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 2 Aug 2026 16:16:51 +0900 Subject: refactor(php-shim): introduce PhpClass for reporting PHP class names Rust has no runtime class name, so `Command::get_class` existed purely to let each command hand back its PHP class name, supplied through the two-argument variant of `delegate_command_trait_impls_to_inner!` at the impl site. Replace it with a general `PhpClass` trait plus an `impl_php_class!` macro, so the name is stated once next to the type definition and the mechanism is reusable outside commands. `Command` gains `PhpClass` as a supertrait and drops `get_class`, and `VcsDriverKind`'s hand-rolled `php_class_name` table moves onto the trait. Behavior is unchanged: the same class-name strings are reported, and the base command state still panics when asked for a name it cannot supply. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/symfony/console/command/command.rs | 27 ++++++++-------------- .../symfony/console/command/complete_command.rs | 14 ++++++----- .../console/command/dump_completion_command.rs | 12 ++++++---- .../src/symfony/console/command/help_command.rs | 12 ++++++---- .../src/symfony/console/command/list_command.rs | 12 ++++++---- 5 files changed, 38 insertions(+), 39 deletions(-) (limited to 'crates/shirabe-external-packages/src/symfony/console') diff --git a/crates/shirabe-external-packages/src/symfony/console/command/command.rs b/crates/shirabe-external-packages/src/symfony/console/command/command.rs index 1f1455f8..2af60db0 100644 --- a/crates/shirabe-external-packages/src/symfony/console/command/command.rs +++ b/crates/shirabe-external-packages/src/symfony/console/command/command.rs @@ -325,13 +325,6 @@ macro_rules! delegate_command_trait_impls_to_inner { $crate::delegate_to_inner!($field, fn ignore_validation_errors(&self)); $crate::delegate_to_inner!($field, fn get_ignore_validation_errors(&self) -> bool); }; - // Variant taking the command's PHP fully-qualified class name (see `Command::get_class`). - ($field:ident, $fqcn:literal) => { - $crate::delegate_command_trait_impls_to_inner!($field); - fn get_class(&self) -> String { - $fqcn.to_string() - } - }; } /// Polymorphic interface for all commands (PHP's `Command` base class as seen by @@ -341,7 +334,7 @@ macro_rules! delegate_command_trait_impls_to_inner { /// the state methods there and override the behavior hooks (`configure`/`execute`/...). /// Object-safe so `dyn Command` works. All methods take `&self`; the command's mutable /// state is interior-mutable (see [`CommandData`]). -pub trait Command: std::fmt::Debug + shirabe_php_shim::AsAny { +pub trait Command: std::fmt::Debug + shirabe_php_shim::AsAny + shirabe_php_shim::PhpClass { /// Configures the current command. fn configure(&self) -> anyhow::Result<()> { Ok(()) @@ -376,12 +369,6 @@ pub trait Command: std::fmt::Debug + shirabe_php_shim::AsAny { Ok(()) } - /// The PHP fully-qualified class name of the concrete command. Port hook for PHP's - /// `\get_class($command)` (used by `CompleteCommand`'s debug log), which Rust cannot - /// reflect from a trait object; every concrete command supplies its FQCN, usually via - /// `delegate_command_trait_impls_to_inner!($field, "Fqcn")`. - fn get_class(&self) -> String; - /// Adds suggestions to `suggestions` for the current completion input. /// /// PHP's `complete` is `void` but can throw; errors are surfaced through `anyhow::Result` @@ -571,15 +558,19 @@ pub trait Command: std::fmt::Debug + shirabe_php_shim::AsAny { fn get_ignore_validation_errors(&self) -> bool; } +impl shirabe_php_shim::PhpClass for CommandData { + fn php_class_name(&self) -> &'static str { + panic!( + "php_class_name called on the base command state; concrete commands supply their class name" + ); + } +} + impl Command for CommandData { fn is_enabled(&self) -> bool { true } - fn get_class(&self) -> String { - panic!("get_class called on the base command state; concrete commands supply their FQCN"); - } - fn set_application( &self, application: Option>>, diff --git a/crates/shirabe-external-packages/src/symfony/console/command/complete_command.rs b/crates/shirabe-external-packages/src/symfony/console/command/complete_command.rs index a4567829..8a2ccc97 100644 --- a/crates/shirabe-external-packages/src/symfony/console/command/complete_command.rs +++ b/crates/shirabe-external-packages/src/symfony/console/command/complete_command.rs @@ -11,7 +11,7 @@ use crate::symfony::console::input::input_interface::InputInterface; use crate::symfony::console::input::input_option::InputOption; use crate::symfony::console::output::output_interface::OutputInterface; use indexmap::IndexMap; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PhpMixed, impl_php_class}; use std::ops::{Deref, DerefMut}; /// Responsible for providing the values to the shell completion. @@ -22,6 +22,11 @@ pub struct CompleteCommand { is_debug: std::cell::Cell, } +impl_php_class!( + CompleteCommand, + r"Symfony\Component\Console\Command\CompleteCommand" +); + impl Deref for CompleteCommand { type Target = CommandData; @@ -145,7 +150,7 @@ impl CompleteCommand { fn get_class_of_command(command: &std::rc::Rc>) -> String { // LazyCommand is intentionally not ported. - command.borrow().get_class() + command.borrow().php_class_name().to_string() } fn get_definition_options( @@ -414,8 +419,5 @@ impl Command for CompleteCommand { } } - crate::delegate_command_trait_impls_to_inner!( - inner, - "Symfony\\Component\\Console\\Command\\CompleteCommand" - ); + crate::delegate_command_trait_impls_to_inner!(inner); } diff --git a/crates/shirabe-external-packages/src/symfony/console/command/dump_completion_command.rs b/crates/shirabe-external-packages/src/symfony/console/command/dump_completion_command.rs index 680ff962..31a94f0f 100644 --- a/crates/shirabe-external-packages/src/symfony/console/command/dump_completion_command.rs +++ b/crates/shirabe-external-packages/src/symfony/console/command/dump_completion_command.rs @@ -10,7 +10,7 @@ use crate::symfony::console::input::input_interface::InputInterface; use crate::symfony::console::input::input_option::InputOption; use crate::symfony::console::output::output_interface::{self, OutputInterface}; use crate::symfony::process::process::Process; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PhpMixed, impl_php_class}; use std::ops::{Deref, DerefMut}; /// __DIR__.'/../Resources/completion.bash', embedded at compile time (this port ships as a @@ -23,6 +23,11 @@ pub struct DumpCompletionCommand { inner: CommandData, } +impl_php_class!( + DumpCompletionCommand, + r"Symfony\Component\Console\Command\DumpCompletionCommand" +); + impl Deref for DumpCompletionCommand { type Target = CommandData; @@ -288,8 +293,5 @@ impl Command for DumpCompletionCommand { self.complete_impl(input, suggestions) } - crate::delegate_command_trait_impls_to_inner!( - inner, - "Symfony\\Component\\Console\\Command\\DumpCompletionCommand" - ); + crate::delegate_command_trait_impls_to_inner!(inner); } diff --git a/crates/shirabe-external-packages/src/symfony/console/command/help_command.rs b/crates/shirabe-external-packages/src/symfony/console/command/help_command.rs index 83ed4f5b..d7a88422 100644 --- a/crates/shirabe-external-packages/src/symfony/console/command/help_command.rs +++ b/crates/shirabe-external-packages/src/symfony/console/command/help_command.rs @@ -13,7 +13,7 @@ use crate::symfony::console::input::input_definition::DefinitionItem; use crate::symfony::console::input::input_interface::InputInterface; use crate::symfony::console::input::input_option::InputOption; use crate::symfony::console::output::output_interface::OutputInterface; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PhpMixed, impl_php_class}; use std::ops::{Deref, DerefMut}; /// HelpCommand displays the help for a given command. @@ -23,6 +23,11 @@ pub struct HelpCommand { command: std::cell::RefCell>>>, } +impl_php_class!( + HelpCommand, + r"Symfony\Component\Console\Command\HelpCommand" +); + impl Deref for HelpCommand { type Target = CommandData; @@ -164,8 +169,5 @@ impl Command for HelpCommand { Ok(()) } - crate::delegate_command_trait_impls_to_inner!( - inner, - "Symfony\\Component\\Console\\Command\\HelpCommand" - ); + crate::delegate_command_trait_impls_to_inner!(inner); } diff --git a/crates/shirabe-external-packages/src/symfony/console/command/list_command.rs b/crates/shirabe-external-packages/src/symfony/console/command/list_command.rs index 79673000..1c1a2aab 100644 --- a/crates/shirabe-external-packages/src/symfony/console/command/list_command.rs +++ b/crates/shirabe-external-packages/src/symfony/console/command/list_command.rs @@ -13,7 +13,7 @@ use crate::symfony::console::input::input_definition::DefinitionItem; use crate::symfony::console::input::input_interface::InputInterface; use crate::symfony::console::input::input_option::InputOption; use crate::symfony::console::output::output_interface::OutputInterface; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PhpMixed, impl_php_class}; use std::ops::{Deref, DerefMut}; /// ListCommand displays the list of all available commands for the application. @@ -22,6 +22,11 @@ pub struct ListCommand { inner: CommandData, } +impl_php_class!( + ListCommand, + r"Symfony\Component\Console\Command\ListCommand" +); + impl Deref for ListCommand { type Target = CommandData; @@ -165,8 +170,5 @@ impl Command for ListCommand { Ok(()) } - crate::delegate_command_trait_impls_to_inner!( - inner, - "Symfony\\Component\\Console\\Command\\ListCommand" - ); + crate::delegate_command_trait_impls_to_inner!(inner); } -- cgit v1.3.1