Completed
Push — 0.18-dev ( c34984 )
by Colin
09:09 queued 07:58
created

AdjoiningTextCollapser::collapseTextNodes()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 14
cts 14
cp 1
rs 9.4222
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 5
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 1665
    public static function collapseTextNodes(Node $container)
28
    {
29 1665
        $walker = $container->walker();
30 1665
        while (($event = $walker->next()) !== null) {
31 1665
            if ($event->isEntering()) {
32 1665
                $node = $event->getNode();
33 1665
                $next = $node->next();
34 1665
                if ($node instanceof Text && $next instanceof Text) {
35 390
                    $node->append($next->getContent());
36 390
                    $next->detach();
37
                    // Re-start the next `while` iteration at the same spot as before
38 390
                    $walker->resumeAt($node, true);
39 130
                }
40 555
            }
41 555
        }
42 1665
    }
43
}
44