Completed
Pull Request — master (#61)
by Vladimir
03:02
created

MarkdownEngine::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\MarkupEngine;
9
10
use allejo\stakx\Configuration;
11
use allejo\stakx\Service;
12
use Highlight\Highlighter;
13
14
class MarkdownEngine extends \ParsedownExtra implements MarkupEngine
15
{
16
    protected $highlighter;
17
18
    public function __construct()
19
    {
20
        parent::__construct();
21
22
        $this->highlighter = new Highlighter();
23
    }
24
25
    protected function blockHeader($Line)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $Line is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
26
    {
27
        $Block = parent::blockHeader($Line);
28
29
        if (isset($Block['element']['text']))
30
        {
31
            $Block['element']['attributes']['id'] = $this->slugifyHeader($Block);
32
        }
33
34
        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...
35
    }
36
37
    protected function blockSetextHeader($Line, array $Block = null)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $Line is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $Block is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
38
    {
39
        $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...
40
41
        if (isset($Block['element']['name']))
42
        {
43
            $element = $Block['element']['name'];
44
45
            if (in_array($element, ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']))
46
            {
47
                $Block['element']['attributes']['id'] = $this->slugifyHeader($Block);
48
            }
49
        }
50
51
        return $Block;
52
    }
53
54
    private function slugifyHeader($Block)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $Block is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
55
    {
56
        $id = strtolower($Block['element']['text']);
57
        $id = str_replace(' ', '-', $id);
58
        $id = preg_replace('/[^0-9a-zA-Z-_]/', '', $id);
59
60
        return preg_replace('/-+/', '-', $id);
61
    }
62
63
    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...
64
    {
65
        // The class has a `language-` prefix, remove this to get the language
66
        if (isset($block['element']['text']['attributes']) && Service::getParameter(Configuration::HIGHLIGHTER_ENABLED))
67
        {
68
            $cssClass = &$block['element']['text']['attributes']['class'];
69
            $language = substr($cssClass, 9);
70
            $cssClass = 'hljs ' . $cssClass;
71
72
            try
73
            {
74
                $highlighted = $this->highlighter->highlight($language, $block['element']['text']['text']);
75
                $block['element']['text']['text'] = $highlighted->value;
76
77
                // Only return the block if Highlighter knew the language and how to handle it.
78
                return $block;
79
            }
80
                // Exception thrown when language not supported
81
            catch (\DomainException $exception)
82
            {
83
                trigger_error("An unsupported language ($language) was detected in a code block", 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
        return 'markdown';
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getExtensions()
106
    {
107
        return [
108
            'md',
109
            'mdown',
110
            'markdown',
111
        ];
112
    }
113
}
114