GithubCommitParser   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getMatchDefinition() 0 3 1
A parse() 0 26 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Markdown\GithubCommit;
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 GithubCommitParser implements InlineParserInterface
14
{
15
    // Regex used to match Github commit link.
16
    public const REGEXP_COMMIT = '\bhttps?:\/\/github\.com\/(?<repo>[\w-]+\/[\w-]+)\/commit\/(?<commit>[0-9a-f]{7,40})';
17
18
    public function getMatchDefinition(): InlineParserMatch
19
    {
20
        return InlineParserMatch::regex(self::REGEXP_COMMIT);
21
    }
22
23
    public function parse(InlineParserContext $inlineContext): bool
24
    {
25
        $route = $inlineContext->getFullMatch();
26
27
        // Push the cursor to the lenght of the full match.
28
        $inlineContext->getCursor()->advanceBy(mb_strlen($route));
29
30
        $commit = mb_substr($inlineContext->getMatches()[3], 0, 7);
31
32
        $content = "{$inlineContext->getMatches()[1]}@{$commit}";
33
34
35
        $inlineContext->getContainer()
36
            ->appendChild(new GithubCommit(
37
                $content,
38
                [
39
                    'attributes' => [
40
                        'href' => $route,
41
                        'target' => '_blank',
42
                        'class' => 'link link-hover link-primary',
43
                        'title' => $content
44
                    ]
45
                ]
46
            ));
47
48
        return true;
49
    }
50
}
51