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

EmailAutolinkParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getMatchDefinition() 0 3 1
A parse() 0 16 3
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\Autolink;
15
16
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
17
use League\CommonMark\Parser\Inline\InlineParserInterface;
18
use League\CommonMark\Parser\Inline\InlineParserMatch;
19
use League\CommonMark\Parser\InlineParserContext;
20
21
final class EmailAutolinkParser implements InlineParserInterface
22
{
23
    private const REGEX = '[A-Za-z0-9.\-_+]+@[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_.]+';
24
25 234
    public function getMatchDefinition(): InlineParserMatch
26
    {
27 234
        return InlineParserMatch::regex(self::REGEX);
28
    }
29
30 33
    public function parse(string $match, InlineParserContext $inlineContext): bool
31
    {
32
        // The last character cannot be - or _
33 33
        if (\in_array(\substr($match, -1), ['-', '_'], true)) {
34 9
            return false;
35
        }
36
37
        // Does the URL end with punctuation that should be stripped?
38 27
        if (\substr($match, -1) === '.') {
39 6
            $match = \substr($match, 0, -1);
40
        }
41
42 27
        $inlineContext->getCursor()->advanceBy(\strlen($match));
43 27
        $inlineContext->getContainer()->appendChild(new Link('mailto:' . $match, $match));
44
45 27
        return true;
46
    }
47
}
48