Skip to content

Commit c721ebb

Browse files
committed
fix(Helpers): handle zero key and non-array intermediate values in ArrayHelper::dotKeyExists()
1 parent 59e5eea commit c721ebb

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

‎system/Helpers/Array/ArrayHelper.php‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,16 @@ public static function dotKeyExists(string $index, array $array): bool
146146
$currentArray = $array;
147147

148148
// Grab the current index
149-
while ($currentIndex = array_shift($indexes)) {
149+
while (($currentIndex = array_shift($indexes)) !== null) {
150150
if ($currentIndex === '*') {
151151
$currentIndex = array_shift($indexes);
152152

153+
if (! is_array($currentArray)) {
154+
return false;
155+
}
156+
153157
foreach ($currentArray as $item) {
154-
if (! array_key_exists($currentIndex, $item)) {
158+
if (! is_array($item) || ! array_key_exists($currentIndex, $item)) {
155159
return false;
156160
}
157161
}
@@ -166,7 +170,7 @@ public static function dotKeyExists(string $index, array $array): bool
166170
continue;
167171
}
168172

169-
if (! array_key_exists($currentIndex, $currentArray)) {
173+
if (! is_array($currentArray) || ! array_key_exists($currentIndex, $currentArray)) {
170174
return false;
171175
}
172176

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ public function testDotKeyExists(): void
4343
$this->assertTrue(ArrayHelper::dotKeyExists('contacts.friends', $this->array));
4444
$this->assertFalse(ArrayHelper::dotKeyExists('not.friends', $this->array));
4545
$this->assertTrue(ArrayHelper::dotKeyExists('contacts.friends.0.name', $this->array));
46+
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.0.nonexistent', $this->array));
4647
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.name', $this->array));
48+
$this->assertFalse(ArrayHelper::dotKeyExists('0', []));
49+
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.0.name.sub', $this->array));
50+
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.age.sub', $this->array));
4751
}
4852

4953
public function testDotKeyExistsWithEndingWildCard(): void
@@ -72,6 +76,9 @@ public function testDotKeyExistsWithWildCard(): void
7276
$this->assertTrue(ArrayHelper::dotKeyExists('contacts.*.0.age', $this->array));
7377
$this->assertTrue(ArrayHelper::dotKeyExists('contacts.*.1.age', $this->array));
7478
$this->assertTrue(ArrayHelper::dotKeyExists('contacts.*.0.name', $this->array));
79+
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.*.0.nonexistent', $this->array));
7580
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.*.1.name', $this->array));
81+
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.*.age.sub', $this->array));
82+
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.age.*.sub', $this->array));
7683
}
7784
}

0 commit comments

Comments
 (0)