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 the compiled runtime to evaluate sub-expressions when the left side is null.
* Fixed the ordering operators to return null instead of false for non-numbers.
* Fixed aggregate and sort functions to error on an invalid first or only element.
* Improved the performance of type checks on large arrays.

## 2.9.3 - 2026-11-16

Expand Down
16 changes: 12 additions & 4 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public static function type($arg)
if (empty($arg)) {
return 'array';
}
reset($arg);
return key($arg) === 0 ? 'array' : 'object';
return self::firstKey($arg) === 0 ? 'array' : 'object';
} elseif ($arg instanceof \stdClass) {
return 'object';
} elseif ($arg instanceof \Closure) {
Expand Down Expand Up @@ -83,7 +82,7 @@ public static function type($arg)
public static function isObject($value)
{
if (is_array($value)) {
return !$value || array_keys($value)[0] !== 0;
return !$value || self::firstKey($value) !== 0;
}

// Handle array-like values. Must be empty or offset 0 does not exist
Expand All @@ -102,7 +101,7 @@ public static function isObject($value)
public static function isArray($value)
{
if (is_array($value)) {
return !$value || array_keys($value)[0] === 0;
return !$value || self::firstKey($value) === 0;
}

// Handle array-like values. Must be empty or offset 0 exists.
Expand All @@ -111,6 +110,15 @@ public static function isArray($value)
: false;
}

private static function firstKey(array $array)
{
foreach ($array as $key => $value) {
return $key;
}

return null;
}

/**
* JSON-semantic equality: one number type, structural comparison for arrays
* and objects, and no object key-order sensitivity.
Expand Down
13 changes: 13 additions & 0 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static function typeProvider(): array
[[], 'array'],
[[1, 2], 'array'],
[['a' => 1], 'object'],
[['a' => 1, 0 => 2], 'object'],
[new \stdClass(), 'object'],
[function () {}, 'expression'],
[new \ArrayObject(), 'array'],
Expand Down Expand Up @@ -80,6 +81,7 @@ public static function isArrayProvider(): array
[[], true],
[[1, 2], true],
[['a' => 1], false],
[['a' => 1, 0 => 2], false],
[new _TestClass(), false],
[new \ArrayObject(['a' => 'b']), false],
[new \ArrayObject([1]), true],
Expand All @@ -101,6 +103,7 @@ public static function isObjectProvider(): array
[[], true],
[[1, 2], false],
[['a' => 1], true],
[['a' => 1, 0 => 2], true],
[new _TestClass(), false],
[new \ArrayObject(['a' => 'b']), true],
[new \ArrayObject([1]), false],
Expand All @@ -116,6 +119,16 @@ public function testChecksIfObject($given, $result): void
$this->assertSame($result, Utils::isObject($given));
}

public function testTypeChecksIgnoreInternalPointer(): void
{
$list = [1, 2];
end($list);

$this->assertSame('array', Utils::type($list));
$this->assertTrue(Utils::isArray($list));
$this->assertFalse(Utils::isObject($list));
}

public static function isTruthyProvider(): array
{
return [
Expand Down