Passed
Push — latest ( 212a66...171a44 )
by Colin
01:50
created

FootnoteRefParser::parse()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 9.8333
cc 4
nc 4
nop 1
crap 4
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\Extension\Footnote\Node\FootnoteRef;
18
use League\CommonMark\Parser\Inline\InlineParserInterface;
19
use League\CommonMark\Parser\InlineParserContext;
20
use League\CommonMark\Reference\Reference;
21
22
final class FootnoteRefParser implements InlineParserInterface
23
{
24
    /**
25
     * {@inheritDoc}
26
     */
27 90
    public function getCharacters(): array
28
    {
29 90
        return ['['];
30
    }
31
32 75
    public function parse(InlineParserContext $inlineContext): bool
33
    {
34 75
        $container = $inlineContext->getContainer();
35 75
        $cursor    = $inlineContext->getCursor();
36 75
        $nextChar  = $cursor->peek();
37 75
        if ($nextChar !== '^') {
38 18
            return false;
39
        }
40
41 63
        $state = $cursor->saveState();
42
43 63
        $m = $cursor->match('#\[\^([^\s\]]+)\]#');
44 63
        if ($m !== null) {
45 60
            if (\preg_match('#\[\^([^\s\]]+)\]#', $m, $matches) > 0) {
46 60
                $container->appendChild(new FootnoteRef($this->createReference($matches[1])));
47
48 60
                return true;
49
            }
50
        }
51
52 3
        $cursor->restoreState($state);
53
54 3
        return false;
55
    }
56
57 60
    private function createReference(string $label): Reference
58
    {
59 60
        return new Reference($label, '#fn:' . $label, $label);
60
    }
61
}
62