Completed
Push — master ( 7a0a03...73b89e )
by Colin
02:28
created

ATXHeadingParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
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 30
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

1 Method

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