MentionParser   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 25
c 1
b 0
f 0
dl 0
loc 79
ccs 26
cts 26
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createWithCallback() 0 3 1
A __construct() 0 6 1
A createWithStringTemplate() 0 3 1
A parse() 0 23 4
A getMatchDefinition() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\Mention;
15
16
use League\CommonMark\Extension\Mention\Generator\CallbackGenerator;
17
use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface;
18
use League\CommonMark\Extension\Mention\Generator\StringTemplateLinkGenerator;
19
use League\CommonMark\Parser\Inline\InlineParserInterface;
20
use League\CommonMark\Parser\Inline\InlineParserMatch;
21
use League\CommonMark\Parser\InlineParserContext;
22
23
final class MentionParser implements InlineParserInterface
24
{
25
    /**
26
     * @var string
27
     *
28
     * @psalm-readonly
29
     */
30
    private $name;
31
32
    /**
33
     * @var string
34
     *
35
     * @psalm-readonly
36
     */
37
    private $prefix;
38
39
    /**
40
     * @var string
41
     *
42
     * @psalm-readonly
43
     */
44
    private $identifierPattern;
45
46
    /**
47
     * @var MentionGeneratorInterface
48
     *
49
     * @psalm-readonly
50
     */
51
    private $mentionGenerator;
52
53 39
    public function __construct(string $name, string $prefix, string $identifierPattern, MentionGeneratorInterface $mentionGenerator)
54
    {
55 39
        $this->name              = $name;
56 39
        $this->prefix            = $prefix;
57 39
        $this->identifierPattern = $identifierPattern;
58 39
        $this->mentionGenerator  = $mentionGenerator;
59 39
    }
60
61 39
    public function getMatchDefinition(): InlineParserMatch
62
    {
63 39
        return InlineParserMatch::join(
64 39
            InlineParserMatch::string($this->prefix),
65 39
            InlineParserMatch::regex($this->identifierPattern)
66
        );
67
    }
68
69 33
    public function parse(InlineParserContext $inlineContext): bool
70
    {
71 33
        $cursor = $inlineContext->getCursor();
72
73
        // The prefix must not have any other characters immediately prior
74 33
        $previousChar = $cursor->peek(-1);
75 33
        if ($previousChar !== null && \preg_match('/\w/', $previousChar)) {
76
            // peek() doesn't modify the cursor, so no need to restore state first
77 3
            return false;
78
        }
79
80 30
        [$prefix, $identifier] = $inlineContext->getSubMatches();
81
82 30
        $mention = $this->mentionGenerator->generateMention(new Mention($this->name, $prefix, $identifier));
83
84 30
        if ($mention === null) {
85 3
            return false;
86
        }
87
88 27
        $cursor->advanceBy($inlineContext->getFullMatchLength());
89 27
        $inlineContext->getContainer()->appendChild($mention);
90
91 27
        return true;
92
    }
93
94 3
    public static function createWithStringTemplate(string $name, string $prefix, string $mentionRegex, string $urlTemplate): MentionParser
95
    {
96 3
        return new self($name, $prefix, $mentionRegex, new StringTemplateLinkGenerator($urlTemplate));
97
    }
98
99 9
    public static function createWithCallback(string $name, string $prefix, string $mentionRegex, callable $callback): MentionParser
100
    {
101 9
        return new self($name, $prefix, $mentionRegex, new CallbackGenerator($callback));
102
    }
103
}
104