diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-20 00:31:45 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-20 00:31:49 +0900 |
| commit | cac18ef73a39b4ac41fa4d6ccb753804d4c42cb7 (patch) | |
| tree | 968f438437042e8092752aa521c2d7e6ae28307e /crates | |
| parent | 7c5162e7c2f9d466e96248b7482aa98350244e8b (diff) | |
| download | php-shirabe-cac18ef73a39b4ac41fa4d6ccb753804d4c42cb7.tar.gz php-shirabe-cac18ef73a39b4ac41fa4d6ccb753804d4c42cb7.tar.zst php-shirabe-cac18ef73a39b4ac41fa4d6ccb753804d4c42cb7.zip | |
feat(php-shim): implement count()
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/shirabe-php-shim/src/lib.rs | 20 | ||||
| -rw-r--r-- | crates/shirabe/src/command/package_discovery_trait.rs | 25 | ||||
| -rw-r--r-- | crates/shirabe/src/console/application.rs | 2 | ||||
| -rw-r--r-- | crates/shirabe/src/downloader/vcs_downloader.rs | 35 | ||||
| -rw-r--r-- | crates/shirabe/src/json/json_manipulator.rs | 2 | ||||
| -rw-r--r-- | crates/shirabe/src/repository/vcs/github_driver.rs | 14 | ||||
| -rw-r--r-- | crates/shirabe/src/util/http/curl_downloader.rs | 17 | ||||
| -rw-r--r-- | crates/shirabe/src/util/perforce.rs | 8 |
8 files changed, 33 insertions, 90 deletions
diff --git a/crates/shirabe-php-shim/src/lib.rs b/crates/shirabe-php-shim/src/lib.rs index 5821819..d643260 100644 --- a/crates/shirabe-php-shim/src/lib.rs +++ b/crates/shirabe-php-shim/src/lib.rs @@ -2137,8 +2137,20 @@ pub fn trim(s: &str, chars: Option<&str>) -> String { s.trim_matches(|c| mask.contains(&c)).to_string() } -pub fn count(_value: &PhpMixed) -> i64 { - todo!() +pub fn count(value: &PhpMixed) -> usize { + match value { + PhpMixed::List(items) => items.len(), + PhpMixed::Array(entries) => entries.len(), + PhpMixed::Object(object) => object.count(), + // PHP 8 throws a `TypeError` for non-countable arguments. + PhpMixed::Null + | PhpMixed::Bool(_) + | PhpMixed::Int(_) + | PhpMixed::Float(_) + | PhpMixed::String(_) => { + panic!("count(): Argument #1 ($value) must be of type Countable|array") + } + } } pub fn array_shift<T>(_array: &mut Vec<T>) -> Option<T> { @@ -2800,6 +2812,10 @@ impl ArrayObject { pub fn to_array(&self) -> IndexMap<String, Box<PhpMixed>> { self.data.clone() } + + pub fn count(&self) -> usize { + self.data.len() + } } #[derive(Debug)] diff --git a/crates/shirabe/src/command/package_discovery_trait.rs b/crates/shirabe/src/command/package_discovery_trait.rs index c48b4d5..3263444 100644 --- a/crates/shirabe/src/command/package_discovery_trait.rs +++ b/crates/shirabe/src/command/package_discovery_trait.rs @@ -268,10 +268,7 @@ pub trait PackageDiscoveryTrait { None, )?; - if count(&PhpMixed::List( - matches.iter().map(|_| Box::new(PhpMixed::Null)).collect(), - )) > 0 - { + if matches.len() > 0 { // Remove existing packages from search results. matches.retain(|found_package| { !in_array( @@ -298,10 +295,7 @@ pub trait PackageDiscoveryTrait { if !exact_match { let providers: IndexMap<String, crate::repository::ProviderInfo> = self.get_repos().get_providers(package.clone())?; - if count(&PhpMixed::List( - providers.iter().map(|_| Box::new(PhpMixed::Null)).collect(), - )) > 0 - { + if providers.len() > 0 { // PHP: array_unshift($matches, ['name' => $package, 'description' => '']); matches.insert( 0, @@ -536,10 +530,7 @@ pub trait PackageDiscoveryTrait { // Check if it is a virtual package provided by others let providers = repo_set.borrow().get_providers(name)?; - if count(&PhpMixed::List( - providers.iter().map(|_| Box::new(PhpMixed::Null)).collect(), - )) > 0 - { + if providers.len() > 0 { let mut constraint = "*".to_string(); if input.borrow().is_interactive() { let providers_count = providers.len(); @@ -702,10 +693,7 @@ pub trait PackageDiscoveryTrait { // Check for similar names/typos let similar = self.find_similar(name)?; - if count(&PhpMixed::List( - similar.iter().map(|_| Box::new(PhpMixed::Null)).collect(), - )) > 0 - { + if similar.len() > 0 { if in_array( PhpMixed::String(name.to_string()), &PhpMixed::List( @@ -925,10 +913,7 @@ pub trait PackageDiscoveryTrait { } } - if count(&PhpMixed::List( - details.iter().map(|_| Box::new(PhpMixed::Null)).collect(), - )) == 0 - { + if details.is_empty() { return Ok(String::new()); } diff --git a/crates/shirabe/src/console/application.rs b/crates/shirabe/src/console/application.rs index 146767d..4e31577 100644 --- a/crates/shirabe/src/console/application.rs +++ b/crates/shirabe/src/console/application.rs @@ -1116,7 +1116,7 @@ impl Application { .map(|s| Box::new(PhpMixed::String(s.clone()))) .collect(), ); - if is_array(&avast_detect_pm) && count(&avast_detect_pm) != 0 { + if is_array(&avast_detect_pm) && avast_detect.len() != 0 { io.write_error3("<error>The following exception indicates a possible issue with the Avast Firewall</error>", true, io_interface::QUIET); io.write_error3( "<error>Check https://getcomposer.org/local-issuer for details</error>", diff --git a/crates/shirabe/src/downloader/vcs_downloader.rs b/crates/shirabe/src/downloader/vcs_downloader.rs index a967c3b..18a50da 100644 --- a/crates/shirabe/src/downloader/vcs_downloader.rs +++ b/crates/shirabe/src/downloader/vcs_downloader.rs @@ -165,24 +165,14 @@ pub trait VcsDownloader: true, io_interface::NORMAL, ); - } else if count(&PhpMixed::List( - urls.iter() - .map(|s| Box::new(PhpMixed::String(s.clone()))) - .collect(), - )) > 0 - { + } else if urls.len() > 0 { self.io().write_error3( " Failed, trying the next URL", true, io_interface::NORMAL, ); } - if count(&PhpMixed::List( - urls.iter() - .map(|s| Box::new(PhpMixed::String(s.clone()))) - .collect(), - )) == 0 - { + if urls.len() == 0 { return Err(e); } } @@ -279,24 +269,14 @@ pub trait VcsDownloader: true, io_interface::NORMAL, ); - } else if count(&PhpMixed::List( - urls.iter() - .map(|s| Box::new(PhpMixed::String(s.clone()))) - .collect(), - )) > 0 - { + } else if urls.len() > 0 { self.io().write_error3( " Failed, trying the next URL", true, io_interface::NORMAL, ); } - if count(&PhpMixed::List( - urls.iter() - .map(|s| Box::new(PhpMixed::String(s.clone()))) - .collect(), - )) == 0 - { + if urls.len() == 0 { return Err(e); } } @@ -358,12 +338,7 @@ pub trait VcsDownloader: true, io_interface::NORMAL, ); - } else if count(&PhpMixed::List( - urls.iter() - .map(|s| Box::new(PhpMixed::String(s.clone()))) - .collect(), - )) > 0 - { + } else if urls.len() > 0 { self.io().write_error3( " Failed, trying the next URL", true, diff --git a/crates/shirabe/src/json/json_manipulator.rs b/crates/shirabe/src/json/json_manipulator.rs index a3c0a16..3bc5725 100644 --- a/crates/shirabe/src/json/json_manipulator.rs +++ b/crates/shirabe/src/json/json_manipulator.rs @@ -556,7 +556,7 @@ impl JsonManipulator { .get(name) .map(|v| v.as_bool() == Some(false)) .unwrap_or(false) - && 1 == count(&PhpMixed::Array(repository_as_array.clone())) + && 1 == repository_as_array.len() { let idx: i64 = repository_index.parse().unwrap_or(0); if !self.remove_list_item("repositories", idx)? { diff --git a/crates/shirabe/src/repository/vcs/github_driver.rs b/crates/shirabe/src/repository/vcs/github_driver.rs index 864b4f3..696851e 100644 --- a/crates/shirabe/src/repository/vcs/github_driver.rs +++ b/crates/shirabe/src/repository/vcs/github_driver.rs @@ -1066,19 +1066,7 @@ impl GitHubDriver { let scopes_failed = array_diff(&scopes_needed, &scopes_issued); // non-authenticated requests get no scopesNeeded, so ask for credentials // authenticated requests which failed some scopes should ask for new credentials too - if headers.is_empty() - || count(&PhpMixed::List( - scopes_needed - .iter() - .map(|s| Box::new(PhpMixed::String(s.clone()))) - .collect(), - )) == 0 - || count(&PhpMixed::List( - scopes_failed - .iter() - .map(|s| Box::new(PhpMixed::String(s.clone()))) - .collect(), - )) > 0 + if headers.is_empty() || scopes_needed.is_empty() || scopes_failed.len() > 0 { git_hub_util.authorize_oauth_interactively( &self.inner.origin_url, diff --git a/crates/shirabe/src/util/http/curl_downloader.rs b/crates/shirabe/src/util/http/curl_downloader.rs index 98f874a..3a47d4f 100644 --- a/crates/shirabe/src/util/http/curl_downloader.rs +++ b/crates/shirabe/src/util/http/curl_downloader.rs @@ -736,22 +736,7 @@ impl CurlDownloader { } pub fn tick(&mut self) -> anyhow::Result<()> { - if count(&PhpMixed::Array( - self.jobs - .iter() - .map(|(k, v)| { - ( - k.to_string(), - Box::new(PhpMixed::Array( - v.iter() - .map(|(k2, v2)| (k2.clone(), Box::new(v2.clone()))) - .collect(), - )), - ) - }) - .collect(), - )) == 0 - { + if self.jobs.is_empty() { return Ok(()); } diff --git a/crates/shirabe/src/util/perforce.rs b/crates/shirabe/src/util/perforce.rs index 1f64dcc..937e305 100644 --- a/crates/shirabe/src/util/perforce.rs +++ b/crates/shirabe/src/util/perforce.rs @@ -682,13 +682,7 @@ impl Perforce { let res_array = explode(PHP_EOL, &result); for line in &res_array { let res_bits = explode(" ", line); - if count(&PhpMixed::List( - res_bits - .iter() - .map(|s| Box::new(PhpMixed::String(s.clone()))) - .collect(), - )) > 4 - { + if res_bits.len() > 4 { let branch = Preg::replace( r"/[^A-Za-z0-9 ]/", "", |
