ATXHeadingParser   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 34
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A parse() 0 25 3
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\Heading;
18
use League\CommonMark\ContextInterface;
19
use League\CommonMark\Cursor;
20
use League\CommonMark\Util\RegexHelper;
21
22
final class ATXHeadingParser implements BlockParserInterface
23
{
24
    /**
25
     * @param ContextInterface $context
26
     * @param Cursor           $cursor
27
     *
28
     * @return bool
29
     */
30 2058
    public function parse(ContextInterface $context, Cursor $cursor): bool
31
    {
32 2058
        if ($cursor->isIndented()) {
33 180
            return false;
34
        }
35
36 2001
        $match = RegexHelper::matchAll('/^#{1,6}(?:[ \t]+|$)/', $cursor->getLine(), $cursor->getNextNonSpacePosition());
37 2001
        if (!$match) {
38 1956
            return false;
39
        }
40
41 81
        $cursor->advanceToNextNonSpaceOrTab();
42
43 81
        $cursor->advanceBy(\strlen($match[0]));
44
45 81
        $level = \strlen(\trim($match[0]));
46 81
        $str = $cursor->getRemainder();
47 81
        $str = \preg_replace('/^[ \t]*#+[ \t]*$/', '', $str);
48 81
        $str = \preg_replace('/[ \t]+#+[ \t]*$/', '', $str);
49
50 81
        $context->addBlock(new Heading($level, $str));
51 81
        $context->setBlocksParsed(true);
52
53 81
        return true;
54
    }
55
}
56