Completed
Push — master ( 83739a...a13ee4 )
by Colin
14s queued 11s
created

EscapableParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 6
dl 0
loc 38
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCharacters() 0 4 1
A parse() 0 22 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\Newline;
18
use League\CommonMark\Inline\Element\Text;
19
use League\CommonMark\InlineParserContext;
20
use League\CommonMark\Util\RegexHelper;
21
22
final class EscapableParser implements InlineParserInterface
23
{
24
    /**
25
     * @return string[]
26
     */
27 2427
    public function getCharacters(): array
28
    {
29 2427
        return ['\\'];
30
    }
31
32
    /**
33
     * @param InlineParserContext $inlineContext
34
     *
35
     * @return bool
36
     */
37 126
    public function parse(InlineParserContext $inlineContext): bool
38
    {
39 126
        $cursor = $inlineContext->getCursor();
40 126
        $nextChar = $cursor->peek();
41
42 126
        if ($nextChar === "\n") {
43 15
            $cursor->advanceBy(2);
44 15
            $inlineContext->getContainer()->appendChild(new Newline(Newline::HARDBREAK));
45
46 15
            return true;
47 111
        } elseif ($nextChar !== null && RegexHelper::isEscapable($nextChar)) {
48 90
            $cursor->advanceBy(2);
49 90
            $inlineContext->getContainer()->appendChild(new Text($nextChar));
50
51 90
            return true;
52
        }
53
54 21
        $cursor->advanceBy(1);
55 21
        $inlineContext->getContainer()->appendChild(new Text('\\'));
56
57 21
        return true;
58
    }
59
}
60