Test Setup Failed
Pull Request — latest (#3)
by Mark
34:19
created

Document::replaceNode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 19
rs 9.9332
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file was originally part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace UnicornFail\Emoji\Node;
18
19
class Document
20
{
21
    /** @var array<array-key, Node> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, Node> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, Node>.
Loading history...
22
    protected $nodes = [];
23
24
    public function appendNode(Node $node): void
25
    {
26
        $node->setDocument($this);
27
28
        $this->nodes[] = $node;
29
    }
30
31
    /**
32
     * @return Node[]
33
     */
34
    public function &getNodes(): array
35
    {
36
        return $this->nodes;
37
    }
38
39
    public function prependNode(Node $node): void
40
    {
41
        $node->setDocument($this);
42
43
        \array_unshift($this->nodes, $node);
44
45
        $this->nodes = \array_values($this->nodes);
46
    }
47
48
    public function replaceNode(Node $oldNode, ?Node $newNode = null): void
49
    {
50
        $index = \array_search($oldNode, $this->nodes, true);
51
52
        if ($index === false) {
53
            return;
54
        }
55
56
        $replacement = [];
57
58
        if ($newNode !== null) {
59
            $oldNode->setDocument();
60
            $newNode->setDocument($this);
61
            $replacement[] = $newNode;
62
        }
63
64
        \array_splice($this->nodes, (int) $index, 1, $replacement);
65
66
        $this->nodes = \array_values($this->nodes);
67
    }
68
}
69