From 69971505a2491a3858c0d2372a9479f86054b8be Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 31 Mar 2015 22:21:15 -0400 Subject: [PATCH] Fix maxDimensions() for empty/1 dimensional arrays. maxDimensions() should not emit warnings or mis-calculate an array's dimensions. Fixes #6224 --- lib/Cake/Test/Case/Utility/HashTest.php | 8 ++++++++ lib/Cake/Utility/Hash.php | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/HashTest.php b/lib/Cake/Test/Case/Utility/HashTest.php index 5580315cd..bfb863279 100644 --- a/lib/Cake/Test/Case/Utility/HashTest.php +++ b/lib/Cake/Test/Case/Utility/HashTest.php @@ -275,6 +275,14 @@ class HashTest extends CakeTestCase { * @return void */ public function testMaxDimensions() { + $data = array(); + $result = Hash::maxDimensions($data); + $this->assertEquals(0, $result); + + $data = array('a', 'b'); + $result = Hash::maxDimensions($data); + $this->assertEquals(1, $result); + $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1')); $result = Hash::maxDimensions($data); $this->assertEquals($result, 2); diff --git a/lib/Cake/Utility/Hash.php b/lib/Cake/Utility/Hash.php index 70485195b..9462efcb4 100644 --- a/lib/Cake/Utility/Hash.php +++ b/lib/Cake/Utility/Hash.php @@ -763,10 +763,14 @@ class Hash { $depth = array(); if (is_array($data) && reset($data) !== false) { foreach ($data as $value) { - $depth[] = self::dimensions((array)$value) + 1; + if (is_array($value)) { + $depth[] = self::dimensions($value) + 1; + } else { + $depth[] = 1; + } } } - return max($depth); + return empty($depth) ? 0 : max($depth); } /**