Completed
Push — master ( 9afc27...035c9e )
by Chris
04:26
created

VectorNode::containsChild()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
crap 3.072
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\Jom;
4
5
use DaveRandom\Jom\Exceptions\InvalidKeyException;
6
use DaveRandom\Jom\Exceptions\InvalidReferenceNodeException;
7
use DaveRandom\Jom\Exceptions\InvalidSubjectNodeException;
8
use DaveRandom\Jom\Exceptions\WriteOperationForbiddenException;
9
10
abstract class VectorNode extends Node implements \Countable, \IteratorAggregate, \ArrayAccess
11
{
12
    /** @var Node|null */
13
    protected $firstChild;
14
15
    /** @var Node|null */
16
    protected $lastChild;
17
18
    /** @var Node[] */
19
    protected $children = [];
20
21
    /** @var int */
22
    protected $activeIteratorCount = 0;
23
24
    /**
25
     * @throws WriteOperationForbiddenException
26
     */
27 163
    private function checkWritable(): void
28
    {
29 163
        if ($this->activeIteratorCount !== 0) {
30 9
            throw new WriteOperationForbiddenException('Cannot modify a vector with an active iterator');
31
        }
32
    }
33
34
    /**
35
     * @throws InvalidSubjectNodeException
36
     */
37 153
    private function checkSubjectNodeIsOrphan(Node $node): void
38
    {
39 153
        if ($node->parent !== null) {
40 16
            throw new InvalidSubjectNodeException('Node already present in the document');
41
        }
42
    }
43
44
    /**
45
     * @throws InvalidSubjectNodeException
46
     */
47 163
    private function checkSubjectNodeHasSameOwner(Node $node): void
48
    {
49 163
        if ($node->ownerDocument !== $this->ownerDocument) {
50 21
            throw new InvalidSubjectNodeException('Node belongs to a different document');
51
        }
52
53
    }
54
55
    /**
56
     * @throws InvalidSubjectNodeException
57
     */
58 20
    private function checkSubjectNodeIsChild(Node $node): void
59
    {
60 20
        if ($node->parent !== $this) {
61
            throw new InvalidSubjectNodeException('Node not present in children of this node');
62
        }
63
64
    }
65
66
    /**
67
     * @throws InvalidReferenceNodeException
68
     */
69 23
    private function checkReferenceNodeIsChild(Node $node): void
70
    {
71 23
        if ($node->parent !== $this) {
72 2
            throw new InvalidReferenceNodeException('Reference node not present in children of this node');
73
        }
74
75
    }
76
77 151
    private function updateFirstChildIfChanged(?Node $newNode, ?Node $oldNode): void
78
    {
79 151
        if ($this->firstChild === $oldNode) {
80 151
            $this->firstChild = $newNode;
81
        }
82
    }
83
84 27
    private function updateLastChildIfChanged(?Node $newNode, ?Node $oldNode): void
85
    {
86 27
        if ($this->lastChild === $oldNode) {
87 24
            $this->lastChild = $newNode;
88
        }
89
    }
90
91 27
    private function setNodePreviousSibling(?Node $node, ?Node $newSiblingNode): void
92
    {
93 27
        if ($node !== null) {
94 12
            $node->previousSibling = $newSiblingNode;
95
        }
96
    }
97
98 151
    private function setNodeNextSibling(?Node $node, ?Node $newSiblingNode): void
99
    {
100 151
        if ($node !== null) {
101 102
            $node->nextSibling = $newSiblingNode;
102
        }
103
    }
104
105
    /**
106
     * @throws InvalidKeyException
107
     */
108 21
    protected function resolveNode($nodeOrKey): Node
109
    {
110 21
        if ($nodeOrKey instanceof Node) {
111 21
            return $nodeOrKey;
112
        }
113
114
        if (isset($this->children[$nodeOrKey])) {
115
            return $this->children[$nodeOrKey];
116
        }
117
118
        throw new InvalidKeyException("{$nodeOrKey} does not reference a valid child node");
119
    }
120
121
    /**
122
     * @throws WriteOperationForbiddenException
123
     * @throws InvalidSubjectNodeException
124
     */
125 159
    protected function appendNode(Node $node, $key): Node
126
    {
127
        // Prevent modifying a collection with an active iterator
128 159
        $this->checkWritable();
129
130
        // Validate arguments
131 159
        $this->checkSubjectNodeHasSameOwner($node);
132 151
        $this->checkSubjectNodeIsOrphan($node);
133
134
        // Update first/last child pointers
135 151
        $this->updateFirstChildIfChanged($node, null);
136 151
        $previousSibling = $this->lastChild;
137 151
        $this->lastChild = $node;
138
139
        // Update next sibling pointer of old $lastChild (no next sibling node to update)
140 151
        $this->setNodeNextSibling($previousSibling, $node);
141
142
        // Add the child to the key map
143 151
        $this->children[$key] = $node;
144
145
        // Set references on new child
146 151
        $node->setReferences($this, $key, $previousSibling, null);
147
148 151
        return $node;
149
    }
150
151
    /**
152
     * @throws WriteOperationForbiddenException
153
     * @throws InvalidSubjectNodeException
154
     * @throws InvalidReferenceNodeException
155
     */
156 34
    protected function insertNode(Node $node, $key, ?Node $before = null): Node
157
    {
158
        // A null $before reference means push the node on to the end of the list
159 34
        if ($before === null) {
160 13
            return $this->appendNode($node, $key);
161
        }
162
163
        // Prevent modifying a collection with an active iterator
164 30
        $this->checkWritable();
165
166
        // Validate arguments
167 27
        $this->checkSubjectNodeHasSameOwner($node);
168 21
        $this->checkSubjectNodeIsOrphan($node);
169 15
        $this->checkReferenceNodeIsChild($before);
170
171
        // Update first child pointer (last child pointer is not affected)
172 14
        $this->updateFirstChildIfChanged($node, $before);
173
174
        // Update next sibling pointer of previous sibling of $before
175 14
        $this->setNodeNextSibling($before->previousSibling, $node);
176
177
        // Replace the child in the key map
178 14
        $this->children[$key] = $node;
179
180
        // Set references on new child
181 14
        $node->setReferences($this, $key, $before->previousSibling, $before);
182
183
        // Update references on ref child
184 14
        $before->setReferences($before->parent, $before->key, $node, $before->nextSibling);
185
186 14
        return $node;
187
    }
188
189
    /**
190
     * @throws WriteOperationForbiddenException
191
     * @throws InvalidSubjectNodeException
192
     * @throws InvalidReferenceNodeException
193
     */
194 21
    protected function replaceNode(Node $newNode, Node $oldNode): Node
195
    {
196
        // Prevent modifying a collection with an active iterator
197 21
        $this->checkWritable();
198
199
        // Validate arguments
200 19
        $this->checkSubjectNodeHasSameOwner($newNode);
201 13
        $this->checkSubjectNodeIsOrphan($newNode);
202 8
        $this->checkReferenceNodeIsChild($oldNode);
203
204
        // Update first/last child pointers
205 7
        $this->updateFirstChildIfChanged($newNode, $oldNode);
206 7
        $this->updateLastChildIfChanged($newNode, $oldNode);
207
208
        // Update sibling pointers of sibling nodes
209 7
        $this->setNodeNextSibling($oldNode->previousSibling, $newNode);
210 7
        $this->setNodePreviousSibling($oldNode->nextSibling, $newNode);
211
212
        // Replace the node in the key map
213 7
        $this->children[$oldNode->key] = $newNode;
214
215
        // Copy references from old node to new node
216 7
        $newNode->setReferences($oldNode->parent, $oldNode->key, $oldNode->previousSibling, $oldNode->nextSibling);
217
218
        // Clear references from old node
219 7
        $oldNode->setReferences(null, null, null, null);
220
221 7
        return $oldNode;
222
    }
223
224
    /**
225
     * @throws WriteOperationForbiddenException
226
     * @throws InvalidSubjectNodeException
227
     */
228 22
    protected function removeNode(Node $node): Node
229
    {
230
        // Prevent modifying a collection with an active iterator
231 22
        $this->checkWritable();
232
233
        // Validate arguments
234 20
        $this->checkSubjectNodeIsChild($node);
235
236
        // Update first/last child pointers
237 20
        $this->updateFirstChildIfChanged($node->nextSibling, $node);
238 20
        $this->updateLastChildIfChanged($node->previousSibling, $node);
239
240
        // Update sibling pointers of sibling nodes
241 20
        $this->setNodeNextSibling($node->previousSibling, $node->nextSibling);
242 20
        $this->setNodePreviousSibling($node->nextSibling, $node->previousSibling);
243
244
        // Remove the node from the key map
245 20
        unset($this->children[$node->key]);
246
247
        // Clear references from node
248 20
        $node->setReferences(null, null, null, null);
249
250 20
        return $node;
251
    }
252
253
    final public function __clone()
254
    {
255
        parent::__clone();
256
257
        // Get an iterator for the original collection's child nodes
258
        $children = $this->firstChild !== null
259
            ? $this->firstChild->parent->getIterator()
0 ignored issues
show
Bug introduced by
The method getIterator() does not exist on null. ( Ignorable by Annotation )

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

259
            ? $this->firstChild->parent->/** @scrutinizer ignore-call */ getIterator()

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...
260
            : [];
261
262
        // Reset the child ref properties
263
        $this->firstChild = null;
264
        $this->lastChild = null;
265
        $this->children = [];
266
        $this->activeIteratorCount = 0;
267
268
        // Loop the original child nodes and append clones of them
269
        foreach ($children as $key => $child) {
270
            try {
271
                $this->appendNode(clone $child, $key);
272
            //@codeCoverageIgnoreStart
273
            } catch (\Exception $e) {
274
                throw unexpected($e);
275
            }
276
            //@codeCoverageIgnoreEnd
277
        }
278
    }
279
280
    final public function hasChildren(): bool
281
    {
282
        return !empty($this->children);
283
    }
284
285 27
    public function containsChild(Node $child): bool
286
    {
287
        do {
288 27
            if ($child->parent === $this) {
289 27
                return true;
290
            }
291 21
        } while (null !== $child = $child->parent);
292
293
        return false;
294
    }
295
296 18
    final public function getFirstChild(): ?Node
297
    {
298 18
        return $this->firstChild;
299
    }
300
301 12
    final public function getLastChild(): ?Node
302
    {
303 12
        return $this->lastChild;
304
    }
305
306
    final public function clear(): void
307
    {
308
        try {
309
            while ($this->lastChild !== null) {
310
                $this->removeNode($this->lastChild);
311
            }
312
        //@codeCoverageIgnoreStart
313
        } catch (\Exception $e) {
314
            throw unexpected($e);
315
        }
316
        //@codeCoverageIgnoreEnd
317
    }
318
319
    final public function getIterator(): NodeListIterator
320
    {
321 9
        return new NodeListIterator($this->firstChild, function($state) {
322 9
            $this->activeIteratorCount += $state;
323 9
            \assert($this->activeIteratorCount >= 0, new \Error('Vector node active iterator count is negative'));
324 9
        });
325
    }
326
327
    final public function count(): int
328
    {
329
        return \count($this->children);
330
    }
331
332
    final public function jsonSerialize(): array
333
    {
334
        return \iterator_to_array($this->getIterator());
335
    }
336
337
    final public function offsetExists($key): bool
338
    {
339
        return isset($this->children[$key]);
340
    }
341
342
    /**
343
     * @throws WriteOperationForbiddenException
344
     * @throws InvalidSubjectNodeException
345
     */
346
    final public function offsetUnset($key): void
347
    {
348
        if (isset($this->children[$key])) {
349
            $this->removeNode($this->children[$key]);
350
        }
351
    }
352
353
    abstract public function toArray(): array;
354
    abstract public function offsetGet($index): Node;
355
    abstract public function offsetSet($index, $value): void;
356
}
357