Completed
Push — 1.5 ( f40f61...09e907 )
by Colin
02:28
created

FootnoteParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 40
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 30 4
A createFootnote() 0 6 1
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\Block\Parser\BlockParserInterface;
18
use League\CommonMark\ContextInterface;
19
use League\CommonMark\Cursor;
20
use League\CommonMark\Extension\Footnote\Node\Footnote;
21
use League\CommonMark\Reference\Reference;
22
use League\CommonMark\Util\RegexHelper;
23
24
final class FootnoteParser implements BlockParserInterface
25
{
26 42
    public function parse(ContextInterface $context, Cursor $cursor): bool
27
    {
28 42
        if ($cursor->isIndented()) {
29
            return false;
30
        }
31
32 42
        $match = RegexHelper::matchAll(
33 42
            '/^\[\^([^\n^\]]+)\]\:\s/',
34 42
            $cursor->getLine(),
35 42
            $cursor->getNextNonSpacePosition()
36
        );
37
38 42
        if (!$match) {
39 42
            return false;
40
        }
41
42 18
        $cursor->advanceToNextNonSpaceOrTab();
43 18
        $cursor->advanceBy(\strlen($match[0]));
44 18
        $str = $cursor->getRemainder();
45 18
        \preg_replace('/^\[\^([^\n^\]]+)\]\:\s/', '', $str);
46
47 18
        if (\preg_match('/^\[\^([^\n^\]]+)\]\:\s/', $match[0], $matches) > 0) {
48 18
            $context->addBlock($this->createFootnote($matches[1]));
49 18
            $context->setBlocksParsed(true);
50
51 18
            return true;
52
        }
53
54
        return false;
55
    }
56
57 18
    private function createFootnote(string $label): Footnote
58
    {
59 18
        return new Footnote(
60 18
            new Reference($label, $label, $label)
61
        );
62
    }
63
}
64