From f05e1a55cf0d44e9611e06a3d7b4fccdcb90ce7b Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 21 Jun 2026 17:42:32 +0900 Subject: refactor(php-shim): split filter_var into per-filter functions Replace the dispatch-on-constant filter_var() and filter_var_with_options() with dedicated filter_var_boolean/url/email/ip and filter_var_int_with_range, dropping the FILTER_VALIDATE_* constants and updating all call sites. Co-Authored-By: Claude Opus 4.8 (1M context) --- crates/shirabe/src/util/error_handler.rs | 8 ++++---- crates/shirabe/src/util/no_proxy_pattern.rs | 23 +++++------------------ crates/shirabe/src/util/remote_filesystem.rs | 15 ++++++--------- 3 files changed, 15 insertions(+), 31 deletions(-) (limited to 'crates/shirabe/src/util') diff --git a/crates/shirabe/src/util/error_handler.rs b/crates/shirabe/src/util/error_handler.rs index e0590d9..b9210fc 100644 --- a/crates/shirabe/src/util/error_handler.rs +++ b/crates/shirabe/src/util/error_handler.rs @@ -3,9 +3,9 @@ use crate::io::IOInterface; use crate::io::IOInterfaceImmutable; use shirabe_php_shim::{ - E_ALL, E_DEPRECATED, E_USER_DEPRECATED, E_USER_WARNING, E_WARNING, ErrorException, - FILTER_VALIDATE_BOOLEAN, PHP_EOL, PhpMixed, STDERR, debug_backtrace, error_reporting, - filter_var, ini_get, is_resource, set_error_handler, + E_ALL, E_DEPRECATED, E_USER_DEPRECATED, E_USER_WARNING, E_WARNING, ErrorException, PHP_EOL, + PhpMixed, STDERR, debug_backtrace, error_reporting, filter_var_boolean, ini_get, is_resource, + set_error_handler, }; use std::cell::{Cell, RefCell}; use std::rc::Rc; @@ -37,7 +37,7 @@ impl ErrorHandler { let mut message = message; let xdebug_scream = ini_get("xdebug.scream").unwrap_or_default(); - if filter_var(&xdebug_scream, FILTER_VALIDATE_BOOLEAN) { + if filter_var_boolean(&xdebug_scream) { message += "\n\nWarning: You have xdebug.scream enabled, the warning above may be\na legitimately suppressed error that you were not supposed to see."; } diff --git a/crates/shirabe/src/util/no_proxy_pattern.rs b/crates/shirabe/src/util/no_proxy_pattern.rs index 08cd92f..3715ea9 100644 --- a/crates/shirabe/src/util/no_proxy_pattern.rs +++ b/crates/shirabe/src/util/no_proxy_pattern.rs @@ -4,10 +4,9 @@ use anyhow::Result; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::Preg; use shirabe_php_shim::{ - FILTER_VALIDATE_INT, FILTER_VALIDATE_IP, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_SCHEME, PhpMixed, - RuntimeException, array_key_exists, chr, empty, explode, filter_var, filter_var_with_options, - inet_pton, ltrim, parse_url, str_pad, str_repeat, stripos, strlen, strpbrk, strpos, substr, - substr_count, unpack, + PHP_URL_HOST, PHP_URL_PORT, PHP_URL_SCHEME, PhpMixed, RuntimeException, array_key_exists, chr, + empty, explode, filter_var_int_with_range, filter_var_ip, inet_pton, ltrim, parse_url, str_pad, + str_repeat, stripos, strlen, strpbrk, strpos, substr, substr_count, unpack, }; /// Tests URLs against NO_PROXY patterns @@ -271,7 +270,7 @@ impl NoProxyPattern { } // See if this is an ip address - if !filter_var(&host, FILTER_VALIDATE_IP) { + if !filter_var_ip(&host) { return Ok(!modified); } @@ -483,18 +482,6 @@ impl NoProxyPattern { /// Wrapper around filter_var FILTER_VALIDATE_INT fn validate_int(&self, int: &str, min: i64, max: i64) -> bool { - let mut options: IndexMap = IndexMap::new(); - let mut inner: IndexMap = IndexMap::new(); - inner.insert("min_range".to_string(), PhpMixed::Int(min)); - inner.insert("max_range".to_string(), PhpMixed::Int(max)); - options.insert( - "options".to_string(), - PhpMixed::Array(inner.into_iter().collect()), - ); - - !matches!( - filter_var_with_options(int, FILTER_VALIDATE_INT, &options), - PhpMixed::Bool(false) - ) + filter_var_int_with_range(int, min, max) } } diff --git a/crates/shirabe/src/util/remote_filesystem.rs b/crates/shirabe/src/util/remote_filesystem.rs index bada2af..e36a174 100644 --- a/crates/shirabe/src/util/remote_filesystem.rs +++ b/crates/shirabe/src/util/remote_filesystem.rs @@ -4,12 +4,12 @@ use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; use shirabe_php_shim::{ - FILTER_VALIDATE_BOOLEAN, PHP_URL_HOST, PHP_URL_PATH, PHP_URL_SCHEME, PHP_VERSION_ID, PhpMixed, - RuntimeException, STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_FILE_SIZE_IS, STREAM_NOTIFY_PROGRESS, + PHP_URL_HOST, PHP_URL_PATH, PHP_URL_SCHEME, PHP_VERSION_ID, PhpMixed, RuntimeException, + STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_FILE_SIZE_IS, STREAM_NOTIFY_PROGRESS, array_replace_recursive, base64_encode, explode, extension_loaded, file_put_contents, - filter_var, gethostbyname, http_clear_last_response_headers, http_get_last_response_headers, - ini_get, json_decode, parse_url, preg_quote, sprintf, strpos, strtolower, strtr, substr, trim, - zlib_decode, + filter_var_boolean, gethostbyname, http_clear_last_response_headers, + http_get_last_response_headers, ini_get, json_decode, parse_url, preg_quote, sprintf, strpos, + strtolower, strtr, substr, trim, zlib_decode, }; use crate::config::Config; @@ -410,10 +410,7 @@ impl RemoteFilesystem { result = None; } if !error_message.is_empty() - && !filter_var( - &ini_get("allow_url_fopen").unwrap_or_default(), - FILTER_VALIDATE_BOOLEAN, - ) + && !filter_var_boolean(&ini_get("allow_url_fopen").unwrap_or_default()) { error_message = format!( "allow_url_fopen must be enabled in php.ini ({})", -- cgit v1.3.1