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

HtmlBlockStartParser::isType7BlockAllowed()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

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