Repository navigation
fix(Helpers): handle zero key and non-array intermediate values in ArrayHelper::dotKeyExists() - #10757
fix(Helpers): handle zero key and non-array intermediate values in ArrayHelper::dotKeyExists()#10757banghamid wants to merge 2 commits into
Conversation
…rayHelper::dotKeyExists()
|
Thank you @banghamid for this PR! The fix is clean, accurate, and addresses two very real issues: premature loop termination due to The logic and test coverage look very solid. I have just a couple of minor suggestions before this is ready to merge: 1. Test coverage for
|
|
Thanks for taking a look @gr8man! Good catch on the wildcard non-array item branch not being covered yet. I pushed an update to address all three points:
Let me know if anything else is needed! |
Description
ArrayHelper::dotKeyExists()previously had two bugs when traversing dot-notation paths:'0':while ($currentIndex = array_shift($indexes))evaluated'0'as falsy and immediately exited the loop, returningtruewithout checking whether'0'or any subsequent segments existed in the array (e.g.,ArrayHelper::dotKeyExists('0', [])andArrayHelper::dotKeyExists('contacts.friends.0.nonexistent', $array)both returnedtrue).TypeErroron non-array intermediate values: When an intermediate segment resolved to a scalar ornull(e.g.,contacts.friends.1.age.subwhereageis an integer), callingarray_key_exists()on the next segment threw aTypeError.This PR:
while (($currentIndex = array_shift($indexes)) !== null)so'0'segments are properly traversed.is_array()on$currentArrayand wildcard$itemelements before callingarray_key_exists().ArrayHelperDotKeyExistsTest.\docs/changelogs/v4.7.7.md.Checklist: