Skip to content
Permalink
Browse files

Simplify branching and add default options.

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 a9ef1f8aeaa864655e42a80bbfac093a5a01cff9
Showing with 14 additions and 28 deletions.
  1. +0 −11 lib/Cake/Test/Case/Utility/HashTest.php
  2. +14 −17 lib/Cake/Utility/Hash.php
@@ -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()
*
@@ -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)) {
@@ -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);
}
@@ -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.
You can’t perform that action at this time.