Completed
Push — spec-next ( 6d269d...4246ae )
by Colin
171:59 queued 137:00
created

AdjoiningTextCollapser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A collapseTextNodes() 0 16 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
    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