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

HtmlBlockStartParser::tryStart()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 24
ccs 12
cts 12
cp 1
rs 8.8333
c 1
b 0
f 0
cc 7
nc 4
nop 2
crap 7
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