Completed
Push — master ( 0b0b24...54ff37 )
by Colin
02:37
created

FencedCodeParser::parse()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 8.6026
c 0
b 0
f 0
cc 7
nc 4
nop 2
crap 7
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\Extension\CommonMark\Parser\Block;
16
17
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
18
use League\CommonMark\Parser\Block\BlockParserInterface;
19
use League\CommonMark\Parser\ContextInterface;
20
use League\CommonMark\Parser\Cursor;
21
22
final class FencedCodeParser implements BlockParserInterface
23
{
24 2412
    public function parse(ContextInterface $context, Cursor $cursor): bool
25
    {
26 2412
        if ($cursor->isIndented()) {
27 183
            return false;
28
        }
29
30 2355
        $c = $cursor->getCharacter();
31 2355
        if ($c !== ' ' && $c !== "\t" && $c !== '`' && $c !== '~') {
32 2196
            return false;
33
        }
34
35 354
        $indent = $cursor->getIndent();
36 354
        $fence = $cursor->match('/^[ \t]*(?:`{3,}(?!.*`)|^~{3,})/');
37 354
        if ($fence === null) {
38 249
            return false;
39
        }
40
41
        // fenced code block
42 105
        $fence = \ltrim($fence, " \t");
43 105
        $fenceLength = \strlen($fence);
44 105
        $context->addBlock(new FencedCode($fenceLength, $fence[0], $indent));
45
46 105
        return true;
47
    }
48
}
49