Completed
Push — master ( afd04b...3b4c22 )
by Colin
10s
created

BacktickParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 94.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 6
dl 0
loc 44
ccs 18
cts 19
cp 0.9474
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCharacters() 0 4 1
B parse() 0 28 4
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Inline\Parser;
16
17
use League\CommonMark\Inline\Element\Code;
18
use League\CommonMark\Inline\Element\Text;
19
use League\CommonMark\InlineParserContext;
20
use League\CommonMark\Util\RegexHelper;
21
22
class BacktickParser extends AbstractInlineParser
23
{
24
    /**
25
     * @return string[]
26
     */
27 1941
    public function getCharacters()
28
    {
29 1941
        return ['`'];
30
    }
31
32
    /**
33
     * @param InlineParserContext $inlineContext
34
     *
35
     * @return bool
36
     */
37 111
    public function parse(InlineParserContext $inlineContext)
38
    {
39 111
        $cursor = $inlineContext->getCursor();
40
41 111
        $ticks = $cursor->match('/^`+/');
42 111
        if ($ticks === '') {
43
            return false;
44
        }
45
46 111
        $currentPosition = $cursor->getPosition();
47 111
        $previousState = $cursor->saveState();
48
49 111
        while ($matchingTicks = $cursor->match('/`+/m')) {
50 93
            if ($matchingTicks === $ticks) {
51 90
                $code = mb_substr($cursor->getLine(), $currentPosition, $cursor->getPosition() - $currentPosition - strlen($ticks), 'utf-8');
52 90
                $c = preg_replace(RegexHelper::REGEX_WHITESPACE, ' ', $code);
53 90
                $inlineContext->getContainer()->appendChild(new Code(trim($c)));
54
55 90
                return true;
56
            }
57 6
        }
58
59
        // If we got here, we didn't match a closing backtick sequence
60 36
        $cursor->restoreState($previousState);
61 36
        $inlineContext->getContainer()->appendChild(new Text($ticks));
62
63 36
        return true;
64
    }
65
}
66