Completed
Push — latest ( 76d169...995567 )
by Colin
22s queued 10s
created

AnonymousFootnoteRefParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 1
b 0
f 0
dl 0
loc 54
ccs 20
cts 21
cp 0.9524
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfiguration() 0 3 1
A parse() 0 12 2
A getMatchDefinition() 0 3 1
A createReference() 0 9 1
A __construct() 0 3 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\Configuration\ConfigurationAwareInterface;
18
use League\CommonMark\Configuration\ConfigurationInterface;
19
use League\CommonMark\Extension\Footnote\Node\FootnoteRef;
20
use League\CommonMark\Normalizer\SlugNormalizer;
21
use League\CommonMark\Normalizer\TextNormalizerInterface;
22
use League\CommonMark\Parser\Inline\InlineParserInterface;
23
use League\CommonMark\Parser\Inline\InlineParserMatch;
24
use League\CommonMark\Parser\InlineParserContext;
25
use League\CommonMark\Reference\Reference;
26
27
final class AnonymousFootnoteRefParser implements InlineParserInterface, ConfigurationAwareInterface
28
{
29
    /** @var ConfigurationInterface */
30
    private $config;
31
32
    /**
33
     * @var TextNormalizerInterface
34
     *
35
     * @psalm-readonly
36
     */
37
    private $slugNormalizer;
38
39 105
    public function __construct()
40
    {
41 105
        $this->slugNormalizer = new SlugNormalizer();
42 105
    }
43
44 105
    public function getMatchDefinition(): InlineParserMatch
45
    {
46 105
        return InlineParserMatch::regex('\^\[[^\]]+\]');
47
    }
48
49 21
    public function parse(string $match, InlineParserContext $inlineContext): bool
50
    {
51 21
        if (\preg_match('#\^\[([^\]]+)\]#', $match, $matches) <= 0) {
52
            return false;
53
        }
54
55 21
        $inlineContext->getCursor()->advanceBy(\mb_strlen($match));
56
57 21
        $reference = $this->createReference($matches[1]);
58 21
        $inlineContext->getContainer()->appendChild(new FootnoteRef($reference, $matches[1]));
59
60 21
        return true;
61
    }
62
63
    /**
64
     * @psalm-immutable
65
     */
66 21
    private function createReference(string $label): Reference
67
    {
68 21
        $refLabel = $this->slugNormalizer->normalize($label);
69 21
        $refLabel = \mb_substr($refLabel, 0, 20);
70
71 21
        return new Reference(
72 14
            $refLabel,
73 21
            '#' . $this->config->get('footnote/footnote_id_prefix', 'fn:') . $refLabel,
74 7
            $label
75
        );
76
    }
77
78 105
    public function setConfiguration(ConfigurationInterface $config): void
79
    {
80 105
        $this->config = $config;
81 105
    }
82
}
83