Completed
Push — master ( d2636b...e88807 )
by Colin
02:51
created

EscapableParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 0
cbo 6
dl 0
loc 44
ccs 19
cts 20
cp 0.95
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCharacters() 0 4 1
B parse() 0 28 5
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\Newline;
18
use League\CommonMark\Inline\Element\Text;
19
use League\CommonMark\InlineParserContext;
20
use League\CommonMark\Util\RegexHelper;
21
22
class EscapableParser 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 93
    public function parse(InlineParserContext $inlineContext)
38
    {
39 93
        $cursor = $inlineContext->getCursor();
40 93
        if ($cursor->getCharacter() !== '\\') {
41
            return false;
42
        }
43
44 93
        $nextChar = $cursor->peek();
45
46 93
        if ($nextChar === "\n") {
47 12
            $cursor->advanceBy(2);
48 12
            $inlineContext->getContainer()->appendChild(new Newline(Newline::HARDBREAK));
49
50 12
            return true;
51 81
        } elseif ($nextChar !== null &&
52 75
            preg_match('/' . RegexHelper::REGEX_ESCAPABLE . '/', $nextChar)
53 54
        ) {
54 69
            $cursor->advanceBy(2);
55 69
            $inlineContext->getContainer()->appendChild(new Text($nextChar));
56
57 69
            return true;
58
        }
59
60 12
        $cursor->advance();
61 12
        $inlineContext->getContainer()->appendChild(new Text('\\'));
62
63 12
        return true;
64
    }
65
}
66