Conditions | 8 |
Paths | 17 |
Total Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 72 |
Changes | 0 |
1 | <?php |
||
33 | private static function _flatToNested( |
||
34 | array $flatTree, |
||
35 | string $levelName = 'level', |
||
36 | ?int $level = null, |
||
37 | int $startPos = 0, |
||
38 | array &$result = array() |
||
39 | ): array { |
||
40 | $total = count($flatTree); |
||
41 | $first = true; |
||
42 | |||
43 | for ($pos = $startPos; $pos < $total; ++$pos) { |
||
44 | $item = $flatTree[$pos]; |
||
45 | |||
46 | if (null === $level) { |
||
47 | $level = $item[$levelName]; |
||
48 | } |
||
49 | |||
50 | if (!array_key_exists('_children', $item)) { |
||
51 | $item['_children'] = array(); |
||
52 | } |
||
53 | |||
54 | if ($level == $item[$levelName]) { |
||
55 | $result[] = $item; |
||
56 | $first = true; |
||
57 | } elseif (($level + 1) == $item[$levelName] && true == $first) { |
||
|
|||
58 | $children = array($item); |
||
59 | self::_flatToNested($flatTree, $levelName, (int) $item[$levelName], $pos + 1, $children); |
||
60 | $result[count($result) - 1]['_children'] = $children; |
||
61 | $first = false; |
||
62 | } elseif ($level > $item[$levelName]) { |
||
63 | break; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | return $result; |
||
68 | } |
||
69 | } |
||
70 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.