Passed
Push — main ( 8bb0e7...efbdfd )
by Colin
05:29 queued 03:15
created

HtmlBlockStartParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 16
c 1
b 0
f 0
dl 0
loc 37
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B tryStart() 0 24 7
A isType7BlockAllowed() 0 9 3
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\CommonMark\Parser\Block;
15
16
use League\CommonMark\Extension\CommonMark\Node\Block\HtmlBlock;
17
use League\CommonMark\Node\Block\Paragraph;
18
use League\CommonMark\Parser\Block\BlockStart;
19
use League\CommonMark\Parser\Block\BlockStartParserInterface;
20
use League\CommonMark\Parser\Cursor;
21
use League\CommonMark\Parser\MarkdownParserStateInterface;
22
use League\CommonMark\Util\RegexHelper;
23
24
final class HtmlBlockStartParser implements BlockStartParserInterface
25
{
26 2004
    public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
27
    {
28 2004
        if ($cursor->isIndented() || $cursor->getNextNonSpaceCharacter() !== '<') {
29 1755
            return BlockStart::none();
30
        }
31
32 312
        $tmpCursor = clone $cursor;
33 312
        $tmpCursor->advanceToNextNonSpaceOrTab();
34 312
        $line = $tmpCursor->getRemainder();
35
36 312
        for ($blockType = 1; $blockType <= 7; $blockType++) {
37
            /** @psalm-var HtmlBlock::TYPE_* $blockType */
38
            /** @phpstan-var HtmlBlock::TYPE_* $blockType */
39 312
            $match = RegexHelper::matchAt(
40 312
                RegexHelper::getHtmlBlockOpenRegex($blockType),
41
                $line
42
            );
43
44 312
            if ($match !== null && ($blockType < 7 || $this->isType7BlockAllowed($cursor, $parserState))) {
45 177
                return BlockStart::of(new HtmlBlockParser($blockType))->at($cursor);
46
            }
47
        }
48
49 150
        return BlockStart::none();
50
    }
51
52 45
    private function isType7BlockAllowed(Cursor $cursor, MarkdownParserStateInterface $parserState): bool
53
    {
54
        // Type 7 blocks can't interrupt paragraphs
55 45
        if ($parserState->getLastMatchedBlockParser()->getBlock() instanceof Paragraph) {
56 12
            return false;
57
        }
58
59
        // Even lazy ones
60 33
        return $cursor->isBlank() || ! $parserState->getActiveBlockParser()->canHaveLazyContinuationLines();
61
    }
62
}
63