Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Fixed functions, projections and equality to read array-like objects by their elements.
* Fixed list functions and flatten to accept array-like objects.
* Fixed empty array-like objects to be falsy.
* Fixed length() and reverse() to read objects with __toString() as strings.
* Changed array-like objects that are not Traversable to error when their elements are listed.

## 2.10.0 - 2026-11-17
Expand Down
10 changes: 4 additions & 6 deletions src/FnDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ private function fn_length(array $args)
{
$this->validateArity('length', count($args), 1);
$this->validateType('length:0', $args[0], ['string', 'array', 'object']);
return is_string($args[0])
? mb_strlen($args[0], 'UTF-8')
return Utils::type($args[0]) === 'string'
? mb_strlen((string) $args[0], 'UTF-8')
: count($args[0] instanceof \Countable ? $args[0] : (array) $args[0]);
}

Expand Down Expand Up @@ -185,11 +185,9 @@ private function fn_reverse(array $args)
$this->validate('reverse', $args, [['array', 'string']]);
if (is_array($args[0])) {
return array_reverse($args[0]);
} elseif (is_string($args[0])) {
return implode('', array_reverse(mb_str_split($args[0], 1, 'UTF-8')));
} else {
throw new \RuntimeException('Cannot reverse provided argument');
}

return implode('', array_reverse(mb_str_split((string) $args[0], 1, 'UTF-8')));
}

private function fn_sum(array $args)
Expand Down
9 changes: 9 additions & 0 deletions tests/FnDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ public function testMaxComparesStringableObjectsByCodePoint(): void
$this->assertSame($low, $fn('min', [[$low, $high]]));
}

public function testLengthAndReverseReadStringableObjectsAsStrings(): void
{
$fn = new FnDispatcher();
$value = new _TestComparableString('héllo');

$this->assertSame(5, $fn('length', [$value]));
$this->assertSame('olléh', $fn('reverse', [$value]));
}

public function testSortComparesNumbersNumerically(): void
{
$fn = FnDispatcher::getInstance();
Expand Down