| Conditions | 10 |
| Paths | 41 |
| Total Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 24 |
| CRAP Score | 10 |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 7 | 10 | public static function sort($data, array $sorting = []) |
|
| 8 | { |
||
| 9 | 10 | if (empty($sorting)) { |
|
| 10 | 2 | return $data; |
|
| 11 | } |
||
| 12 | |||
| 13 | 8 | foreach ($data as $key => $row) { |
|
| 14 | 8 | foreach ($row as $column => $value) { |
|
| 15 | 8 | $col = '__' . $column . '__'; |
|
| 16 | 8 | if (!isset($$col)) { |
|
| 17 | 8 | $$col = []; |
|
| 18 | } |
||
| 19 | 8 | ${$col}[$key] = $value; |
|
| 20 | } |
||
| 21 | } |
||
| 22 | |||
| 23 | 8 | $args = []; |
|
| 24 | 8 | foreach ($sorting as $sort) { |
|
| 25 | 8 | foreach ($sort as $key => $direction) { |
|
| 26 | 8 | $col = '__' . $key . '__'; |
|
| 27 | 8 | if (!isset($$col)) { |
|
| 28 | 4 | continue; |
|
| 29 | } |
||
| 30 | 6 | $args[] = $$col; |
|
| 31 | 7 | $args[] = strtolower($direction) == 'asc' ? SORT_ASC : SORT_DESC; |
|
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | 8 | if (empty($args)) { |
|
| 36 | 2 | return $data; |
|
| 37 | } |
||
| 38 | |||
| 39 | 6 | $keys = array_keys($data); |
|
| 40 | 6 | $args[] = &$data; |
|
| 41 | 6 | $args[] = &$keys; |
|
| 42 | |||
| 43 | 6 | call_user_func_array('array_multisort', $args); |
|
| 44 | 6 | return array_combine($keys, $data); |
|
| 45 | } |
||
| 46 | } |
||
| 47 |