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

DocumentBlockParser::closeBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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