From c321a8fa9354e48626fde7dedda83fd62b24fee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=BCrth?= Date: Tue, 8 Jul 2014 13:01:02 +0200 Subject: [PATCH] Hash::nest() should throw an exception instead of returning an empty array Refs: https://github.com/cakephp/cakephp/pull/3498#issuecomment-48316204 --- lib/Cake/Test/Case/Utility/HashTest.php | 6 +++--- lib/Cake/Utility/Hash.php | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/HashTest.php b/lib/Cake/Test/Case/Utility/HashTest.php index 5a68b55e9..2dac9f756 100644 --- a/lib/Cake/Test/Case/Utility/HashTest.php +++ b/lib/Cake/Test/Case/Utility/HashTest.php @@ -2277,8 +2277,9 @@ class HashTest extends CakeTestCase { } /** - * Tests that nest() returns an empty array for invalid input instead of throwing notices. + * Tests that nest() throws an InvalidArgumentException when providing an invalid input. * + * @expectedException InvalidArgumentException * @return void */ public function testNestInvalid() { @@ -2291,8 +2292,7 @@ class HashTest extends CakeTestCase { ) ) ); - $result = Hash::nest($input); - $this->assertSame(array(), $result); + Hash::nest($input); } /** diff --git a/lib/Cake/Utility/Hash.php b/lib/Cake/Utility/Hash.php index c08dc07fb..1b8cc1cfe 100644 --- a/lib/Cake/Utility/Hash.php +++ b/lib/Cake/Utility/Hash.php @@ -1006,6 +1006,7 @@ class Hash { * @param array $options Options are: * @return array of results, nested * @see Hash::extract() + * @throws InvalidArgumentException When providing invalid data. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::nest */ public static function nest(array $data, $options = array()) { @@ -1048,10 +1049,14 @@ class Hash { } } + if (!$return) { + throw new InvalidArgumentException(__d('cake_dev', + 'Invalid data array to nest.' + )); + } + if ($options['root']) { $root = $options['root']; - } elseif (!$return) { - return array(); } else { $root = self::get($return[0], $parentKeys); }