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

AnonymousFootnoteRefParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 5
dl 0
loc 41
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCharacters() 0 4 1
A parse() 0 24 4
A createReference() 0 8 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\Extension\Footnote\Node\FootnoteRef;
18
use League\CommonMark\Inline\Parser\InlineParserInterface;
19
use League\CommonMark\InlineParserContext;
20
use League\CommonMark\Reference\Reference;
21
22
final class AnonymousFootnoteRefParser implements InlineParserInterface
23
{
24 42
    public function getCharacters(): array
25
    {
26 42
        return ['^'];
27
    }
28
29 18
    public function parse(InlineParserContext $inlineContext): bool
30
    {
31 18
        $container = $inlineContext->getContainer();
32 18
        $cursor = $inlineContext->getCursor();
33 18
        $nextChar = $cursor->peek();
34 18
        if ($nextChar !== '[') {
35
            return false;
36
        }
37 18
        $state = $cursor->saveState();
38
39 18
        $m = $cursor->match('/\^\[[^\n^\]]+\]/');
40 18
        if ($m !== null) {
41 18
            if (\preg_match('#\^\[([^\]]+)\]#', $m, $matches) > 0) {
42 18
                $reference = $this->createReference($matches[1]);
43 18
                $container->appendChild(new FootnoteRef($reference, $matches[1]));
44
45 18
                return true;
46
            }
47
        }
48
49
        $cursor->restoreState($state);
50
51
        return false;
52
    }
53
54 18
    private function createReference(string $label): Reference
55
    {
56 18
        $refLabel = Reference::normalizeReference($label);
57 18
        $refLabel = \mb_strtolower(\str_replace(' ', '-', $refLabel));
58 18
        $refLabel = \substr($refLabel, 0, 20);
59
60 18
        return new Reference($refLabel, '#fn:' . $refLabel, $label);
61
    }
62
}
63