Passed
Push — master ( 3c183e...81d331 )
by SignpostMarv
03:01
created

InefficientDaftNestedRebuild::InefficientRebuild()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 6
dl 0
loc 37
rs 9.328
c 0
b 0
f 0
ccs 12
cts 12
cp 1
crap 3
1
<?php
2
/**
3
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject;
10
11
class InefficientDaftNestedRebuild
12
{
13
    /**
14
    * @var DaftNestedWriteableObjectTree
15
    */
16
    private $tree;
17
18 60
    public function __construct(DaftNestedWriteableObjectTree $tree)
19
    {
20 60
        $this->tree = $tree;
21 60
    }
22
23 60
    public function RebuildTree() : void
24
    {
25 60
        $parentIdXref = [(array) $this->tree->GetNestedObjectTreeRootId()];
26
27
        /**
28
        * @var array<int, array<int, DaftNestedWriteableObject>> $children
29
        */
30 60
        $children = [[]];
31
32
        /**
33
        * @var array<int, scalar|scalar[]> $idXref
34
        */
35 60
        $idXref = [];
36
37 60
        $tree = $this->tree->RecallDaftNestedObjectFullTree();
38
39
        usort($tree, function (DaftNestedWriteableObject $a, DaftNestedWriteableObject $b) : int {
40 60
            return $this->tree->CompareObjects($a, $b);
41 60
        });
42
43
        /**
44
        * @var DaftNestedWriteableObject $leaf
45
        */
46 60
        foreach ($tree as $i => $leaf) {
47 60
            $leafParentId = $leaf->ObtainDaftNestedObjectParentId();
48 60
            $pos = array_search($leafParentId, $parentIdXref, true);
49
50 60
            if (false === $pos) {
51 54
                $parentIdXref[] = $leafParentId;
52
53
                /**
54
                * @var int $pos
55
                */
56 54
                $pos = array_search($leafParentId, $parentIdXref, true);
57
58 54
                $children[$pos] = [];
59
            }
60
61 60
            if ( ! in_array($leaf, $children[$pos], true)) {
62 60
                $children[$pos][] = $leaf;
63
            }
64
65 60
            if ( ! in_array($leaf->GetId(), $idXref, true)) {
66
                /**
67
                * @var scalar|scalar[] $leafId
68
                */
69 60
                $leafId = $leaf->GetId();
70 60
                $idXref[] = $leafId;
71
            }
72
73 60
            $leaf->SetIntNestedLeft(0);
74 60
            $leaf->SetIntNestedRight(0);
75 60
            $leaf->SetIntNestedLevel(0);
76
77 60
            $tree[$i] = $this->tree->StoreThenRetrieveFreshLeaf($leaf);
78
        }
79
80 60
        $n = 0;
81
82
        /**
83
        * @var DaftNestedWriteableObject $rootLeaf
84
        */
85 60
        foreach ($children[0] as $rootLeaf) {
86 60
            $n = $this->InefficientRebuild($rootLeaf, 0, $n, $parentIdXref, $idXref, $children);
87
        }
88 60
    }
89
90 60
    protected function InefficientRebuild(
91
        DaftNestedWriteableObject $leaf,
92
        int $level,
93
        int $n,
94
        array $parents,
95
        array $ids,
96
        array $children
97
    ) : int {
98
        /**
99
        * @var scalar|scalar[] $id
100
        */
101 60
        $id = $leaf->GetId();
102
103 60
        $leaf->SetIntNestedLevel($level);
104 60
        $leaf->SetIntNestedLeft($n);
105
106 60
        ++$n;
107
108
        /**
109
        * @var int|false $parentPos
110
        */
111 60
        $parentPos = array_search((array) $id, $parents, true);
112
113 60
        if (false !== $parentPos) {
114
            /**
115
            * @var DaftNestedWriteableObject $child
116
            */
117 54
            foreach ($children[$parentPos] as $child) {
118 54
                $n = $this->InefficientRebuild($child, $level + 1, $n, $parents, $ids, $children);
119
            }
120
        }
121
122 60
        $leaf->SetIntNestedRight($n);
123
124 60
        $this->tree->StoreThenRetrieveFreshLeaf($leaf);
125
126 60
        return $n + 1;
127
    }
128
}
129