Passed
Push — 2.3 ( c49358...0d523d )
by Colin
12:44
created

FootnoteStartParser::tryStart()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.0035

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 4
nop 2
dl 0
loc 29
ccs 18
cts 19
cp 0.9474
crap 5.0035
rs 9.3888
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 * (c) Rezo Zero / Ambroise Maupate
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace League\CommonMark\Extension\Footnote\Parser;
16
17
use League\CommonMark\Parser\Block\BlockStart;
18
use League\CommonMark\Parser\Block\BlockStartParserInterface;
19
use League\CommonMark\Parser\Cursor;
20
use League\CommonMark\Parser\MarkdownParserStateInterface;
21
use League\CommonMark\Reference\Reference;
22
use League\CommonMark\Util\RegexHelper;
23
24
final class FootnoteStartParser implements BlockStartParserInterface
25
{
26 68
    public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
27
    {
28 68
        if ($cursor->isIndented() || $parserState->getLastMatchedBlockParser()->canHaveLazyContinuationLines()) {
29 8
            return BlockStart::none();
30
        }
31
32 68
        $match = RegexHelper::matchFirst(
33 68
            '/^\[\^([^\s^\]]+)\]\:(?:\s|$)/',
34 68
            $cursor->getLine(),
35 68
            $cursor->getNextNonSpacePosition()
36 68
        );
37
38 68
        if (! $match) {
39 10
            return BlockStart::none();
40
        }
41
42 66
        $cursor->advanceToNextNonSpaceOrTab();
43 66
        $cursor->advanceBy(\strlen($match[0]));
44 66
        $str = $cursor->getRemainder();
45 66
        \preg_replace('/^\[\^([^\s^\]]+)\]\:(?:\s|$)/', '', $str);
46
47 66
        if (\preg_match('/^\[\^([^\s^\]]+)\]\:(?:\s|$)/', $match[0], $matches) !== 1) {
48
            return BlockStart::none();
49
        }
50
51 66
        $reference      = new Reference($matches[1], $matches[1], $matches[1]);
52 66
        $footnoteParser = new FootnoteParser($reference);
53
54 66
        return BlockStart::of($footnoteParser)->at($cursor);
55
    }
56
}
57