Passed
Push — master ( 6e67eb...aff4f7 )
by SignpostMarv
03:24
created

InefficientDaftNestedRebuild::Reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 17
ccs 7
cts 7
cp 1
crap 1
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 60
    public function __construct(DaftNestedWriteableObjectTree $tree)
34
    {
35 60
        $this->tree = $tree;
36 60
    }
37
38 60
    public function RebuildTree() : void
39
    {
40 60
        $this->ProcessTree();
41
42 60
        $n = 0;
43
44
        /**
45
        * @var DaftNestedWriteableObject $rootLeaf
46
        */
47 60
        foreach ($this->children[0] as $rootLeaf) {
48 60
            $n = $this->InefficientRebuild(
49 60
                $rootLeaf,
50 60
                0,
51 60
                $n,
52 60
                $this->parentIdXref,
53 60
                $this->idXref,
54 60
                $this->children
55
            );
56
        }
57 60
    }
58
59 60
    protected function Reset() : void
60
    {
61 60
        $parentIdXref = [(array) $this->tree->GetNestedObjectTreeRootId()];
62
63
        /**
64
        * @var array<int, array<int, DaftNestedWriteableObject>> $children
65
        */
66 60
        $children = [[]];
67
68
        /**
69
        * @var array<int, scalar|scalar[]> $idXref
70
        */
71 60
        $idXref = [];
72
73 60
        $this->parentIdXref = $parentIdXref;
74 60
        $this->children = $children;
75 60
        $this->idXref = $idXref;
76 60
    }
77
78 60
    protected function ProcessTree() : void
79
    {
80 60
        $this->Reset();
81
82 60
        $tree = $this->tree->RecallDaftNestedObjectFullTree();
83
84
        usort($tree, function (DaftNestedWriteableObject $a, DaftNestedWriteableObject $b) : int {
85 60
            return $this->tree->CompareObjects($a, $b);
86 60
        });
87
88
        /**
89
        * @var DaftNestedWriteableObject $leaf
90
        */
91 60
        foreach ($tree as $i => $leaf) {
92 60
            $leafParentId = $leaf->ObtainDaftNestedObjectParentId();
93 60
            $pos = array_search($leafParentId, $this->parentIdXref, true);
94
95 60
            if (false === $pos) {
96 54
                $this->parentIdXref[] = $leafParentId;
97
98
                /**
99
                * @var int $pos
100
                */
101 54
                $pos = array_search($leafParentId, $this->parentIdXref, true);
102
103 54
                $this->children[$pos] = [];
104
            }
105
106 60
            if ( ! in_array($leaf, $this->children[$pos], true)) {
107 60
                $this->children[$pos][] = $leaf;
108
            }
109
110 60
            if ( ! in_array($leaf->GetId(), $this->idXref, true)) {
111
                /**
112
                * @var scalar|scalar[] $leafId
113
                */
114 60
                $leafId = $leaf->GetId();
115 60
                $this->idXref[] = $leafId;
116
            }
117
118 60
            $leaf->SetIntNestedLeft(0);
119 60
            $leaf->SetIntNestedRight(0);
120 60
            $leaf->SetIntNestedLevel(0);
121
122 60
            $tree[$i] = $this->tree->StoreThenRetrieveFreshLeaf($leaf);
123
        }
124 60
    }
125
126 60
    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 60
        $id = $leaf->GetId();
138
139 60
        $leaf->SetIntNestedLevel($level);
140 60
        $leaf->SetIntNestedLeft($n);
141
142 60
        ++$n;
143
144
        /**
145
        * @var int|false $parentPos
146
        */
147 60
        $parentPos = array_search((array) $id, $parents, true);
148
149 60
        if (false !== $parentPos) {
150
            /**
151
            * @var DaftNestedWriteableObject $child
152
            */
153 54
            foreach ($this->children[$parentPos] as $child) {
154 54
                $n = $this->InefficientRebuild($child, $level + 1, $n, $parents, $ids, $this->children);
155
            }
156
        }
157
158 60
        $leaf->SetIntNestedRight($n);
159
160 60
        $this->tree->StoreThenRetrieveFreshLeaf($leaf);
161
162 60
        return $n + 1;
163
    }
164
}
165