From 70e463708b461efd61a611061cfee0539d28645a Mon Sep 17 00:00:00 2001 From: nsfisis Date: Thu, 6 Aug 2026 06:36:42 +0900 Subject: refactor: replace literal-list in_array_strict with matches! Call sites whose haystack was an inline array of literals (or a local built solely to feed one) had to wrap both sides in PhpMixed just to compare, allocating a String per element on every call. matches! does the same test against the underlying &str/i64/Option directly, so the PhpMixed round trip and its .to_string()/.clone()/.iter().map() conversions are gone. Sites whose haystack is a runtime value or a named constant array are left on in_array_strict: inlining a named constant would duplicate its contents at the call site. Co-Authored-By: Claude Opus 5 (1M context) --- .../shirabe/src/repository/composer_repository.rs | 12 ++----- .../src/repository/vcs/git_bitbucket_driver.rs | 6 ++-- crates/shirabe/src/repository/vcs/gitlab_driver.rs | 39 ++++++---------------- crates/shirabe/src/repository/vcs_repository.rs | 7 ++-- 4 files changed, 18 insertions(+), 46 deletions(-) (limited to 'crates/shirabe/src/repository') diff --git a/crates/shirabe/src/repository/composer_repository.rs b/crates/shirabe/src/repository/composer_repository.rs index 833be0af..59e1d520 100644 --- a/crates/shirabe/src/repository/composer_repository.rs +++ b/crates/shirabe/src/repository/composer_repository.rs @@ -40,8 +40,8 @@ use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; use shirabe_metadata_minifier::MetadataMinifier; use shirabe_php_shim::{ InvalidArgumentException, LogicException, PHP_EOL, PhpMixed, RuntimeException, - UnexpectedValueException, extension_loaded, hash, http_build_query, in_array_strict, - json_decode, parse_url_all, php_regex, realpath, strtolower, strtr, urlencode, var_export, + UnexpectedValueException, extension_loaded, hash, http_build_query, json_decode, parse_url_all, + php_regex, realpath, strtolower, strtr, urlencode, var_export, }; use shirabe_semver::CompilingMatcher; use shirabe_semver::constraint::AnyConstraint; @@ -1441,13 +1441,7 @@ impl ComposerRepository { if let Some(te) = e.downcast_ref::() { let status_code = te.get_status_code(); if self.lazy_providers_url.is_some() - && in_array_strict( - match status_code { - Some(c) => PhpMixed::Int(c), - None => PhpMixed::Null, - }, - &[PhpMixed::Int(404), PhpMixed::Int(499)], - ) + && matches!(status_code, Some(404 | 499)) { let mut p: IndexMap = IndexMap::new(); p.insert("packages".to_string(), PhpMixed::Array(IndexMap::new())); diff --git a/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs b/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs index 6b62d818..8fe93018 100644 --- a/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs +++ b/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs @@ -18,8 +18,8 @@ use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; use shirabe_php_shim::{ InvalidArgumentException, LogicException, PhpMixed, RuntimeException, array_key_exists, - array_search_mixed, extension_loaded, http_build_query_mixed, implode, in_array_strict, - is_array, php_regex, strpos, + array_search_mixed, extension_loaded, http_build_query_mixed, implode, is_array, php_regex, + strpos, }; #[derive(Debug)] @@ -697,7 +697,7 @@ impl GitBitbucketDriver { { let te = &e; let code = te.get_code(); - let in_set = in_array_strict(code, &[PhpMixed::Int(403), PhpMixed::Int(404)]); + let in_set = matches!(code, 403 | 404); if in_set || (401 == code && strpos(te.get_message(), "Could not authenticate against") diff --git a/crates/shirabe/src/repository/vcs/gitlab_driver.rs b/crates/shirabe/src/repository/vcs/gitlab_driver.rs index 8f49e8ef..5fe513f6 100644 --- a/crates/shirabe/src/repository/vcs/gitlab_driver.rs +++ b/crates/shirabe/src/repository/vcs/gitlab_driver.rs @@ -18,8 +18,8 @@ use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; use shirabe_php_shim::{ InvalidArgumentException, LogicException, PhpMixed, RuntimeException, array_search_mixed, - array_shift, ctype_alnum, empty, explode, extension_loaded, implode, in_array_loose, - in_array_strict, is_array, is_string, ord, php_regex, strpos, strtolower, + array_shift, ctype_alnum, empty, explode, extension_loaded, implode, in_array_loose, is_array, + is_string, ord, php_regex, strpos, strtolower, }; /// Driver for GitLab API, use the Git driver for local checkouts. @@ -115,13 +115,7 @@ impl GitLabDriver { .get(&CaptureKey::ByName("scheme".to_string())) .cloned() .unwrap_or_default(); - self.scheme = if in_array_strict( - scheme_match.clone(), - &[ - PhpMixed::String("https".to_string()), - PhpMixed::String("http".to_string()), - ], - ) { + self.scheme = if matches!(scheme_match.as_str(), "https" | "http") { scheme_match } else if self .inner @@ -158,14 +152,7 @@ impl GitLabDriver { .filter(|_| is_string(&protocol_value)) { // https treated as a synonym for http. - if !in_array_strict( - protocol.to_string(), - &[ - PhpMixed::String("git".to_string()), - PhpMixed::String("http".to_string()), - PhpMixed::String("https".to_string()), - ], - ) { + if !matches!(protocol, "git" | "http" | "https") { return Err(RuntimeException { message: "gitlab-protocol must be one of git, http.".to_string(), code: 0, @@ -601,18 +588,12 @@ impl GitLabDriver { let bytes: Vec = string.chars().collect(); for byte in &bytes { let character = byte.to_string(); - let final_character = if !ctype_alnum(&character) - && !in_array_strict( - character.clone(), - &[ - PhpMixed::String("-".to_string()), - PhpMixed::String("_".to_string()), - ], - ) { - format!("%{:02X}", ord(&character)) - } else { - character - }; + let final_character = + if !ctype_alnum(&character) && !matches!(character.as_str(), "-" | "_") { + format!("%{:02X}", ord(&character)) + } else { + character + }; encoded.push_str(&final_character); } diff --git a/crates/shirabe/src/repository/vcs_repository.rs b/crates/shirabe/src/repository/vcs_repository.rs index c8790f81..521d7aea 100644 --- a/crates/shirabe/src/repository/vcs_repository.rs +++ b/crates/shirabe/src/repository/vcs_repository.rs @@ -29,7 +29,7 @@ use crate::util::Url; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::Preg; use shirabe_php_shim::{ - InvalidArgumentException, PhpClass, PhpMixed, in_array_strict, php_regex, str_replace, strpos, + InvalidArgumentException, PhpClass, PhpMixed, php_regex, str_replace, strpos, }; use shirabe_semver::constraint::SimpleConstraint; @@ -1030,10 +1030,7 @@ impl VcsRepository { } fn should_rethrow_transport_exception(&self, e: &TransportException) -> bool { - in_array_strict( - e.get_code(), - &[PhpMixed::Int(401), PhpMixed::Int(403), PhpMixed::Int(429)], - ) || e.get_code() >= 500 + matches!(e.get_code(), 401 | 403 | 429) || e.get_code() >= 500 } } -- cgit v1.3.1