ParagraphParser   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 18
dl 0
loc 75
ccs 28
cts 28
cp 1
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getBlock() 0 3 1
A closeBlock() 0 4 3
A parseInlines() 0 5 2
A getReferences() 0 3 1
A tryContinue() 0 7 2
A addLine() 0 3 1
A getContentString() 0 3 1
A canHaveLazyContinuationLines() 0 3 1
A __construct() 0 4 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\Paragraph;
18
use League\CommonMark\Parser\Cursor;
19
use League\CommonMark\Parser\InlineParserEngineInterface;
20
use League\CommonMark\Reference\ReferenceInterface;
21
use League\CommonMark\Reference\ReferenceParser;
22
23
final class ParagraphParser extends AbstractBlockContinueParser implements BlockContinueParserWithInlinesInterface
24
{
25
    /**
26
     * @var Paragraph
27
     *
28
     * @psalm-readonly
29
     */
30
    private $block;
31
32
    /**
33
     * @var ReferenceParser
34
     *
35
     * @psalm-readonly
36
     */
37
    private $referenceParser;
38
39 2604
    public function __construct()
40
    {
41 2604
        $this->block           = new Paragraph();
42 2604
        $this->referenceParser = new ReferenceParser();
43 2604
    }
44
45 414
    public function canHaveLazyContinuationLines(): bool
46
    {
47 414
        return true;
48
    }
49
50
    /**
51
     * @return Paragraph
52
     */
53 2604
    public function getBlock(): AbstractBlock
54
    {
55 2604
        return $this->block;
56
    }
57
58 1047
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
59
    {
60 1047
        if ($cursor->isBlank()) {
61 645
            return BlockContinue::none();
62
        }
63
64 576
        return BlockContinue::at($cursor);
65
    }
66
67 2604
    public function addLine(string $line): void
68
    {
69 2604
        $this->referenceParser->parse($line);
70 2604
    }
71
72 2496
    public function closeBlock(): void
73
    {
74 2496
        if ($this->referenceParser->hasReferences() && $this->referenceParser->getParagraphContent() === '') {
75 225
            $this->block->detach();
76
        }
77 2496
    }
78
79 2496
    public function parseInlines(InlineParserEngineInterface $inlineParser): void
80
    {
81 2496
        $content = $this->getContentString();
82 2496
        if ($content !== '') {
83 2490
            $inlineParser->parse($content, $this->block);
84
        }
85 2496
    }
86
87 2604
    public function getContentString(): string
88
    {
89 2604
        return $this->referenceParser->getParagraphContent();
90
    }
91
92
    /**
93
     * @return ReferenceInterface[]
94
     */
95 2604
    public function getReferences(): iterable
96
    {
97 2604
        return $this->referenceParser->getReferences();
98
    }
99
}
100