Passed
Push — master ( 6c8dfe...6e67eb )
by SignpostMarv
03:14
created

InefficientDaftNestedRebuild::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
    protected $tree;
17
18
    /**
19
    * @var array<int, scalar|scalar[]>
20
    */
21
    protected $parentIdXref = [];
22
23
    /**
24
    * @var array<int, array<int, DaftNestedWriteableObject>>
25
    */
26
    protected $children = [[]];
27
28
    /**
29
    * @var array<int, scalar|scalar[]>
30
    */
31
    protected $idXref = [];
32
33
    public function __construct(DaftNestedWriteableObjectTree $tree)
34
    {
35
        $this->tree = $tree;
36
    }
37
38
    public function RebuildTree() : void
39
    {
40
        $this->ProcessTree();
41
42
        $n = 0;
43
44
        /**
45
        * @var DaftNestedWriteableObject $rootLeaf
46
        */
47
        foreach ($this->children[0] as $rootLeaf) {
48
            $n = $this->InefficientRebuild(
49
                $rootLeaf,
50
                0,
51
                $n,
52
                $this->parentIdXref,
53
                $this->idXref,
54
                $this->children
55
            );
56
        }
57
    }
58
59
    protected function Reset() : void
60
    {
61
        $parentIdXref = [(array) $this->tree->GetNestedObjectTreeRootId()];
62
63
        /**
64
        * @var array<int, array<int, DaftNestedWriteableObject>> $children
65
        */
66
        $children = [[]];
67
68
        /**
69
        * @var array<int, scalar|scalar[]> $idXref
70
        */
71
        $idXref = [];
72
73
        $this->parentIdXref = $parentIdXref;
74
        $this->children = $children;
75
        $this->idXref = $idXref;
76
    }
77
78
    protected function ProcessTree() : void
79
    {
80
        $this->Reset();
81
82
        $tree = $this->tree->RecallDaftNestedObjectFullTree();
83
84
        usort($tree, function (DaftNestedWriteableObject $a, DaftNestedWriteableObject $b) : int {
85
            return $this->tree->CompareObjects($a, $b);
86
        });
87
88
        /**
89
        * @var DaftNestedWriteableObject $leaf
90
        */
91
        foreach ($tree as $i => $leaf) {
92
            $leafParentId = $leaf->ObtainDaftNestedObjectParentId();
93
            $pos = array_search($leafParentId, $this->parentIdXref, true);
94
95
            if (false === $pos) {
96
                $this->parentIdXref[] = $leafParentId;
97
98
                /**
99
                * @var int $pos
100
                */
101
                $pos = array_search($leafParentId, $this->parentIdXref, true);
102
103
                $this->children[$pos] = [];
104
            }
105
106
            if ( ! in_array($leaf, $this->children[$pos], true)) {
107
                $this->children[$pos][] = $leaf;
108
            }
109
110
            if ( ! in_array($leaf->GetId(), $this->idXref, true)) {
111
                /**
112
                * @var scalar|scalar[] $leafId
113
                */
114
                $leafId = $leaf->GetId();
115
                $this->idXref[] = $leafId;
116
            }
117
118
            $leaf->SetIntNestedLeft(0);
119
            $leaf->SetIntNestedRight(0);
120
            $leaf->SetIntNestedLevel(0);
121
122
            $tree[$i] = $this->tree->StoreThenRetrieveFreshLeaf($leaf);
123
        }
124
    }
125
126
    protected function InefficientRebuild(
127
        DaftNestedWriteableObject $leaf,
128
        int $level,
129
        int $n,
130
        array $parents,
131
        array $ids,
132
        array $children
0 ignored issues
show
Unused Code introduced by
The parameter $children is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

132
        /** @scrutinizer ignore-unused */ array $children

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
133
    ) : int {
134
        /**
135
        * @var scalar|scalar[] $id
136
        */
137
        $id = $leaf->GetId();
138
139
        $leaf->SetIntNestedLevel($level);
140
        $leaf->SetIntNestedLeft($n);
141
142
        ++$n;
143
144
        /**
145
        * @var int|false $parentPos
146
        */
147
        $parentPos = array_search((array) $id, $parents, true);
148
149
        if (false !== $parentPos) {
150
            /**
151
            * @var DaftNestedWriteableObject $child
152
            */
153
            foreach ($this->children[$parentPos] as $child) {
154
                $n = $this->InefficientRebuild($child, $level + 1, $n, $parents, $ids, $this->children);
155
            }
156
        }
157
158
        $leaf->SetIntNestedRight($n);
159
160
        $this->tree->StoreThenRetrieveFreshLeaf($leaf);
161
162
        return $n + 1;
163
    }
164
}
165