Completed
Push — master ( 4475f0...d6425f )
by Colin
102:20 queued 70:50
created

AdjoiningTextCollapser::collapseTextNodes()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4222
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\CommonMark\Inline;
13
14
use League\CommonMark\Inline\Element\Text;
15
use League\CommonMark\Node\Node;
16
17
/**
18
 * @internal
19
 */
20
final class AdjoiningTextCollapser
21
{
22
    /**
23
     * @param Node $container
24
     *
25
     * @internal
26
     */
27
    public static function collapseTextNodes(Node $container)
28
    {
29
        $walker = $container->walker();
30
        while (($event = $walker->next()) !== null) {
31
            if ($event->isEntering()) {
32
                $node = $event->getNode();
33
                $next = $node->next();
34
                if ($node instanceof Text && $next instanceof Text) {
35
                    $node->append($next->getContent());
36
                    $next->detach();
37
                    // Re-start the next `while` iteration at the same spot as before
38
                    $walker->resumeAt($node, true);
39
                }
40
            }
41
        }
42
    }
43
}
44