Completed
Push — master ( 0b0b24...54ff37 )
by Colin
02:37
created

AdjacentTextMerger   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 0
loc 69
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeChildNodes() 0 9 4
A mergeTextNodesBetweenExclusive() 0 9 5
A mergeTextNodesInclusive() 0 25 5
B mergeIfNeeded() 0 20 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\Node\Inline;
16
17
use League\CommonMark\Node\Node;
18
19
/**
20
 * @internal
21
 */
22
final class AdjacentTextMerger
23
{
24 2451
    public static function mergeChildNodes(Node $node): void
25
    {
26
        // No children or just one child node, no need for merging
27 2451
        if ($node->firstChild() === $node->lastChild() || $node->firstChild() === null || $node->lastChild() === null) {
28 1482
            return;
29
        }
30
31 1278
        self::mergeTextNodesInclusive($node->firstChild(), $node->lastChild());
32 1278
    }
33
34 513
    public static function mergeTextNodesBetweenExclusive(Node $fromNode, Node $toNode): void
35
    {
36
        // No nodes between them
37 513
        if ($fromNode === $toNode || $fromNode->next() === $toNode || $fromNode->next() === null || $toNode->previous() === null) {
38 3
            return;
39
        }
40
41 513
        self::mergeTextNodesInclusive($fromNode->next(), $toNode->previous());
42 513
    }
43
44 1458
    private static function mergeTextNodesInclusive(Node $fromNode, Node $toNode): void
45
    {
46 1458
        $first = null;
47 1458
        $last = null;
48
49 1458
        $node = $fromNode;
50 1458
        while ($node !== null) {
51 1458
            if ($node instanceof Text) {
52 1377
                if ($first === null) {
53 1377
                    $first = $node;
54
                }
55 1377
                $last = $node;
56
            } else {
57 1086
                self::mergeIfNeeded($first, $last);
58 1086
                $first = null;
59 1086
                $last = null;
60
            }
61 1458
            if ($node === $toNode) {
62 1458
                break;
63
            }
64 1404
            $node = $node->next();
65
        }
66
67 1458
        self::mergeIfNeeded($first, $last);
68 1458
    }
69
70 1458
    private static function mergeIfNeeded(?Text $first, ?Text $last): void
71
    {
72 1458
        if ($first === null || $last === null || $first === $last) {
73
            // No merging needed
74 1107
            return;
75
        }
76
77 537
        $s = $first->getContent();
78
79 537
        $node = $first->next();
80 537
        $stop = $last->next();
81 537
        while ($node !== $stop && $node instanceof Text) {
82 537
            $s .= $node->getContent();
83 537
            $unlink = $node;
84 537
            $node = $node->next();
85 537
            $unlink->detach();
86
        }
87
88 537
        $first->setContent($s);
89 537
    }
90
}
91