From 791ef1cd465597ff43dab4216c4b00e9e4160da8 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Thu, 6 Aug 2026 06:25:10 +0900 Subject: refactor(php-shim): split in_array into strict and loose variants --- crates/shirabe/src/util/auth_helper.rs | 60 +++++++++++-------------- crates/shirabe/src/util/git.rs | 29 ++++++------ crates/shirabe/src/util/github.rs | 8 +--- crates/shirabe/src/util/gitlab.rs | 14 ++---- crates/shirabe/src/util/http/curl_downloader.rs | 50 +++++++++------------ crates/shirabe/src/util/platform.rs | 11 +++-- crates/shirabe/src/util/process_executor.rs | 20 ++++----- crates/shirabe/src/util/url.rs | 18 +++----- 8 files changed, 85 insertions(+), 125 deletions(-) (limited to 'crates/shirabe/src/util') diff --git a/crates/shirabe/src/util/auth_helper.rs b/crates/shirabe/src/util/auth_helper.rs index 0e02e764..80852bb2 100644 --- a/crates/shirabe/src/util/auth_helper.rs +++ b/crates/shirabe/src/util/auth_helper.rs @@ -12,8 +12,8 @@ use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::Preg; use shirabe_php_shim::{ PHP_URL_HOST, PHP_URL_PATH, PHP_URL_SCHEME, PhpMixed, RuntimeException, base64_encode, explode, - in_array, is_array, is_string, json_decode, parse_url, php_regex, str_replace, strpos, - strtolower, substr, trim, + in_array_loose, in_array_strict, is_array, is_string, json_decode, parse_url, php_regex, + str_replace, strpos, strtolower, substr, trim, }; #[derive(Debug)] @@ -71,13 +71,12 @@ impl AuthHelper { 0, Some(1), )); - if in_array( - PhpMixed::String(input.clone()), - &PhpMixed::List(vec![ + if in_array_loose( + input.clone(), + &[ PhpMixed::String("y".to_string()), PhpMixed::String("n".to_string()), - ]), - false, + ], ) { return Ok(PhpMixed::String(input)); } @@ -250,14 +249,13 @@ impl AuthHelper { .and_then(|a| a.get("password")) .and_then(|v| v.clone()) .unwrap_or_default(); - if in_array( - PhpMixed::String(password), - &PhpMixed::List(vec![ + if in_array_strict( + password, + &[ PhpMixed::String("gitlab-ci-token".to_string()), PhpMixed::String("private-token".to_string()), PhpMixed::String("oauth2".to_string()), - ]), - true, + ], ) { return Err(TransportException::new( format!("Invalid credentials for '{}', aborting.", url), @@ -522,26 +520,21 @@ impl AuthHelper { authentication_display_message = Some("Using GitHub token authentication".to_string()); } - } else if in_array( - PhpMixed::String(password.clone()), - &PhpMixed::List(vec![ + } else if in_array_strict( + password.clone(), + &[ PhpMixed::String("oauth2".to_string()), PhpMixed::String("private-token".to_string()), PhpMixed::String("gitlab-ci-token".to_string()), - ]), - true, - ) && in_array( - PhpMixed::String(origin.to_string()), - &PhpMixed::List({ - let gitlab_domains = self.config.borrow_mut().get("gitlab-domains"); - match &gitlab_domains { - PhpMixed::List(l) => l.clone(), - PhpMixed::Array(a) => a.values().cloned().collect(), - _ => vec![], - } - }), - true, - ) { + ], + ) && in_array_strict(origin.to_string(), &{ + let gitlab_domains = self.config.borrow_mut().get("gitlab-domains"); + match &gitlab_domains { + PhpMixed::List(l) => l.clone(), + PhpMixed::Array(a) => a.values().cloned().collect(), + _ => vec![], + } + }) { if password == "oauth2" { headers.push(PhpMixed::String(format!( "Authorization: Bearer {}", @@ -600,13 +593,12 @@ impl AuthHelper { .insert(origin.to_string(), display_message.clone()); } } - } else if in_array( - PhpMixed::String(origin.to_string()), - &PhpMixed::List(vec![ + } else if in_array_strict( + origin.to_string(), + &[ PhpMixed::String("api.bitbucket.org".to_string()), PhpMixed::String("api.github.com".to_string()), - ]), - true, + ], ) { return self.add_authentication_options(options, &str_replace("api.", "", origin), url); } diff --git a/crates/shirabe/src/util/git.rs b/crates/shirabe/src/util/git.rs index cbda0d43..0ee69926 100644 --- a/crates/shirabe/src/util/git.rs +++ b/crates/shirabe/src/util/git.rs @@ -17,8 +17,9 @@ use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; use shirabe_php_shim::{ InvalidArgumentException, PHP_EOL, PhpMixed, RuntimeException, array_map, clearstatcache, - explode, implode, in_array, is_dir, php_regex, preg_quote, rawurldecode, rawurlencode, - str_contains, str_ends_with, str_replace_array, strlen, strpos, substr, trim, version_compare, + explode, implode, in_array_loose, in_array_strict, is_dir, php_regex, preg_quote, rawurldecode, + rawurlencode, str_contains, str_ends_with, str_replace_array, strlen, strpos, substr, trim, + version_compare, }; use std::sync::Mutex; @@ -329,15 +330,12 @@ impl Git { Self::get_github_domains_regex(&self.config.borrow()) ), url, - ) && !in_array( - PhpMixed::String("ssh".to_string()), - &PhpMixed::List( - protocols_list - .iter() - .map(|s| PhpMixed::String(s.clone())) - .collect(), - ), - true, + ) && !in_array_strict( + "ssh".to_string(), + &protocols_list + .iter() + .map(|s| PhpMixed::String(s.clone())) + .collect::>(), ); let mut auth: Option>> = None; @@ -1346,16 +1344,15 @@ impl Git { let mut masked_credentials: Vec = vec![]; for credential in credentials { - if in_array( - PhpMixed::String(credential.clone()), - &PhpMixed::List(vec![ + if in_array_loose( + credential.clone(), + &[ PhpMixed::String("private-token".to_string()), PhpMixed::String("x-token-auth".to_string()), PhpMixed::String("oauth2".to_string()), PhpMixed::String("gitlab-ci-token".to_string()), PhpMixed::String("x-oauth-basic".to_string()), - ]), - false, + ], ) { masked_credentials.push(credential.clone()); } else if strlen(credential) > 6 { diff --git a/crates/shirabe/src/util/github.rs b/crates/shirabe/src/util/github.rs index 0afbb684..01a5dd1f 100644 --- a/crates/shirabe/src/util/github.rs +++ b/crates/shirabe/src/util/github.rs @@ -9,7 +9,7 @@ use crate::util::HttpDownloader; use crate::util::ProcessExecutor; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; -use shirabe_php_shim::{PhpMixed, date, in_array, php_regex, stripos, strtolower}; +use shirabe_php_shim::{PhpMixed, date, in_array_loose, php_regex, stripos, strtolower}; #[derive(Debug)] pub struct GitHub { @@ -52,11 +52,7 @@ impl GitHub { pub fn authorize_oauth(&mut self, origin_url: &str) -> bool { let github_domains = self.config.borrow_mut().get("github-domains"); - if !in_array( - PhpMixed::String(origin_url.to_string()), - &github_domains, - false, - ) { + if !in_array_loose(origin_url.to_string(), github_domains.values()) { return false; } diff --git a/crates/shirabe/src/util/gitlab.rs b/crates/shirabe/src/util/gitlab.rs index 2b23cdbf..c14ad46a 100644 --- a/crates/shirabe/src/util/gitlab.rs +++ b/crates/shirabe/src/util/gitlab.rs @@ -11,7 +11,7 @@ use crate::util::ProcessExecutor; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::Preg; use shirabe_php_shim::{ - PhpMixed, RuntimeException, http_build_query, in_array, json_decode, php_regex, time, + PhpMixed, RuntimeException, http_build_query, in_array_strict, json_decode, php_regex, time, }; #[derive(Debug)] @@ -55,15 +55,9 @@ impl GitLab { let bc_origin_url = Preg::replace(php_regex!("{:\\d+}"), "", origin_url); let gitlab_domains = self.config.borrow_mut().get("gitlab-domains"); - if !in_array( - PhpMixed::String(origin_url.to_string()), - &gitlab_domains, - true, - ) && !in_array( - PhpMixed::String(bc_origin_url.clone()), - &gitlab_domains, - true, - ) { + if !in_array_strict(origin_url.to_string(), gitlab_domains.values()) + && !in_array_strict(bc_origin_url.clone(), gitlab_domains.values()) + { return false; } diff --git a/crates/shirabe/src/util/http/curl_downloader.rs b/crates/shirabe/src/util/http/curl_downloader.rs index 2d2259b9..64ff42a6 100644 --- a/crates/shirabe/src/util/http/curl_downloader.rs +++ b/crates/shirabe/src/util/http/curl_downloader.rs @@ -33,7 +33,8 @@ use crate::util::{AuthHelper, PromptAuthResult, StoreAuth}; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::Preg; use shirabe_php_shim::{ - PhpMixed, in_array, parse_url, php_regex, preg_quote, rename, strpos, substr, unlink_silent, + PhpMixed, in_array_loose, in_array_strict, parse_url, php_regex, preg_quote, rename, strpos, + substr, unlink_silent, }; use std::sync::atomic::{AtomicBool, Ordering}; @@ -412,15 +413,12 @@ impl CurlDownloader { .and_then(|v| v.as_int()) .unwrap_or(0); if Self::method_is_get(options) - && in_array( - PhpMixed::Int(status_code), - &PhpMixed::List( - [423, 425, 500, 502, 503, 504, 507, 510] - .iter() - .map(|c| PhpMixed::Int(*c)) - .collect(), - ), - true, + && in_array_strict( + status_code, + &[423, 425, 500, 502, 503, 504, 507, 510] + .iter() + .map(|c| PhpMixed::Int(*c)) + .collect::>(), ) && retries < self.max_retries { @@ -717,10 +715,9 @@ impl CurlDownloader { .and_then(|b| b.as_int()) .unwrap_or(0); - if in_array( - PhpMixed::Int(response.inner.get_status_code()), - &PhpMixed::List(vec![PhpMixed::Int(401), PhpMixed::Int(403)]), - false, + if in_array_loose( + response.inner.get_status_code(), + &[PhpMixed::Int(401), PhpMixed::Int(403)], ) && retry_auth_failure { let status_message = response.inner.get_status_message(); @@ -767,11 +764,7 @@ impl CurlDownloader { _ => Vec::new(), }; if response.inner.get_status_code() == 404 - && in_array( - PhpMixed::String(origin.to_string()), - &PhpMixed::List(gitlab_domains_list), - true, - ) + && in_array_strict(origin.to_string(), &gitlab_domains_list) && strpos(url, "archive.zip").is_some() { needs_auth_retry = Some("GitLab requires authentication and it was not provided"); @@ -827,19 +820,16 @@ impl CurlDownloader { } let mut details = String::new(); - if in_array( - PhpMixed::String( - response - .inner - .get_header("content-type") - .unwrap_or_default() - .to_lowercase(), - ), - &PhpMixed::List(vec![ + if in_array_strict( + response + .inner + .get_header("content-type") + .unwrap_or_default() + .to_lowercase(), + &[ PhpMixed::String("application/json".to_string()), PhpMixed::String("application/json; charset=utf-8".to_string()), - ]), - true, + ], ) { let body = response.inner.get_body().unwrap_or(""); details = format!( diff --git a/crates/shirabe/src/util/platform.rs b/crates/shirabe/src/util/platform.rs index 7b1c8f61..9ebf4f4f 100644 --- a/crates/shirabe/src/util/platform.rs +++ b/crates/shirabe/src/util/platform.rs @@ -5,7 +5,7 @@ use crate::util::Silencer; use shirabe_external_packages::composer::pcre::Preg; use shirabe_php_shim::{ PHP_ENV, PHP_SERVER, PhpMixed, PhpResource, RuntimeException, defined, file_exists, - file_get_contents, fstat, function_exists, getcwd, getenv, in_array, ini_get, is_array, + file_get_contents, fstat, function_exists, getcwd, getenv, in_array_strict, ini_get, is_array, is_readable, mb_strlen, php_os_family, php_regex, posix_geteuid, posix_getpwuid, posix_getuid, posix_isatty, putenv, putenv_clear, realpath, stream_isatty, stripos, strlen, strtoupper, substr, usleep, @@ -280,13 +280,12 @@ impl Platform { // detect msysgit/mingw and assume this is a tty because detection // does not work correctly, see https://github.com/composer/composer/issues/9690 - if in_array( - PhpMixed::String(strtoupper(&Self::get_env("MSYSTEM").unwrap_or_default())), - &PhpMixed::List(vec![ + if in_array_strict( + strtoupper(&Self::get_env("MSYSTEM").unwrap_or_default()), + &[ PhpMixed::String("MINGW32".to_string()), PhpMixed::String("MINGW64".to_string()), - ]), - true, + ], ) { return true; } diff --git a/crates/shirabe/src/util/process_executor.rs b/crates/shirabe/src/util/process_executor.rs index c24e2f83..463c5d19 100644 --- a/crates/shirabe/src/util/process_executor.rs +++ b/crates/shirabe/src/util/process_executor.rs @@ -15,8 +15,9 @@ use shirabe_external_packages::symfony::process::exception::ProcessSignaledExcep use shirabe_external_packages::symfony::process::exception::RuntimeException as SymfonyProcessRuntimeException; use shirabe_php_shim::{ LogicException, PHP_EOL, PhpMixed, RuntimeException, array_intersect, array_map, - escapeshellarg, explode, implode, in_array, is_array, is_dir, is_numeric, is_string, php_regex, - rtrim, str_replace, strcspn, strlen, strpbrk, strtolower, strtr_array, substr_replace, trim, + escapeshellarg, explode, implode, in_array_strict, is_array, is_dir, is_numeric, is_string, + php_regex, rtrim, str_replace, strcspn, strlen, strpbrk, strtolower, strtr_array, + substr_replace, trim, }; use std::sync::{LazyLock, Mutex}; @@ -974,15 +975,12 @@ impl ProcessExecutor { /// Resolves executable paths on Windows fn get_executable(name: &str) -> String { - if in_array( - PhpMixed::String(strtolower(name)), - &PhpMixed::List( - Self::BUILTIN_CMD_COMMANDS - .iter() - .map(|s| PhpMixed::String(s.to_string())) - .collect(), - ), - true, + if in_array_strict( + strtolower(name), + &Self::BUILTIN_CMD_COMMANDS + .iter() + .map(|s| PhpMixed::String(s.to_string())) + .collect::>(), ) { return name.to_string(); } diff --git a/crates/shirabe/src/util/url.rs b/crates/shirabe/src/util/url.rs index 621f6fab..b1974b0b 100644 --- a/crates/shirabe/src/util/url.rs +++ b/crates/shirabe/src/util/url.rs @@ -4,7 +4,9 @@ use crate::config::Config; use crate::util::GitHub; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; -use shirabe_php_shim::{PHP_URL_HOST, PHP_URL_PORT, PhpMixed, in_array, parse_url, php_regex}; +use shirabe_php_shim::{ + PHP_URL_HOST, PHP_URL_PORT, PhpMixed, in_array_strict, parse_url, php_regex, +}; pub struct Url; @@ -93,17 +95,13 @@ impl Url { r#ref ); } - } else if in_array( - PhpMixed::String(host.clone()), - &config.get("github-domains"), - true, - ) { + } else if in_array_strict(host.clone(), config.get("github-domains").values()) { url = Preg::replace( php_regex!(r"{(/repos/[^/]+/[^/]+/(zip|tar)ball)(?:/.+)?$}i"), &format!("$1/{}", r#ref), &url, ); - } else if in_array(PhpMixed::String(host), &config.get("gitlab-domains"), true) { + } else if in_array_strict(host, config.get("gitlab-domains").values()) { url = Preg::replace( php_regex!( r"{(/api/v[34]/projects/[^/]+/repository/archive\.(?:zip|tar\.gz|tar\.bz2|tar)\?sha=).+$}i" @@ -146,11 +144,7 @@ impl Url { // Gitlab can be installed in a non-root context (i.e. gitlab.com/foo). When downloading archives the originUrl // is the host without the path, so we look for the registered gitlab-domains matching the host here if !origin.contains('/') - && !in_array( - PhpMixed::String(origin.clone()), - &config.get("gitlab-domains"), - true, - ) + && !in_array_strict(origin.clone(), config.get("gitlab-domains").values()) { let gitlab_domains: Vec = match config.get("gitlab-domains") { PhpMixed::List(list) => list -- cgit v1.3.1