Completed
Push — master ( 7b9015...c5b311 )
by Vladimir
26s queued 11s
created

MarkdownEngine   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 0
loc 109
ccs 32
cts 38
cp 0.8421
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A parse() 0 6 1
A blockHeader() 0 11 2
A blockSetextHeader() 0 19 3
A blockFencedCodeComplete() 0 13 3
A inlineImage() 0 13 2
A slugifyHeader() 0 4 1
A getTemplateTag() 0 4 1
A getExtensions() 0 8 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\Manager\AssetManager;
12
use allejo\stakx\Markup\AssetHandlerTrait;
13
use allejo\stakx\Markup\SyntaxHighlighterTrait;
14
use allejo\stakx\RuntimeStatus;
15
use allejo\stakx\Service;
16
use Highlight\Highlighter;
17
18
class MarkdownEngine extends \ParsedownExtra implements MarkupEngineInterface
19
{
20
    use AssetHandlerTrait;
21
    use SyntaxHighlighterTrait;
22
23 91
    public function __construct(AssetManager $assetManager)
24
    {
25 91
        parent::__construct();
26
27 91
        $this->highlighter = new Highlighter();
28 91
        $this->assetManager = $assetManager;
29 91
    }
30
31 20
    public function parse($text, $contentItem = null)
32
    {
33 20
        $this->contentItem = $contentItem;
34
35 20
        return parent::parse($text);
36
    }
37
38 11
    protected function blockHeader($Line)
39
    {
40 11
        $Block = parent::blockHeader($Line);
41
42 11
        if (isset($Block['element']['text']))
43
        {
44 11
            $Block['element']['attributes']['id'] = $this->slugifyHeader($Block);
45
        }
46
47 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...
48
    }
49
50 3
    protected function blockSetextHeader($Line, array $Block = null)
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...
51
    {
52
        // @TODO Remove this `@` operator in an update to Parsedown and ParsedownExtra
53
        //   https://wiki.php.net/rfc/notice-for-non-valid-array-container
54
        //   https://github.com/erusev/parsedown-extra/issues/134
55 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...
56
57 3
        if (isset($Block['element']['name']))
58
        {
59 1
            $element = $Block['element']['name'];
60
61 1
            if (in_array($element, ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']))
62
            {
63 1
                $Block['element']['attributes']['id'] = $this->slugifyHeader($Block);
64
            }
65
        }
66
67 3
        return $Block;
68
    }
69
70 7
    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...
71
    {
72
        // The class has a `language-` prefix, remove this to get the language
73 7
        if (isset($block['element']['text']['attributes']) && Service::hasRunTimeFlag(RuntimeStatus::USING_HIGHLIGHTER))
74
        {
75 6
            $cssClass = $block['element']['text']['attributes']['class'];
76 6
            $block['markup'] = $this->highlightCode($cssClass, $block['element']['text']['text']);
77
78 5
            return $block;
79
        }
80
81 1
        return parent::blockFencedCodeComplete($block);
82
    }
83
84
    protected function inlineImage($Excerpt)
85
    {
86
        $imageBlock = parent::inlineImage($Excerpt);
87
88
        if ($imageBlock !== null)
89
        {
90
            $imageSrc = trim($imageBlock['element']['attributes']['src']);
91
92
            $this->registerAsset($imageSrc);
93
        }
94
95
        return $imageBlock;
96
    }
97
98 11
    private function slugifyHeader($Block)
99
    {
100 11
        return __::slug($Block['element']['text']);
101
    }
102
103
    ///
104
    // MarkupEngine Implementation
105
    ///
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 76
    public function getTemplateTag()
111
    {
112 76
        return 'markdown';
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 76
    public function getExtensions()
119
    {
120
        return [
121 76
            'md',
122
            'mdown',
123
            'markdown',
124
        ];
125
    }
126
}
127