Completed
Push — latest ( da8ccb...1f5488 )
by Colin
15s queued 12s
created

AnonymousFootnoteRefParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 16
c 1
b 0
f 0
dl 0
loc 43
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setEnvironment() 0 4 1
A parse() 0 9 1
A getMatchDefinition() 0 3 1
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\Configuration\ConfigurationInterface;
18
use League\CommonMark\Environment\EnvironmentAwareInterface;
19
use League\CommonMark\Environment\EnvironmentInterface;
20
use League\CommonMark\Extension\Footnote\Node\FootnoteRef;
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, EnvironmentAwareInterface
28
{
29
    /** @var ConfigurationInterface */
30
    private $config;
31
32
    /**
33
     * @var TextNormalizerInterface
34
     *
35
     * @psalm-readonly-allow-private-mutation
36
     */
37
    private $slugNormalizer;
38
39 105
    public function getMatchDefinition(): InlineParserMatch
40
    {
41 105
        return InlineParserMatch::regex('\^\[([^\]]+)\]');
42
    }
43
44 21
    public function parse(InlineParserContext $inlineContext): bool
45
    {
46 21
        $inlineContext->getCursor()->advanceBy($inlineContext->getFullMatchLength());
47
48 21
        [$label]   = $inlineContext->getSubMatches();
49 21
        $reference = $this->createReference($label);
50 21
        $inlineContext->getContainer()->appendChild(new FootnoteRef($reference, $label));
51
52 21
        return true;
53
    }
54
55 21
    private function createReference(string $label): Reference
56
    {
57 21
        $refLabel = $this->slugNormalizer->normalize($label, ['length' => 20]);
58
59 21
        return new Reference(
60 14
            $refLabel,
61 21
            '#' . $this->config->get('footnote/footnote_id_prefix') . $refLabel,
62 7
            $label
63
        );
64
    }
65
66 105
    public function setEnvironment(EnvironmentInterface $environment): void
67
    {
68 105
        $this->config         = $environment->getConfiguration();
69 105
        $this->slugNormalizer = $environment->getSlugNormalizer();
70 105
    }
71
}
72