SkipLinesStartingWithLettersParser   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 11
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 5
c 1
b 0
f 0
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A tryStart() 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\Parser\Block;
15
16
use League\CommonMark\Parser\Cursor;
17
use League\CommonMark\Parser\MarkdownParserStateInterface;
18
use League\CommonMark\Util\RegexHelper;
19
20
/**
21
 * @internal
22
 *
23
 * This "parser" is actually a performance optimization.
24
 *
25
 * Most lines in a typical Markdown document probably won't match a block start. This is especially true for lines starting
26
 * with letters - nothing in the core CommonMark spec or our supported extensions will match those lines as blocks. Therefore,
27
 * if we can identify those lines and skip block start parsing, we can optimize performance by ~10%.
28
 *
29
 * Previously this optimization was hard-coded in the MarkdownParser but did not allow users to override this behavior.
30
 * By implementing this optimization as a block parser instead, users wanting custom blocks starting with letters
31
 * can instead register their block parser with a higher priority to ensure their parser is always called first.
32
 */
33
final class SkipLinesStartingWithLettersParser implements BlockStartParserInterface
34
{
35 2304
    public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
36
    {
37 2304
        if (! $cursor->isIndented() && RegexHelper::isLetter($cursor->getNextNonSpaceCharacter())) {
38 1122
            $cursor->advanceToNextNonSpaceOrTab();
39
40 1122
            return BlockStart::abort();
41
        }
42
43 1812
        return BlockStart::none();
44
    }
45
}
46