From 239af0322ee0ef460042fdbd4928b607c71e0151 Mon Sep 17 00:00:00 2001 From: Dan Voyce Date: Fri, 20 Jun 2014 11:03:17 +1000 Subject: [PATCH] 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 --- lib/Cake/Utility/Hash.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Utility/Hash.php b/lib/Cake/Utility/Hash.php index 359311a12..49f0ac5ae 100644 --- a/lib/Cake/Utility/Hash.php +++ b/lib/Cake/Utility/Hash.php @@ -38,6 +38,7 @@ class Hash { * @param string|array $path The path being searched for. Either a dot * separated string, or an array of path segments. * @param mixed $default The return value when the path does not exist + * @throws InvalidArgumentException * @return mixed The value fetched from the array, or null. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::get */ @@ -48,8 +49,13 @@ class Hash { if (is_string($path) || is_numeric($path)) { $parts = explode('.', $path); } else { - $parts = $path; + if (!is_array($path)) { + throw new InvalidArgumentException(__d('cake_dev', 'Invalid Parameter %s, should be dot separated path or array.', $path)); + } + + $parts = $path; } + foreach ($parts as $key) { if (is_array($data) && isset($data[$key])) { $data =& $data[$key]; @@ -57,6 +63,7 @@ class Hash { return $default; } } + return $data; }