Completed
Push — 2.0 ( 3af091 )
by Colin
13s queued 11s
created

ConsecutiveDescriptionListMerger::__invoke()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 26
ccs 14
cts 14
cp 1
rs 9.2222
cc 6
nc 6
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\DescriptionList\Event;
15
16
use League\CommonMark\Event\DocumentParsedEvent;
17
use League\CommonMark\Extension\DescriptionList\Node\DescriptionList;
18
19
final class ConsecutiveDescriptionListMerger
20
{
21 42
    public function __invoke(DocumentParsedEvent $event): void
22
    {
23 42
        $walker = $event->getDocument()->walker();
24 42
        while ($e = $walker->next()) {
25
            // Wait until we're exiting a description list node
26 42
            if ($e->isEntering()) {
27 42
                continue;
28
            }
29
30 42
            $node = $e->getNode();
31 42
            if (! $node instanceof DescriptionList) {
32 42
                continue;
33
            }
34
35 33
            if (! ($next = $node->next()) instanceof DescriptionList) {
36 33
                continue;
37
            }
38
39
            // There's another description list next; merge it into the current one
40 33
            foreach ($next->children() as $child) {
41 33
                $node->appendChild($child);
42
            }
43
44 33
            $next->detach();
45
46 33
            $walker->resumeAt($node, false);
47
        }
48 42
    }
49
}
50