Completed
Push — master ( b42489...8df37e )
by Colin
14s queued 11s
created

FencedCodeParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 33
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 0 24 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\Block\Parser;
16
17
use League\CommonMark\Block\Element\FencedCode;
18
use League\CommonMark\ContextInterface;
19
use League\CommonMark\Cursor;
20
21
final class FencedCodeParser implements BlockParserInterface
22
{
23
    /**
24
     * @param ContextInterface $context
25
     * @param Cursor           $cursor
26
     *
27
     * @return bool
28
     */
29 2019
    public function parse(ContextInterface $context, Cursor $cursor): bool
30
    {
31 2019
        if ($cursor->isIndented()) {
32 180
            return false;
33
        }
34
35 1962
        $c = $cursor->getCharacter();
36 1962
        if ($c !== ' ' && $c !== "\t" && $c !== '`' && $c !== '~') {
37 1815
            return false;
38
        }
39
40 336
        $indent = $cursor->getIndent();
41 336
        $fence = $cursor->match('/^[ \t]*(?:`{3,}(?!.*`)|^~{3,})/');
42 336
        if ($fence === null) {
43 231
            return false;
44
        }
45
46
        // fenced code block
47 105
        $fence = \ltrim($fence, " \t");
48 105
        $fenceLength = \strlen($fence);
49 105
        $context->addBlock(new FencedCode($fenceLength, $fence[0], $indent));
50
51 105
        return true;
52
    }
53
}
54