Completed
Push — master ( 990cee...3d5add )
by Colin
13s queued 11s
created

AdjacentTextMerger::mergeChildNodes()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 4
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 2028
    public static function mergeChildNodes(Node $node)
26
    {
27
        // No children or just one child node, no need for merging
28 2028
        if ($node->firstChild() === $node->lastChild() || $node->firstChild() === null || $node->lastChild() === null) {
29 1218
            return;
30
        }
31
32 1038
        self::mergeTextNodesInclusive($node->firstChild(), $node->lastChild());
33 1038
    }
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 1212
    private static function mergeTextNodesInclusive(Node $fromNode, Node $toNode)
46
    {
47 1212
        $first = null;
48 1212
        $last = null;
49
50 1212
        $node = $fromNode;
51 1212
        while ($node !== null) {
52 1212
            if ($node instanceof Text) {
53 1182
                if ($first === null) {
54 1182
                    $first = $node;
55
                }
56 1182
                $last = $node;
57
            } else {
58 888
                self::mergeIfNeeded($first, $last);
59 888
                $first = null;
60 888
                $last = null;
61
            }
62 1212
            if ($node === $toNode) {
63 1212
                break;
64
            }
65 1164
            $node = $node->next();
66
        }
67
68 1212
        self::mergeIfNeeded($first, $last);
69 1212
    }
70
71 1212
    private static function mergeIfNeeded(?Text $first, ?Text $last)
72
    {
73 1212
        if ($first === null || $last === null || $first === $last) {
74
            // No merging needed
75 912
            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