Completed
Pull Request — master (#48)
by Vladimir
04:43
created

MarkdownEngine::blockFencedCodeComplete()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 1
dl 0
loc 26
ccs 12
cts 13
cp 0.9231
crap 3.004
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Engines\Markdown;
9
10
use allejo\stakx\Engines\ParsingEngine;
11
use Highlight\Highlighter;
12
13
class MarkdownEngine extends \ParsedownExtra implements ParsingEngine
14
{
15
    protected $highlighter;
16
17 4
    public function __construct()
18
    {
19 4
        parent::__construct();
20
21 4
        $this->highlighter = new Highlighter();
22 4
    }
23
24 3
    protected function blockHeader($line)
25
    {
26 3
        $Block = parent::blockHeader($line);
0 ignored issues
show
Coding Style introduced by
$Block does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
27
28
        // Create our unique ids by sanitizing the header content
29 3
        $id = strtolower($Block['element']['text']);
0 ignored issues
show
Coding Style introduced by
$Block does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
30 3
        $id = str_replace(' ', '-', $id);
31 3
        $id = preg_replace('/[^0-9a-zA-Z-_]/', '', $id);
32 3
        $id = preg_replace('/-+/', '-', $id);
33
34 3
        $Block['element']['attributes']['id'] = $id;
0 ignored issues
show
Coding Style introduced by
$Block does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
35
36 3
        return $Block;
0 ignored issues
show
Coding Style introduced by
$Block does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
37
    }
38
39 3
    public function blockFencedCodeComplete($block)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
40
    {
41
        // The class has a `language-` prefix, remove this to get the language
42 3
        if (isset($block['element']['text']['attributes']))
43 3
        {
44 2
            $cssClass = &$block['element']['text']['attributes']['class'];
45 2
            $language = substr($cssClass, 9);
46 2
            $cssClass = 'hljs ' . $cssClass;
47
48
            try
49
            {
50 2
                $highlighted = $this->highlighter->highlight($language, $block['element']['text']['text']);
51 1
                $block['element']['text']['text'] = $highlighted->value;
52
53
                // Only return the block if Highlighter knew the language and how to handle it.
54 1
                return $block;
55
            }
56
            // Exception thrown when language not supported
57 1
            catch (\DomainException $exception)
58
            {
59 1
                trigger_error("An unsupported language ($language) was detected in a code block", E_USER_WARNING);
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $language instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
60
            }
61
        }
62
63 1
        return parent::blockFencedCodeComplete($block);
64
    }
65
}
66