EmailAutolinkParser   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getMatchDefinition() 0 3 1
A parse() 0 17 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(InlineParserContext $inlineContext): bool
31
    {
32 33
        $email = $inlineContext->getFullMatch();
33
        // The last character cannot be - or _
34 33
        if (\in_array(\substr($email, -1), ['-', '_'], true)) {
35 9
            return false;
36
        }
37
38
        // Does the URL end with punctuation that should be stripped?
39 27
        if (\substr($email, -1) === '.') {
40 6
            $email = \substr($email, 0, -1);
41
        }
42
43 27
        $inlineContext->getCursor()->advanceBy(\strlen($email));
44 27
        $inlineContext->getContainer()->appendChild(new Link('mailto:' . $email, $email));
45
46 27
        return true;
47
    }
48
}
49