Skip to content

Commit

Permalink
Fix maxDimensions() for empty/1 dimensional arrays.
Browse files Browse the repository at this point in the history
maxDimensions() should not emit warnings or mis-calculate an array's
dimensions.

Fixes #6224
  • Loading branch information
markstory committed Apr 1, 2015
1 parent 758820d commit 6997150
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/Cake/Test/Case/Utility/HashTest.php
Expand Up @@ -275,6 +275,14 @@ public function testDimensions() {
* @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);
Expand Down
8 changes: 6 additions & 2 deletions lib/Cake/Utility/Hash.php
Expand Up @@ -763,10 +763,14 @@ public static function maxDimensions(array $data) {
$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);
}

/**
Expand Down

0 comments on commit 6997150

Please sign in to comment.