Skip to content

Commit aa11d97

Browse files
committed
fix: return false from ArrayHelper::dotHas() for a wildcard over an empty array
1 parent f0648c6 commit aa11d97

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎system/Helpers/Array/ArrayHelper.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ private static function hasByDotPath(array|object $array, array $indexes): bool
171171
$currentIndex = array_shift($indexes);
172172

173173
if ($currentIndex === '*') {
174-
foreach (self::entries($array) as $item) {
174+
$entries = self::entries($array);
175+
176+
if ($entries === []) {
177+
return false;
178+
}
179+
180+
foreach ($entries as $item) {
175181
if (! self::isNavigable($item) || ! self::hasByDotPath($item, $indexes)) {
176182
return false;
177183
}

‎tests/system/Helpers/Array/ArrayHelperDotHasTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,7 @@ public function testDotHasWithWildCard(): void
7373
$this->assertTrue(ArrayHelper::dotHas('contacts.*.1.age', $this->array));
7474
$this->assertTrue(ArrayHelper::dotHas('contacts.*.0.name', $this->array));
7575
$this->assertFalse(ArrayHelper::dotHas('contacts.*.1.name', $this->array));
76+
$this->assertFalse(ArrayHelper::dotHas('*.name', []));
77+
$this->assertFalse(ArrayHelper::dotHas('contacts.friends.*.name', ['contacts' => ['friends' => []]]));
7678
}
7779
}

0 commit comments

Comments
 (0)