Test Failed
Push — master ( 852abc...2f35aa )
by SignpostMarv
02:24
created

RecallDaftNestedObjectFullTree()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 57
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4.0012

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 2
nop 1
dl 0
loc 57
rs 9.0309
c 0
b 0
f 0
ccs 22
cts 23
cp 0.9565
crap 4.0012

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject;
10
11
use RuntimeException;
12
13
abstract class DaftObjectMemoryTree extends DaftObjectMemoryRepository implements DaftNestedObjectTree
14
{
15 16
    public function RecallDaftNestedObjectFullTree(int $relativeDepthLimit = null) : array
16
    {
17
        /**
18
        * @var string[] $props
19
        */
20 16
        $props = $this->type::DaftObjectIdProperties();
21
22
        /**
23
        * @var DaftNestedObject[] $out
24
        */
25 16
        $out = array_map(
26
            function (array $id) : DaftNestedObject {
27 16
                $out = $this->RecallDaftObject($id);
28
29 16
                if ( ! ($out instanceof DaftNestedObject)) {
30
                    throw new RuntimeException('Could not retrieve leaf from tree!');
31
                }
32
33 16
                return $out;
34 16
            },
35 16
            array_map(
36
                /**
37
                * @param array<string, scalar|null> $row
38
                */
39
                function (array $row) use ($props) : array {
40
                    /**
41
                    * @var array<string, scalar|null> $out
42
                    */
43 16
                    $out = [];
44
45
                    /**
46
                    * @var string $prop
47
                    */
48 16
                    foreach ($props as $prop) {
49 16
                        $out[$prop] = $row[$prop] ?? null;
50
                    }
51
52 16
                    return $out;
53 16
                },
54 16
                (array) $this->data
55
            )
56
        );
57
58
        usort($out, function (DaftNestedObject $a, DaftNestedObject $b) : int {
59 16
            return $a->GetIntNestedLeft() <=> $b->GetIntNestedLeft();
60 16
        });
61
62 16
        if (is_int($relativeDepthLimit)) {
63 16
            $out = array_filter(
64 16
                $out,
65
                function (DaftNestedObject $e) use ($relativeDepthLimit) : bool {
66 16
                    return $e->GetIntNestedLevel() <= $relativeDepthLimit;
67 16
                }
68
            );
69
        }
70
71 16
        return $out;
72
    }
73
74 6
    public function CountDaftNestedObjectFullTree(int $relativeDepthLimit = null) : int
75
    {
76 6
        return count($this->RecallDaftNestedObjectFullTree($relativeDepthLimit));
77
    }
78
79
    /**
80
    * {@inheritdoc}
81
    */
82 12
    public function RecallDaftNestedObjectTreeWithObject(
83
        DaftNestedObject $root,
84
        bool $includeRoot,
85
        ? int $relativeDepthLimit
86
    ) : array {
87 12
        $left = $root->GetIntNestedLeft();
88 12
        $right = $root->GetIntNestedRight();
89
90 12
        if (is_int($relativeDepthLimit)) {
91 4
            $relativeDepthLimit = $root->GetIntNestedLevel() + $relativeDepthLimit;
92
        }
93
94 12
        return array_values(array_filter(
95 12
            $this->RecallDaftNestedObjectFullTree(),
96
            function (DaftNestedObject $e) use ($includeRoot, $left, $right, $relativeDepthLimit) : bool {
97
                if (
98 12
                    is_int($relativeDepthLimit) &&
99 12
                    $e->GetIntNestedLevel() > $relativeDepthLimit
100
                ) {
101 4
                    return false;
102 12
                } elseif ($includeRoot) {
103 12
                    return $e->GetIntNestedLeft() >= $left && $e->GetIntNestedRight() <= $right;
104
                }
105
106 8
                return $e->GetIntNestedLeft() > $left && $e->GetIntNestedRight() < $right;
107 12
            }
108
        ));
109
    }
110
111 6
    public function CountDaftNestedObjectTreeWithObject(
112
        DaftNestedObject $root,
113
        bool $includeRoot,
114
        ? int $relativeDepthLimit
115
    ) : int {
116 6
        return count(
117 6
            $this->RecallDaftNestedObjectTreeWithObject($root, $includeRoot, $relativeDepthLimit)
118
        );
119
    }
120
121 4
    public function RecallDaftNestedObjectTreeWithId(
122
        $id,
123
        bool $includeRoot,
124
        ? int $relativeDepthLimit
125
    ) : array {
126 4
        $object = $this->RecallDaftObject($id);
127
128
        return
129 4
            ($object instanceof DaftNestedObject)
130 4
                ? $this->RecallDaftNestedObjectTreeWithObject(
131 4
                    $object,
132 4
                    $includeRoot,
133 4
                    $relativeDepthLimit
134
                )
135 4
                : (
136
                    ((array) $id === (array) $this->GetNestedObjectTreeRootId())
137
                        ? $this->RecallDaftNestedObjectFullTree(0)
138 4
                        : []
139
                );
140
    }
141
142
    public function CountDaftNestedObjectTreeWithId(
143 4
        $id,
144 4
        bool $includeRoot,
145 4
        ? int $relativeDepthLimit
146 4
    ) : int {
147
        return count($this->RecallDaftNestedObjectTreeWithId(
148
            $id,
149
            $includeRoot,
150 4
            $relativeDepthLimit
151
        ));
152
    }
153
154 4
    public function RecallDaftNestedObjectPathToObject(
155 4
        DaftNestedObject $leaf,
156
        bool $includeLeaf
157 4
    ) : array {
158 4
        $left = $leaf->GetIntNestedLeft();
159 4
        $right = $leaf->GetIntNestedRight();
160
161
        if ( ! $includeLeaf) {
162 4
            --$left;
163 4
            ++$right;
164
        }
165 4
166 4
        return array_values(array_filter(
167
            $this->RecallDaftNestedObjectFullTree(),
168
            function (DaftNestedObject $e) use ($left, $right) : bool {
169
                return $e->GetIntNestedLeft() <= $left && $e->GetIntNestedRight() >= $right;
170 4
            }
171
        ));
172
    }
173
174 4
    public function CountDaftNestedObjectPathToObject(
175
        DaftNestedObject $leaf,
176
        bool $includeLeaf
177 4
    ) : int {
178
        return count($this->RecallDaftNestedObjectPathToObject($leaf, $includeLeaf));
179 4
    }
180
181
    public function RecallDaftNestedObjectPathToId($id, bool $includeLeaf) : array
182 4
    {
183 4
        $object = $this->RecallDaftObject($id);
184 4
185
        return
186
            ($object instanceof DaftNestedObject)
187
                ? $this->RecallDaftNestedObjectPathToObject($object, $includeLeaf)
188
                : [];
189
    }
190 4
191
    /*
192 4
    * @param mixed $id
193
    */
194
    public function CountDaftNestedObjectPathToId($id, bool $includeLeaf) : int
195 20
    {
196
        return count($this->RecallDaftNestedObjectPathToId($id, $includeLeaf));
197 20
    }
198
199 20
    public function CompareObjects(DaftNestedObject $a, DaftNestedObject $b) : int
200 20
    {
201
        return $a->GetIntNestedSortOrder() <=> $b->GetIntNestedSortOrder();
202
    }
203
204
    protected function RememberDaftObjectData(DefinesOwnIdPropertiesInterface $object) : void
205 58
    {
206
        static::ThrowIfNotType($object, DaftNestedObject::class, 1, __METHOD__);
207
208
        parent::RememberDaftObjectData($object);
209
    }
210
211 58
    /**
212
    * @param DaftObject|string $object
213 58
    */
214 2
    protected static function ThrowIfNotType(
215 2
        $object,
216 2
        string $type,
217 2
        int $argument,
218 2
        string $function
219 2
    ) : void {
220
        parent::ThrowIfNotType($object, $type, $argument, $function);
221
222 56
        if ( ! is_a($object, DaftNestedObject::class, is_string($object))) {
223
            throw new DaftObjectRepositoryTypeByClassMethodAndTypeException(
224
                $argument,
225
                static::class,
226
                $function,
227
                DaftNestedObject::class,
228
                is_string($object) ? $object : get_class($object)
229
            );
230
        }
231
    }
232
}
233