From 50a887bc53a32e88efab1c91fe696f3367e505ca Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Tue, 10 Nov 2026 23:44:08 +0700 Subject: [PATCH 1/5] fix(Helpers): handle zero key and non-array intermediate values in ArrayHelper::dotKeyExists() --- system/Helpers/Array/ArrayHelper.php | 10 +++++++--- .../Helpers/Array/ArrayHelperDotKeyExistsTest.php | 7 +++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/system/Helpers/Array/ArrayHelper.php b/system/Helpers/Array/ArrayHelper.php index b48efbfc4802..d3a34d4448dd 100644 --- a/system/Helpers/Array/ArrayHelper.php +++ b/system/Helpers/Array/ArrayHelper.php @@ -146,12 +146,16 @@ public static function dotKeyExists(string $index, array $array): bool $currentArray = $array; // Grab the current index - while ($currentIndex = array_shift($indexes)) { + while (($currentIndex = array_shift($indexes)) !== null) { if ($currentIndex === '*') { $currentIndex = array_shift($indexes); + if (! is_array($currentArray)) { + return false; + } + foreach ($currentArray as $item) { - if (! array_key_exists($currentIndex, $item)) { + if (! is_array($item) || ! array_key_exists($currentIndex, $item)) { return false; } } @@ -166,7 +170,7 @@ public static function dotKeyExists(string $index, array $array): bool continue; } - if (! array_key_exists($currentIndex, $currentArray)) { + if (! is_array($currentArray) || ! array_key_exists($currentIndex, $currentArray)) { return false; } diff --git a/tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php b/tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php index 432fafcf1a0c..4abd6f864cea 100644 --- a/tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php +++ b/tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php @@ -43,7 +43,11 @@ public function testDotKeyExists(): void $this->assertTrue(ArrayHelper::dotKeyExists('contacts.friends', $this->array)); $this->assertFalse(ArrayHelper::dotKeyExists('not.friends', $this->array)); $this->assertTrue(ArrayHelper::dotKeyExists('contacts.friends.0.name', $this->array)); + $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.0.nonexistent', $this->array)); $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.name', $this->array)); + $this->assertFalse(ArrayHelper::dotKeyExists('0', [])); + $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.0.name.sub', $this->array)); + $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.age.sub', $this->array)); } public function testDotKeyExistsWithEndingWildCard(): void @@ -72,6 +76,9 @@ public function testDotKeyExistsWithWildCard(): void $this->assertTrue(ArrayHelper::dotKeyExists('contacts.*.0.age', $this->array)); $this->assertTrue(ArrayHelper::dotKeyExists('contacts.*.1.age', $this->array)); $this->assertTrue(ArrayHelper::dotKeyExists('contacts.*.0.name', $this->array)); + $this->assertFalse(ArrayHelper::dotKeyExists('contacts.*.0.nonexistent', $this->array)); $this->assertFalse(ArrayHelper::dotKeyExists('contacts.*.1.name', $this->array)); + $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.*.age.sub', $this->array)); + $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.age.*.sub', $this->array)); } } From e61b831ffa31e97d705d8e2cc55e91fdd4bfe5c6 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Thu, 12 Nov 2026 00:43:12 +0700 Subject: [PATCH 2/5] test(Helpers): cover non-array wildcard items, zero keys, and add changelog --- docs/changelogs/v4.7.7.md | 1 + tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php | 3 +++ 2 files changed, 4 insertions(+) diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index d817d9932f94..bc85b5265f6d 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -27,6 +27,7 @@ Release Date: Unreleased - **Database:** Fixed a bug where `SQLite3\Connection::_foreignKeyData()` did not escape table identifiers in `PRAGMA foreign_key_list`, causing syntax errors on table names containing hyphens or schemas. - **Database:** Fixed a bug where `SQLite3\Table::dropIndexes()` used single-quoted string literals instead of escaped identifiers in `DROP INDEX IF EXISTS`, causing syntax errors on index names containing quotes or special characters. - **Helpers:** Fixed a bug where `excerpt()` threw a `TypeError` when `$phrase` was not found in `$text` or when `$phrase` was `null` and `$radius` was an odd integer. When `$phrase` is not found, `excerpt()` now falls back to extracting an excerpt from the beginning of `$text`. +- **Helpers:** Fixed a bug in `ArrayHelper::dotKeyExists()` where `'0'` path segments caused early loop termination and returned `true`, and non-array intermediate values threw a `TypeError`. - **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access. - **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names. - **Images:** Fixed a bug in `ImageMagickHandler::save()` where saving without an extension or with an unsupported extension threw an `ImagickException` instead of falling back to the image's format. diff --git a/tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php b/tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php index 4abd6f864cea..218683fe8a8c 100644 --- a/tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php +++ b/tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php @@ -46,6 +46,8 @@ public function testDotKeyExists(): void $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.0.nonexistent', $this->array)); $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.name', $this->array)); $this->assertFalse(ArrayHelper::dotKeyExists('0', [])); + $this->assertTrue(ArrayHelper::dotKeyExists('0', ['0' => 'value'])); + $this->assertTrue(ArrayHelper::dotKeyExists('0', ['0' => null])); $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.0.name.sub', $this->array)); $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.age.sub', $this->array)); } @@ -80,5 +82,6 @@ public function testDotKeyExistsWithWildCard(): void $this->assertFalse(ArrayHelper::dotKeyExists('contacts.*.1.name', $this->array)); $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.*.age.sub', $this->array)); $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.age.*.sub', $this->array)); + $this->assertFalse(ArrayHelper::dotKeyExists('*.name', [['name' => 'Fred'], 'not-an-array'])); } } From d21707bc4009fef0c17e1400a9996561f03f8373 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:21:55 +0700 Subject: [PATCH 3/5] refactor(Helpers): avoid assignment in while condition in dotKeyExists() --- system/Helpers/Array/ArrayHelper.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/system/Helpers/Array/ArrayHelper.php b/system/Helpers/Array/ArrayHelper.php index d3a34d4448dd..bd587daf8a6a 100644 --- a/system/Helpers/Array/ArrayHelper.php +++ b/system/Helpers/Array/ArrayHelper.php @@ -146,7 +146,9 @@ public static function dotKeyExists(string $index, array $array): bool $currentArray = $array; // Grab the current index - while (($currentIndex = array_shift($indexes)) !== null) { + while ($indexes !== []) { + $currentIndex = array_shift($indexes); + if ($currentIndex === '*') { $currentIndex = array_shift($indexes); From 4211f65860bffd7ba914691ac284d12140e5ebe8 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:06:14 +0700 Subject: [PATCH 4/5] fix(Helpers): check remaining segments per item after wildcard in dotKeyExists() --- docs/changelogs/v4.7.7.md | 2 +- system/Helpers/Array/ArrayHelper.php | 28 ++++++++++--------- .../Array/ArrayHelperDotKeyExistsTest.php | 26 +++++++++++++++++ 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index bc85b5265f6d..e5ce1e52355c 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -27,7 +27,7 @@ Release Date: Unreleased - **Database:** Fixed a bug where `SQLite3\Connection::_foreignKeyData()` did not escape table identifiers in `PRAGMA foreign_key_list`, causing syntax errors on table names containing hyphens or schemas. - **Database:** Fixed a bug where `SQLite3\Table::dropIndexes()` used single-quoted string literals instead of escaped identifiers in `DROP INDEX IF EXISTS`, causing syntax errors on index names containing quotes or special characters. - **Helpers:** Fixed a bug where `excerpt()` threw a `TypeError` when `$phrase` was not found in `$text` or when `$phrase` was `null` and `$radius` was an odd integer. When `$phrase` is not found, `excerpt()` now falls back to extracting an excerpt from the beginning of `$text`. -- **Helpers:** Fixed a bug in `ArrayHelper::dotKeyExists()` where `'0'` path segments caused early loop termination and returned `true`, and non-array intermediate values threw a `TypeError`. +- **Helpers:** Fixed bugs in `ArrayHelper::dotKeyExists()` where `'0'` path segments caused early loop termination, non-array intermediate values threw a `TypeError`, and paths with segments after a wildcard failed when the wildcard matched multiple elements. - **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access. - **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names. - **Images:** Fixed a bug in `ImageMagickHandler::save()` where saving without an extension or with an unsupported extension threw an `ImagickException` instead of falling back to the image's format. diff --git a/system/Helpers/Array/ArrayHelper.php b/system/Helpers/Array/ArrayHelper.php index bd587daf8a6a..5ff11caf01e0 100644 --- a/system/Helpers/Array/ArrayHelper.php +++ b/system/Helpers/Array/ArrayHelper.php @@ -143,33 +143,35 @@ public static function dotKeyExists(string $index, array $array): bool return false; } - $currentArray = $array; + return self::checkDotKeyExists($indexes, $array); + } + /** + * Recursively check if the key exists in the array with wildcards. + * + * @used-by dotKeyExists() + * + * @param list $indexes + * @param array $currentArray + */ + private static function checkDotKeyExists(array $indexes, array $currentArray): bool + { // Grab the current index while ($indexes !== []) { $currentIndex = array_shift($indexes); if ($currentIndex === '*') { - $currentIndex = array_shift($indexes); - - if (! is_array($currentArray)) { + if (! is_array($currentArray) || $currentArray === []) { return false; } foreach ($currentArray as $item) { - if (! is_array($item) || ! array_key_exists($currentIndex, $item)) { + if (! is_array($item) || ! self::checkDotKeyExists($indexes, $item)) { return false; } } - // If indexes is empty, all elements are checked. - if ($indexes === []) { - return true; - } - - $currentArray = self::dotSearch('*.' . $currentIndex, $currentArray); - - continue; + return true; } if (! is_array($currentArray) || ! array_key_exists($currentIndex, $currentArray)) { diff --git a/tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php b/tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php index 218683fe8a8c..c68919959031 100644 --- a/tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php +++ b/tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php @@ -83,5 +83,31 @@ public function testDotKeyExistsWithWildCard(): void $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.*.age.sub', $this->array)); $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.age.*.sub', $this->array)); $this->assertFalse(ArrayHelper::dotKeyExists('*.name', [['name' => 'Fred'], 'not-an-array'])); + $this->assertFalse(ArrayHelper::dotKeyExists('*.name', [])); + } + + public function testDotKeyExistsWithZeroIndexAfterWildCard(): void + { + $array = [ + 'users' => [ + ['contacts' => [['email' => 'alice@example.com']]], + ['contacts' => [['email' => 'bob@example.com']]], + ], + ]; + + $this->assertTrue(ArrayHelper::dotKeyExists('users.*.contacts.0.email', $array)); + $this->assertTrue(ArrayHelper::dotKeyExists('users.*.contacts.*.email', $array)); + $this->assertFalse(ArrayHelper::dotKeyExists('users.*.contacts.0.nonexistent', $array)); + $this->assertFalse(ArrayHelper::dotKeyExists('users.*.contacts.1.email', $array)); + + $arrayWithMissingKey = [ + 'users' => [ + ['contacts' => [['email' => 'alice@example.com']]], + ['contacts' => [['name' => 'Bob']]], + ], + ]; + + $this->assertFalse(ArrayHelper::dotKeyExists('users.*.contacts.0.email', $arrayWithMissingKey)); + $this->assertFalse(ArrayHelper::dotKeyExists('users.*.contacts.*.email', $arrayWithMissingKey)); } } From b187467d60a293aa7fe84a32f7ee5298f8a34a8c Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sun, 15 Nov 2026 21:34:35 +0700 Subject: [PATCH 5/5] perf(Helpers): avoid recursion when one segment remains after wildcard in dotKeyExists() --- system/Helpers/Array/ArrayHelper.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/system/Helpers/Array/ArrayHelper.php b/system/Helpers/Array/ArrayHelper.php index 5ff11caf01e0..6af6ecc506d2 100644 --- a/system/Helpers/Array/ArrayHelper.php +++ b/system/Helpers/Array/ArrayHelper.php @@ -165,6 +165,18 @@ private static function checkDotKeyExists(array $indexes, array $currentArray): return false; } + if (count($indexes) === 1) { + $nextIndex = $indexes[0]; + + foreach ($currentArray as $item) { + if (! is_array($item) || ! array_key_exists($nextIndex, $item)) { + return false; + } + } + + return true; + } + foreach ($currentArray as $item) { if (! is_array($item) || ! self::checkDotKeyExists($indexes, $item)) { return false;