AdjacentTextMerger::mergeIfNeeded()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 8.9777
cc 6
nc 3
nop 2
crap 6
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Additional emphasis processing code based on commonmark-java (https://github.com/atlassian/commonmark-java)
9
 *  - (c) Atlassian Pty Ltd
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Inline;
16
17
use League\CommonMark\Inline\Element\Text;
18
use League\CommonMark\Node\Node;
19
20
/**
21
 * @internal
22
 */
23
final class AdjacentTextMerger
24
{
25 2025
    public static function mergeChildNodes(Node $node)
26
    {
27
        // No children or just one child node, no need for merging
28 2025
        if ($node->firstChild() === $node->lastChild() || $node->firstChild() === null || $node->lastChild() === null) {
29 1215
            return;
30
        }
31
32 1032
        self::mergeTextNodesInclusive($node->firstChild(), $node->lastChild());
33 1032
    }
34
35 417
    public static function mergeTextNodesBetweenExclusive(Node $fromNode, Node $toNode)
36
    {
37
        // No nodes between them
38 417
        if ($fromNode === $toNode || $fromNode->next() === $toNode || $fromNode->next() === null || $toNode->previous() === null) {
39 3
            return;
40
        }
41
42 417
        self::mergeTextNodesInclusive($fromNode->next(), $toNode->previous());
43 417
    }
44
45 1206
    private static function mergeTextNodesInclusive(Node $fromNode, Node $toNode)
46
    {
47 1206
        $first = null;
48 1206
        $last = null;
49
50 1206
        $node = $fromNode;
51 1206
        while ($node !== null) {
52 1206
            if ($node instanceof Text) {
53 1176
                if ($first === null) {
54 1176
                    $first = $node;
55
                }
56 1176
                $last = $node;
57
            } else {
58 882
                self::mergeIfNeeded($first, $last);
59 882
                $first = null;
60 882
                $last = null;
61
            }
62 1206
            if ($node === $toNode) {
63 1206
                break;
64
            }
65 1158
            $node = $node->next();
66
        }
67
68 1206
        self::mergeIfNeeded($first, $last);
69 1206
    }
70
71 1206
    private static function mergeIfNeeded(?Text $first, ?Text $last)
72
    {
73 1206
        if ($first === null || $last === null || $first === $last) {
74
            // No merging needed
75 906
            return;
76
        }
77
78 459
        $s = $first->getContent();
79
80 459
        $node = $first->next();
81 459
        $stop = $last->next();
82 459
        while ($node !== $stop && $node instanceof Text) {
83 459
            $s .= $node->getContent();
84 459
            $unlink = $node;
85 459
            $node = $node->next();
86 459
            $unlink->detach();
87
        }
88
89 459
        $first->setContent($s);
90 459
    }
91
}
92