Skip to content

Commit

Permalink
Simplify branching and add default options.
Browse files Browse the repository at this point in the history
Use fewer conditionals by merging defaults and avoid exceptions
by setting defaults as well.

Refs #7217
  • Loading branch information
markstory committed Aug 26, 2015
1 parent 12e5719 commit a9ef1f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
11 changes: 0 additions & 11 deletions lib/Cake/Test/Case/Utility/HashTest.php
Expand Up @@ -1468,17 +1468,6 @@ public function testSortRegularIgnoreCase() {
$this->assertEquals($expected, $sorted);
}

/**
* Tests that sort() throws an InvalidArgumentException when providing an invalid input.
*
* @expectedException InvalidArgumentException
* @return void
*/
public function testSortInvalidType() {
$toSort = array('a', 'b', 'c');
Hash::sort($toSort, '{n}', 'asc', array('regular'), true);
}

/**
* Test insert()
*
Expand Down
31 changes: 14 additions & 17 deletions lib/Cake/Utility/Hash.php
Expand Up @@ -838,16 +838,22 @@ public static function apply(array $data, $path, $function) {
* - `string` Compare values as strings
* - `natural` Compare items as strings using "natural ordering" in a human friendly way.
* Will sort foo10 below foo2 as an example. Requires PHP 5.4 or greater or it will fallback to 'regular'
*
* To do case insensitive sorting, pass the type as an array as follows:
* array('type' => 'regular', 'ignoreCase' => true)
*
* ```
* array('type' => 'regular', 'ignoreCase' => true)
* ```
*
* When using the array form, `type` defaults to 'regular'. The `ignoreCase` option
* defaults to `false`.
*
* @param array $data An array of data to sort
* @param string $path A Set-compatible path to the array value
* @param string $dir See directions above. Defaults to 'asc'.
* @param mixed $type See direction types above. Defaults to 'regular'.
* @param array|string $type See direction types above. Defaults to 'regular'.
* @return array Sorted array of data
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::sort
* @throws InvalidArgumentException
*/
public static function sort(array $data, $path, $dir = 'asc', $type = 'regular') {
if (empty($data)) {
Expand All @@ -873,21 +879,12 @@ public static function sort(array $data, $path, $dir = 'asc', $type = 'regular')

$dir = strtolower($dir);
$ignoreCase = false;

// $type can be overloaded for case insensitive sort
if (is_array($type)) {

if (!empty($type['ignoreCase'])) {
$ignoreCase = $type['ignoreCase'];
}

if (!empty($type['ignoreCase'])) {
$type = $type['type'];
} else {
throw new InvalidArgumentException(__d('cake_dev',
'Invalid parameter $type. It requires type key to be specified.'
));
}
$type += array('ignoreCase' => false, 'type' => 'regular');
$ignoreCase = $type['ignoreCase'];
$type = $type['type'];
} else {
$type = strtolower($type);
}
Expand Down Expand Up @@ -1127,4 +1124,4 @@ public static function nest(array $data, $options = array()) {
return array_values($return);
}

}
}

0 comments on commit a9ef1f8

Please sign in to comment.