BacktickParser   A
last analyzed

Coupling/Cohesion

Components 0
Dependencies 7

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 7
dl 0
loc 43
ccs 17
cts 18
cp 0.9444
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCharacters() 0 4 1
B parse() 0 27 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 1935
    public function getCharacters()
28
    {
29 1935
        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
        $previousState = $cursor->saveState();
47
48 111
        while ($matchingTicks = $cursor->match('/`+/m')) {
49 93
            if ($matchingTicks === $ticks) {
50 90
                $code = mb_substr($cursor->getLine(), $previousState->getCurrentPosition(), $cursor->getPosition() - $previousState->getCurrentPosition() - strlen($ticks), 'utf-8');
51 90
                $c = preg_replace(RegexHelper::REGEX_WHITESPACE, ' ', $code);
52 90
                $inlineContext->getContainer()->appendChild(new Code(trim($c)));
53
54 90
                return true;
55
            }
56 12
        }
57
58
        // If we got here, we didn't match a closing backtick sequence
59 36
        $cursor->restoreState($previousState);
60 36
        $inlineContext->getContainer()->appendChild(new Text($ticks));
61
62 36
        return true;
63
    }
64
}
65