ParsedownHighlight::blockFencedCodeComplete()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 24

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 13
CRAP Score 3.0032

Importance

Changes 0
Metric Value
dl 24
loc 24
ccs 13
cts 14
cp 0.9286
rs 9.536
c 0
b 0
f 0
cc 3
nc 5
nop 1
crap 3.0032
1
<?php
2
3
namespace sixlive;
4
5
use DomainException;
6
use Highlight\Highlighter;
7
use Parsedown;
8
9 View Code Duplication
class ParsedownHighlight extends Parsedown
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
10
{
11
    protected $highlighter;
12
13 2
    public function __construct()
14
    {
15 2
        $this->highlighter = new Highlighter;
16 2
    }
17
18 2
    protected function blockFencedCodeComplete($block)
19
    {
20 2
        if (! isset($block['element']['element']['attributes'])) {
21
            return $block;
22
        }
23
24 2
        $code = $block['element']['element']['text'];
25 2
        $languageClass = $block['element']['element']['attributes']['class'];
26 2
        $language = explode('-', $languageClass);
27
28
        try {
29 2
            $highlighted = $this->highlighter->highlight($language[1], $code);
30 2
            $block['element']['element']['attributes']['class'] = vsprintf('%s hljs %s', [
31 2
                $languageClass,
32 2
                $highlighted->language,
33
            ]);
34 2
            $block['element']['element']['rawHtml'] = $highlighted->value;
35 2
            unset($block['element']['element']['text']);
36 2
        } catch (DomainException $e) {
37
            //
38
        }
39
40 2
        return $block;
41
    }
42
}
43