Multisort::sort()   B
last analyzed

Complexity

Conditions 10
Paths 41

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 24
cts 24
cp 1
rs 7.6666
c 0
b 0
f 0
cc 10
nc 41
nop 2
crap 10

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace UniMan\Core\Utils;
4
5
class Multisort
6
{
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