Passed
Push — master ( 850017...a8ea98 )
by SignpostMarv
02:56
created

ModifyDaftNestedObjectTreeRemoveWithObjectPrepareRemovalAndRebuild()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 10
ccs 3
cts 3
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
* Base daft nested objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject;
10
11
use BadMethodCallException;
12
use InvalidArgumentException;
13
use RuntimeException;
14
15
trait TraitWriteableTreeUtilities
16
{
17
    /**
18
    * @param mixed $id
19
    */
20
    abstract public function RecallDaftObject($id) : ? DaftObject;
21
22
    abstract public function CountDaftNestedObjectTreeWithObject(
23
        DaftNestedObject $root,
24
        bool $includeRoot,
25
        ? int $relativeDepthLimit
26
    ) : int;
27
28
    abstract public function RemoveDaftObject(DefinesOwnIdPropertiesInterface $object) : void;
29
30
    abstract public function CountDaftNestedObjectFullTree(int $relativeDepthLimit = null) : int;
31
32
    abstract public function RememberDaftObject(DefinesOwnIdPropertiesInterface $object) : void;
33
34
    abstract public function ForgetDaftObject(DefinesOwnIdPropertiesInterface $object) : void;
35
36
    /**
37
    * @param mixed $id
38
    */
39
    abstract public function ForgetDaftObjectById($id) : void;
40
41
    /**
42
    * @return array<int, DaftNestedObject>
43
    */
44
    abstract public function RecallDaftNestedObjectTreeWithObject(
45
        DaftNestedObject $root,
46
        bool $includeRoot,
47
        ? int $relativeDepthLimit
48
    ) : array;
49
50
    /**
51
    * @return array<int, DaftNestedWriteableObject>
52
    */
53
    abstract public function RecallDaftNestedObjectFullTree(int $relativeDepthLimit = null) : array;
54
55
    /**
56
    * @param mixed $id
57
    *
58
    * @return array<int, DaftNestedWriteableObject>
59
    */
60
    abstract public function RecallDaftNestedObjectTreeWithId(
61
        $id,
62
        bool $includeRoot,
63
        ? int $relativeDepthLimit
64
    ) : array;
65
66 48
    protected function ModifyDaftNestedObjectTreeInsertMaybeLooseIntoTree(
67
        DaftNestedWriteableObjectTree $tree,
68
        ? DaftNestedWriteableObject $leaf,
69
        ? DaftObject $reference,
70
        bool $isRoot,
71
        bool $before,
72
        ? bool $above
73
    ) : ? DaftNestedWriteableObject {
74 48
        if ( ! is_null($leaf) && (($reference instanceof DaftNestedWriteableObject) || $isRoot)) {
75 24
            if ($reference instanceof DaftNestedWriteableObject) {
76 18
                return $tree->ModifyDaftNestedObjectTreeInsert($leaf, $reference, $before, $above);
77
            }
78
79 24
            return $this->ModifyDaftNestedObjectTreeInsertLooseIntoTree($leaf, $before, $above);
80
        }
81
82 24
        return null;
83
    }
84
85 60
    protected function RebuildAfterInsert(
86
        DaftNestedWriteableObject $newLeaf
87
    ) : DaftNestedWriteableObject {
88 60
        $this->RebuildTreeInefficiently();
89
90 60
        $newLeaf = $this->RecallDaftObject($newLeaf->GetId());
91
92 60
        if ( ! ($newLeaf instanceof DaftNestedWriteableObject)) {
93 12
            throw new RuntimeException('Could not retrieve leaf from tree after rebuilding!');
94
        }
95
96 48
        return $newLeaf;
97
    }
98
99 4
    protected function ModifyDaftNestedObjectTreeRemoveWithObjectPrepareRemovalAndRebuild(
100
        DaftNestedWriteableObject $root,
101
        DaftNestedWriteableObject $replacementRoot
102
    ) : void {
103
        /**
104
        * @var scalar|scalar[] $replacementRootId
105
        */
106 4
        $replacementRootId = $this->StoreThenRetrieveFreshLeaf($replacementRoot)->GetId();
0 ignored issues
show
Bug introduced by
It seems like StoreThenRetrieveFreshLeaf() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

106
        $replacementRootId = $this->/** @scrutinizer ignore-call */ StoreThenRetrieveFreshLeaf($replacementRoot)->GetId();
Loading history...
107
108 4
        $this->UpdateRoots($root, $replacementRootId);
109 4
    }
110
111
    /**
112
    * @param scalar|scalar[] $replacementRootId
113
    */
114 10
    protected function UpdateRoots(DaftNestedWriteableObject $root, $replacementRootId) : void
115
    {
116
        /**
117
        * @var DaftNestedWriteableObject $alter
118
        */
119 10
        foreach ($this->RecallDaftNestedObjectTreeWithObject($root, false, 1) as $alter) {
120 4
            if ($alter instanceof DaftNestedWriteableObject) {
121 4
                $alter->AlterDaftNestedObjectParentId($replacementRootId);
122 4
                $this->RememberDaftObject($alter);
123
            }
124
        }
125 10
    }
126
127 110
    final protected function ThrowIfNotTree() : DaftNestedWriteableObjectTree
128
    {
129 110
        if ( ! ($this instanceof DaftNestedWriteableObjectTree)) {
130 2
            throw new BadMethodCallException(
131
                'Cannot call ' .
132
                __FUNCTION__ .
133
                ' on ' .
134 2
                static::class .
135 2
                ', class does not implement ' .
136 2
                DaftNestedWriteableObjectTree::class
137
            );
138
        }
139
140 108
        return $this;
141
    }
142
143
    /**
144
    * @param DaftNestedWriteableObject|mixed $leaf
145
    */
146 72
    protected function MaybeGetLeaf($leaf) : ? DaftNestedWriteableObject
147
    {
148 72
        $tree = $this->ThrowIfNotTree();
149
150 72
        if ($leaf === $tree->GetNestedObjectTreeRootId()) {
151 24
            throw new InvalidArgumentException('Cannot pass root id as new leaf');
152 48
        } elseif ($leaf instanceof DaftNestedWriteableObject) {
153 24
            return $tree->StoreThenRetrieveFreshLeaf($leaf);
154
        }
155
156
        /**
157
        * @var DaftNestedWriteableObject|null $out
158
        */
159 40
        $out = $tree->RecallDaftObject($leaf);
160
161 40
        return ($out instanceof DaftNestedWriteableObject) ? $out : null;
162
    }
163
164 24
    protected function ModifyDaftNestedObjectTreeInsertLooseIntoTree(
165
        DaftNestedWriteableObject $leaf,
166
        bool $before,
167
        ? bool $above
168
    ) : DaftNestedWriteableObject {
169 24
        $leaves = $this->RecallDaftNestedObjectFullTree(0);
170
        $leaves = array_filter($leaves, function (DaftNestedWriteableObject $e) use ($leaf) : bool {
171 24
            return $e->GetId() !== $leaf->GetId();
172 24
        });
173 24
        $tree = $this->ThrowIfNotTree();
174
175 24
        if (0 === count($leaves)) {
176 24
            $leaf->SetIntNestedLeft(0);
177 24
            $leaf->SetIntNestedRight(1);
178 24
            $leaf->SetIntNestedLevel(0);
179 24
            $leaf->AlterDaftNestedObjectParentId($tree->GetNestedObjectTreeRootId());
180
181 24
            return $tree->StoreThenRetrieveFreshLeaf($leaf);
182
        }
183
184 24
        $reference = $before ? current($leaves) : end($leaves);
185
186 24
        return $this->ModifyDaftNestedObjectTreeInsert($leaf, $reference, $before, $above);
0 ignored issues
show
Bug introduced by
The method ModifyDaftNestedObjectTreeInsert() does not exist on SignpostMarv\DaftObject\...tWriteableTreeUtilities. Did you maybe mean ModifyDaftNestedObjectTr...ertMaybeLooseIntoTree()? ( Ignorable by Annotation )

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

186
        return $this->/** @scrutinizer ignore-call */ ModifyDaftNestedObjectTreeInsert($leaf, $reference, $before, $above);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
187
    }
188
189 4
    protected function MaybeRemoveWithPossibleObject(
190
        DaftNestedWriteableObject $rootObject,
191
        ? DaftObject $replacementRootObject
192
    ) : int {
193 4
        if ( ! ($replacementRootObject instanceof DaftNestedWriteableObject)) {
194 2
            throw new InvalidArgumentException(
195 2
                'Could not locate replacement root, cannot leave orphan objects!'
196
            );
197
        }
198
199 2
        return $this->ModifyDaftNestedObjectTreeRemoveWithObject(
0 ignored issues
show
Bug introduced by
The method ModifyDaftNestedObjectTreeRemoveWithObject() does not exist on SignpostMarv\DaftObject\...tWriteableTreeUtilities. Did you maybe mean ModifyDaftNestedObjectTr...pareRemovalAndRebuild()? ( Ignorable by Annotation )

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

199
        return $this->/** @scrutinizer ignore-call */ ModifyDaftNestedObjectTreeRemoveWithObject(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
200 2
            $rootObject,
201 2
            $replacementRootObject
202
        );
203
    }
204
205
    /**
206
    * @param scalar|scalar[] $replacementRoot
207
    */
208 6
    protected function UpdateRemoveThenRebuild(
209
        DaftNestedWriteableObject $rootObject,
210
        $replacementRoot
211
    ) : void {
212 6
        $this->UpdateRoots($rootObject, $replacementRoot);
213
214 6
        $this->RemoveDaftObject($rootObject);
215
216 6
        $this->RebuildTreeInefficiently();
217 6
    }
218
219 48
    protected function ModifyDaftNestedObjectTreeInsertAbove(
220
        DaftNestedWriteableObject $newLeaf,
221
        DaftNestedWriteableObject $referenceLeaf
222
    ) : void {
223 48
        $newLeaf->AlterDaftNestedObjectParentId($referenceLeaf->ObtainDaftNestedObjectParentId());
224 48
        $referenceLeaf->AlterDaftNestedObjectParentId($newLeaf->GetId());
225
226 48
        $this->StoreThenRetrieveFreshLeaf($newLeaf);
227 48
        $this->StoreThenRetrieveFreshLeaf($referenceLeaf);
228 48
    }
229
230 6
    protected function ModifyDaftNestedObjectTreeInsertBelow(
231
        DaftNestedWriteableObject $newLeaf,
232
        DaftNestedWriteableObject $referenceLeaf
233
    ) : void {
234 6
        $newLeaf->AlterDaftNestedObjectParentId($referenceLeaf->GetId());
235 6
        $this->StoreThenRetrieveFreshLeaf($newLeaf);
236 6
    }
237
238 52
    protected function SiblingsExceptLeaf(
239
        DaftNestedWriteableObject $newLeaf,
240
        DaftNestedWriteableObject $referenceLeaf
241
    ) : array {
242
        /**
243
        * @var array<int, DaftNestedWriteableObject> $siblings
244
        */
245 52
        $siblings = array_values(array_filter(
246 52
            $this->RecallDaftNestedObjectTreeWithId(
247 52
                $referenceLeaf->ObtainDaftNestedObjectParentId(),
248 52
                false,
249
                0
250
            ),
251
            function (DaftNestedWriteableObject $leaf) use ($newLeaf) : bool {
252 28
                return $leaf->GetId() !== $newLeaf->GetId();
253 52
            }
254
        ));
255
256 52
        return $siblings;
257
    }
258
259 52
    protected function ModifyDaftNestedObjectTreeInsertAdjacent(
260
        DaftNestedWriteableObject $newLeaf,
261
        DaftNestedWriteableObject $referenceLeaf,
262
        bool $before
263
    ) : void {
264 52
        $siblings = $this->SiblingsExceptLeaf($newLeaf, $referenceLeaf);
265
266 52
        $siblingIds = [];
267 52
        $siblingSort = [];
268 52
        $j = count($siblings);
269
270
        /**
271
        * @var DaftNestedWriteableObject $leaf
272
        */
273 52
        foreach ($siblings as $leaf) {
274
            /**
275
            * @var scalar|scalar[] $siblingId
276
            */
277 28
            $siblingId = $leaf->GetId();
278 28
            $siblingIds[] = $siblingId;
279 28
            $siblingSort[] = $leaf->GetIntNestedSortOrder();
280
        }
281
282 52
        $pos = array_search($referenceLeaf->GetId(), $siblingIds, true);
283
284 52
        if (false === $pos) {
285 24
            throw new RuntimeException('Reference leaf not found in siblings tree!');
286
        }
287
288 28
        for ($i = 0; $i < $j; ++$i) {
289 28
            $siblings[$i]->SetIntNestedSortOrder(
290 28
                $siblingSort[$i] +
291 28
                (($before ? ($i < $pos) : ($i <= $pos)) ? -1 : 1)
292
            );
293 28
            $this->StoreThenRetrieveFreshLeaf($siblings[$i]);
294
        }
295
296 28
        $newLeaf->SetIntNestedSortOrder($siblingSort[$pos]);
297 28
        $newLeaf->AlterDaftNestedObjectParentId($referenceLeaf->ObtainDaftNestedObjectParentId());
298
299 28
        $this->StoreThenRetrieveFreshLeaf($newLeaf);
300 28
    }
301
302 60
    protected function RebuildTreeInefficiently() : void
303
    {
304
        /**
305
        * @var DaftNestedWriteableObjectTree $tree
306
        */
307 60
        $tree = $this->ThrowIfNotTree();
308 60
        $rebuilder = new InefficientDaftNestedRebuild($tree);
309 60
        $rebuilder->RebuildTree();
310 60
    }
311
}
312