Completed
Push — latest ( 6c0640...3af091 )
by Colin
16s queued 11s
created

LooseDescriptionHandler::__invoke()   B

Complexity

Conditions 9
Paths 15

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 44
ccs 25
cts 25
cp 1
rs 8.0555
cc 9
nc 15
nop 1
crap 9
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\Description;
18
use League\CommonMark\Extension\DescriptionList\Node\DescriptionList;
19
use League\CommonMark\Extension\DescriptionList\Node\DescriptionTerm;
20
use League\CommonMark\Node\Block\Paragraph;
21
use League\CommonMark\Node\Inline\Newline;
22
23
final class LooseDescriptionHandler
24
{
25 42
    public function __invoke(DocumentParsedEvent $event): void
26
    {
27 42
        $walker = $event->getDocument()->walker();
28 42
        while ($e = $walker->next()) {
29
            // Wait until we're exiting a node
30 42
            if ($e->isEntering()) {
31 42
                continue;
32
            }
33
34 42
            $description = $e->getNode();
35 42
            if (! $description instanceof Description) {
36 42
                continue;
37
            }
38
39
            // Does this description need to be added to a list?
40 33
            if (! $description->parent() instanceof DescriptionList) {
41 15
                $list = new DescriptionList();
42
                // Taking any preceding paragraphs with it
43 15
                if (($paragraph = $description->previous()) instanceof Paragraph) {
44 15
                    $list->appendChild($paragraph);
45
                }
46
47 15
                $description->replaceWith($list);
48 15
                $list->appendChild($description);
49
            }
50
51
            // Is this description preceded by a paragraph that should really be a term?
52 33
            if (! (($paragraph = $description->previous()) instanceof Paragraph)) {
53 30
                continue;
54
            }
55
56
            // Convert the paragraph into one or more terms
57 15
            $term = new DescriptionTerm();
58 15
            $paragraph->replaceWith($term);
59
60 15
            foreach ($paragraph->children() as $child) {
61 15
                if ($child instanceof Newline) {
62 3
                    $newTerm = new DescriptionTerm();
63 3
                    $term->insertAfter($newTerm);
64 3
                    $term = $newTerm;
65 3
                    continue;
66
                }
67
68 15
                $term->appendChild($child);
69
            }
70
        }
71 42
    }
72
}
73