GithubPullRequestParser   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 35
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 24 1
A getMatchDefinition() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Markdown\GithubPullRequest;
6
7
use League\CommonMark\Parser\Inline\InlineParserInterface;
8
use League\CommonMark\Parser\Inline\InlineParserMatch;
9
use League\CommonMark\Parser\InlineParserContext;
10
11
use function mb_strlen;
12
13
final class GithubPullRequestParser implements InlineParserInterface
14
{
15
    // Regex used to match Github pull request link.
16
    public const REGEXP_PURLLREQUEST = '\bhttps?:\/\/github\.com\/(?<repo>[\w-]+\/[\w-]+)\/'.
17
        '(?<type>issues|pull)\/(?<issue>\d+)';
18
19
    public function getMatchDefinition(): InlineParserMatch
20
    {
21
        return InlineParserMatch::regex(self::REGEXP_PURLLREQUEST);
22
    }
23
24
    public function parse(InlineParserContext $inlineContext): bool
25
    {
26
        $route = $inlineContext->getFullMatch();
27
28
        // Push the cursor to the lenght of the full match.
29
        $inlineContext->getCursor()->advanceBy(mb_strlen($route));
30
31
        $content = "{$inlineContext->getMatches()[1]}#{$inlineContext->getMatches()[5]}";
32
33
34
        $inlineContext->getContainer()
35
            ->appendChild(new GithubPullRequest(
36
                $content,
37
                [
38
                    'attributes' => [
39
                        'href' => $route,
40
                        'target' => '_blank',
41
                        'class' => 'link link-hover link-primary',
42
                        'title' => $content
43
                    ]
44
                ]
45
            ));
46
47
        return true;
48
    }
49
}
50