Passed
Push — commonmark-spec-0.31 ( 6bbcc4 )
by Colin
03:10
created

DocumentBlockParser   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 53
ccs 19
cts 21
cp 0.9048
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A canContain() 0 3 1
A __construct() 0 3 1
A removeLinkReferenceDefinitions() 0 18 6
A closeBlock() 0 3 1
A tryContinue() 0 3 1
A getBlock() 0 3 1
A isContainer() 0 3 1
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\Parser\Block;
15
16
use League\CommonMark\Node\Block\AbstractBlock;
17
use League\CommonMark\Node\Block\Document;
18
use League\CommonMark\Node\Block\Paragraph;
19
use League\CommonMark\Parser\Cursor;
20
use League\CommonMark\Reference\ReferenceMapInterface;
21
22
/**
23
 * Parser implementation which ensures everything is added to the root-level Document
24
 */
25
final class DocumentBlockParser extends AbstractBlockContinueParser
26
{
27
    /** @psalm-readonly */
28
    private Document $document;
29
30 2260
    public function __construct(ReferenceMapInterface $referenceMap)
31
    {
32 2260
        $this->document = new Document($referenceMap);
33
    }
34
35 2260
    public function getBlock(): Document
36
    {
37 2260
        return $this->document;
38
    }
39
40 2244
    public function isContainer(): bool
41
    {
42 2244
        return true;
43
    }
44
45 2244
    public function canContain(AbstractBlock $childBlock): bool
46
    {
47 2244
        return true;
48
    }
49
50
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
51
    {
52
        return BlockContinue::at($cursor);
53
    }
54
55 2244
    public function closeBlock(): void
56
    {
57 2244
        $this->removeLinkReferenceDefinitions();
58
    }
59
60 2244
    private function removeLinkReferenceDefinitions(): void
61
    {
62 2244
        $emptyNodes = [];
63
64 2244
        $walker = $this->document->walker();
65 2244
        while ($event = $walker->next()) {
66 2244
            $node = $event->getNode();
67
            // TODO for v3: It would be great if we could find an alternate way to identify such paragraphs.
68
            // Unfortunately, we can't simply check for empty paragraphs here because inlines haven't been processed yet,
69
            // meaning all paragraphs will appear blank here, and we don't have a way to check the status of the reference parser
70
            // which is attached to the (already-closed) paragraph parser.
71 2244
            if ($event->isEntering() && $node instanceof Paragraph && $node->onlyContainsLinkReferenceDefinitions) {
72 158
                $emptyNodes[] = $node;
73
            }
74
        }
75
76 2244
        foreach ($emptyNodes as $node) {
77 158
            $node->detach();
78
        }
79
    }
80
}
81