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

EmailAutolinkParser::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
c 1
b 0
f 1
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 10
cc 3
nc 3
nop 2
crap 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