From 7b2d7ad7481065b59cfecba6cde39ee4d89e616e Mon Sep 17 00:00:00 2001 From: Chris Valliere Date: Thu, 16 Jul 2015 09:36:46 -0400 Subject: [PATCH 1/3] Test Case for Hash::maxDimensions Added test case for Hash::maxDimensions using the example array in pull request #7040. --- lib/Cake/Test/Case/Utility/HashTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/Cake/Test/Case/Utility/HashTest.php b/lib/Cake/Test/Case/Utility/HashTest.php index 419a7f71a..ba736b474 100644 --- a/lib/Cake/Test/Case/Utility/HashTest.php +++ b/lib/Cake/Test/Case/Utility/HashTest.php @@ -303,6 +303,12 @@ class HashTest extends CakeTestCase { $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Hash::maxDimensions($data); $this->assertEquals($result, 3); + + $data = array('1' => array('1.1' => '1.1.1', '1.2' => array('1.2.1' => '1.2.1.1')),'2' => array( + '2.1' => '2.1.1',) + ); + $result = Hash::maxDimensions($data); + $this->assertEquals($result, 3); $data = array( '1' => array('1.1' => '1.1.1'), From 64f0ca002888ffbf5922830ce981d7ff447b2151 Mon Sep 17 00:00:00 2001 From: Chris Valliere Date: Thu, 16 Jul 2015 08:50:32 -0400 Subject: [PATCH 2/3] Fix Hash::maxDimensions The current Hash::maxDimensions function calls Hash::dimensions to try to get the maximum depth of the passed in array. However, this ends up only getting the depth of the first element of each 1st dimension element in the array passed to maxDimensions. The function needs to be called recursively in order to get the depth of ALL of the elements in all of the dimensions of the passed in array. I made the maxDimensions function more closely resemble the deprecated Set::countDim function in order to restore the correct functionality. --- lib/Cake/Utility/Hash.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/Cake/Utility/Hash.php b/lib/Cake/Utility/Hash.php index 142c72b0a..e4b80f1a1 100644 --- a/lib/Cake/Utility/Hash.php +++ b/lib/Cake/Utility/Hash.php @@ -756,18 +756,15 @@ class Hash { * number of dimensions in a mixed array. * * @param array $data Array to count dimensions on + * @param int $count counts current depth of this iteration * @return int The maximum number of dimensions in $data * @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::maxDimensions */ - public static function maxDimensions(array $data) { - $depth = array(); + public static function maxDimensions($data, $count = 0) { + $depth = array($count); if (is_array($data) && reset($data) !== false) { foreach ($data as $value) { - if (is_array($value)) { - $depth[] = self::dimensions($value) + 1; - } else { - $depth[] = 1; - } + $depth[] = static::maxDimensions($value, $count + 1 ); } } return empty($depth) ? 0 : max($depth); From bd23fdeebf845f229231a354970fc596aba5495c Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 20 Jul 2015 22:16:50 -0400 Subject: [PATCH 3/3] Simplify code and reduce test redundancy. We don't need the additional parameter, and some of the tests weren't covering unique scenarios. Refs #7040 --- lib/Cake/Test/Case/Utility/HashTest.php | 47 ++++++++----------------- lib/Cake/Utility/Hash.php | 6 ++-- 2 files changed, 17 insertions(+), 36 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/HashTest.php b/lib/Cake/Test/Case/Utility/HashTest.php index ba736b474..7976929d3 100644 --- a/lib/Cake/Test/Case/Utility/HashTest.php +++ b/lib/Cake/Test/Case/Utility/HashTest.php @@ -300,44 +300,25 @@ class HashTest extends CakeTestCase { $result = Hash::maxDimensions($data); $this->assertEquals($result, 2); - $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); - $result = Hash::maxDimensions($data); - $this->assertEquals($result, 3); - - $data = array('1' => array('1.1' => '1.1.1', '1.2' => array('1.2.1' => '1.2.1.1')),'2' => array( - '2.1' => '2.1.1',) + $data = array( + '1' => array('1.1' => '1.1.1'), + '2', + '3' => array('3.1' => array('3.1.1' => '3.1.1.1')) ); $result = Hash::maxDimensions($data); $this->assertEquals($result, 3); $data = array( - '1' => array('1.1' => '1.1.1'), - array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))), - '3' => array('3.1' => array('3.1.1' => '3.1.1.1')) - ); - $result = Hash::maxDimensions($data); - $this->assertEquals($result, 4); - - $data = array( - '1' => array('1.1' => '1.1.1'), - array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))), - '3' => array('3.1' => array('3.1.1' => '3.1.1.1')) - ); - $result = Hash::maxDimensions($data); - $this->assertEquals($result, 5); - - $data = array( - '1' => array('1.1' => '1.1.1'), - array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), - '3' => array('3.1' => array('3.1.1' => '3.1.1.1')) - ); - $result = Hash::maxDimensions($data); - $this->assertEquals($result, 5); - - $data = array( - '1' => array('1.1' => '1.1.1'), - array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), - '3' => array('3.1' => array('3.1.1' => '3.1.1.1')) + '1' => array( + '1.1' => '1.1.1', + '1.2' => array( + '1.2.1' => array( + '1.2.1.1', + array('1.2.2.1') + ) + ) + ), + '2' => array('2.1' => '2.1.1',) ); $result = Hash::maxDimensions($data); $this->assertEquals($result, 5); diff --git a/lib/Cake/Utility/Hash.php b/lib/Cake/Utility/Hash.php index e4b80f1a1..6f5b32abc 100644 --- a/lib/Cake/Utility/Hash.php +++ b/lib/Cake/Utility/Hash.php @@ -760,11 +760,11 @@ class Hash { * @return int The maximum number of dimensions in $data * @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::maxDimensions */ - public static function maxDimensions($data, $count = 0) { - $depth = array($count); + public static function maxDimensions($data) { + $depth = array(); if (is_array($data) && reset($data) !== false) { foreach ($data as $value) { - $depth[] = static::maxDimensions($value, $count + 1 ); + $depth[] = static::maxDimensions($value) + 1; } } return empty($depth) ? 0 : max($depth);