AlertBlockParser::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 14
ccs 0
cts 9
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Markdown\Parser;
4
5
use League\CommonMark\Block\Parser\BlockParserInterface;
6
use League\CommonMark\ContextInterface;
7
use League\CommonMark\Cursor;
8
use App\Markdown\Element\AlertBlock;
9
10
class AlertBlockParser implements BlockParserInterface {
11
12
    /**
13
     * @inheritDoc
14
     */
15
    public function parse(ContextInterface $context, Cursor $cursor): bool {
16
        $previousState = $cursor->saveState();
17
18
        $alert = $cursor->match('/^!{3}([a-z]+)/');
19
        if($alert === null) {
20
            $cursor->restoreState($previousState);
21
            return false;
22
        }
23
24
        $type = substr($alert, 3);
25
26
        $context->addBlock(new AlertBlock($type));
27
28
        return true;
29
    }
30
}