Document::getNodes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 League\Emoji\Node;
18
19
class Document
20
{
21
    /** @var array<int, Node> */
22
    protected $nodes = [];
23
24 249
    public function appendNode(Node $node): void
25
    {
26 249
        $node->setDocument($this);
27
28 249
        $this->nodes[] = $node;
29 249
    }
30
31
    /**
32
     * @return Node[]
33
     */
34 255
    public function &getNodes(): array
35
    {
36 255
        return $this->nodes;
37
    }
38
39 3
    public function prependNode(Node $node): void
40
    {
41 3
        $node->setDocument($this);
42
43 3
        \array_unshift($this->nodes, $node);
44
45 3
        $this->nodes = \array_values($this->nodes);
46 3
    }
47
48 24
    public function replaceNode(Node $oldNode, ?Node $newNode = null): void
49
    {
50 24
        $index = \array_search($oldNode, $this->nodes, true);
51
52 24
        if ($index === false) {
53 3
            return;
54
        }
55
56 24
        $replacement = [];
57
58 24
        if ($newNode !== null) {
59 24
            $oldNode->setDocument();
60 24
            $newNode->setDocument($this);
61 24
            $replacement[] = $newNode;
62
        }
63
64 24
        \array_splice($this->nodes, /** @scrutinizer ignore-type */ $index, 1, $replacement);
65
66 24
        $this->nodes = \array_values($this->nodes);
67 24
    }
68
}
69