Completed
Push — master ( 6c85d2...15e8d5 )
by Colin
04:57
created

SetExtHeadingParser::parse()   C

Complexity

Conditions 7
Paths 6

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 26
Code Lines 14

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
ccs 14
cts 14
cp 1
rs 6.7273
cc 7
eloc 14
nc 6
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 (http://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\Heading;
18
use League\CommonMark\Block\Element\Paragraph;
19
use League\CommonMark\ContextInterface;
20
use League\CommonMark\Cursor;
21
use League\CommonMark\Util\RegexHelper;
22
23
class SetExtHeadingParser extends AbstractBlockParser
24
{
25
    /**
26
     * @param ContextInterface $context
27
     * @param Cursor           $cursor
28
     *
29
     * @return bool
30
     */
31 1674
    public function parse(ContextInterface $context, Cursor $cursor)
32
    {
33 1674
        if ($cursor->isIndented()) {
34 177
            return false;
35
        }
36
37 1620
        if (!($context->getContainer() instanceof Paragraph)) {
38 1620
            return false;
39
        }
40
41 288
        if (count($context->getContainer()->getStrings()) !== 1) {
42 45
            return false;
43
        }
44
45 285
        $match = RegexHelper::matchAll('/^(?:=+|-+) *$/', $cursor->getLine(), $cursor->getFirstNonSpacePosition());
46 285
        if ($match === null) {
47 246
            return false;
48
        }
49
50 39
        $level = $match[0][0] === '=' ? 1 : 2;
51 39
        $strings = $context->getContainer()->getStrings();
52
53 39
        $context->replaceContainerBlock(new Heading($level, reset($strings) ?: ''));
54
55 39
        return true;
56
    }
57
}
58