Completed
Push — master ( 5a1c08...4a9651 )
by Vladimir
30:46
created

MarkdownEngine::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\MarkupEngine;
9
10
use __;
11
use allejo\stakx\RuntimeStatus;
12
use allejo\stakx\Service;
13
use Highlight\Highlighter;
14
15
class MarkdownEngine extends \ParsedownExtra implements MarkupEngineInterface
16
{
17
    protected $highlighter;
18
19 87
    public function __construct()
20
    {
21 87
        parent::__construct();
22
23 87
        $this->highlighter = new Highlighter();
24 87
    }
25
26 11
    protected function blockHeader($Line)
27
    {
28 11
        $Block = parent::blockHeader($Line);
29
30 11
        if (isset($Block['element']['text']))
31
        {
32 11
            $Block['element']['attributes']['id'] = $this->slugifyHeader($Block);
33
        }
34
35 11
        return $Block;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $Block; (array<string,array<string,string|null>>) is incompatible with the return type of the parent method ParsedownExtra::blockHeader of type array<string,array<string,string>>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
36
    }
37
38 3
    protected function blockSetextHeader($Line, array $Block = null)
39
    {
40 3
        $Block = parent::blockSetextHeader($Line, $Block);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $Block. This often makes code more readable.
Loading history...
41
42 3
        if (isset($Block['element']['name']))
43
        {
44 1
            $element = $Block['element']['name'];
45
46 1
            if (in_array($element, ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']))
47
            {
48 1
                $Block['element']['attributes']['id'] = $this->slugifyHeader($Block);
49
            }
50
        }
51
52 3
        return $Block;
53
    }
54
55 11
    private function slugifyHeader($Block)
56
    {
57 11
        return __::slug($Block['element']['text']);
58
    }
59
60 3
    protected 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...
61
    {
62
        // The class has a `language-` prefix, remove this to get the language
63 3
        if (isset($block['element']['text']['attributes']) && Service::hasRunTimeFlag(RuntimeStatus::USING_HIGHLIGHTER))
64
        {
65 2
            $cssClass = $block['element']['text']['attributes']['class'];
66 2
            $language = substr($cssClass, 9);
67
68
            try
69
            {
70 2
                $highlighted = $this->highlighter->highlight($language, $block['element']['text']['text']);
71 1
                $block['markup'] = "<pre><code class=\"hljs ${cssClass}\">" . $highlighted->value . '</code></pre>';
72
73
                // Only return the block if Highlighter knew the language and how to handle it.
74 1
                return $block;
75
            }
76
            // Exception thrown when language not supported
77 1
            catch (\DomainException $exception)
78
            {
79 1
                trigger_error("An unsupported language ($language) was detected in a code block", E_USER_WARNING);
80
            }
81
            catch (\Exception $e)
82
            {
83
                trigger_error('An error has occurred in the highlight.php language definition files', E_USER_WARNING);
84
            }
85
        }
86
87
        return parent::blockFencedCodeComplete($block);
88
    }
89
90
    ///
91
    // MarkupEngine Implementation
92
    ///
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getTemplateTag()
98
    {
99 76
        return 'markdown';
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getExtensions()
106
    {
107
        return [
108 76
            'md',
109
            'mdown',
110
            'markdown',
111
        ];
112
    }
113
}
114