From cac18ef73a39b4ac41fa4d6ccb753804d4c42cb7 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 20 Jun 2026 00:31:45 +0900 Subject: feat(php-shim): implement count() Co-Authored-By: Claude Opus 4.8 (1M context) --- crates/shirabe-php-shim/src/lib.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'crates/shirabe-php-shim') 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(_array: &mut Vec) -> Option { @@ -2800,6 +2812,10 @@ impl ArrayObject { pub fn to_array(&self) -> IndexMap> { self.data.clone() } + + pub fn count(&self) -> usize { + self.data.len() + } } #[derive(Debug)] -- cgit v1.3.1