Skip to content

Commit 239af03

Browse files
voyceymarkstory
authored andcommitted
Raise an exception when Hash::get() receives invalid parameters
I'm not sure on whether this is a problem with my local app but I have seen it a couple of times in a couple of projects: Warning (2): Invalid argument supplied for foreach() [CORE/Cake/Utility/Hash.php, line 52] I think Hash::get should be able to handle this better rather than throwing an error in a core Util file. Refs #3754
1 parent 6bacc5b commit 239af03

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/Cake/Utility/Hash.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Hash {
3838
* @param string|array $path The path being searched for. Either a dot
3939
* separated string, or an array of path segments.
4040
* @param mixed $default The return value when the path does not exist
41+
* @throws InvalidArgumentException
4142
* @return mixed The value fetched from the array, or null.
4243
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::get
4344
*/
@@ -48,15 +49,21 @@ public static function get(array $data, $path, $default = null) {
4849
if (is_string($path) || is_numeric($path)) {
4950
$parts = explode('.', $path);
5051
} else {
51-
$parts = $path;
52+
if (!is_array($path)) {
53+
throw new InvalidArgumentException(__d('cake_dev', 'Invalid Parameter %s, should be dot separated path or array.', $path));
54+
}
55+
56+
$parts = $path;
5257
}
58+
5359
foreach ($parts as $key) {
5460
if (is_array($data) && isset($data[$key])) {
5561
$data =& $data[$key];
5662
} else {
5763
return $default;
5864
}
5965
}
66+
6067
return $data;
6168
}
6269

0 commit comments

Comments
 (0)