Completed
Push — master ( 2cadce...9a51d7 )
by Andrii
02:29
created

MarkdownRenderer::renderNoteHead()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * Changelog keeper
5
 *
6
 * @link      https://github.com/hiqdev/chkipper
7
 * @package   chkipper
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\chkipper\history;
13
14
/**
15
 * Markdown history renderer.
16
 *
17
 * @author Andrii Vasyliev <[email protected]>
18
 */
19
class MarkdownRenderer extends AbstractRenderer
20
{
21
    public $indent = '    ';
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 2
    public function render(History $history)
27
    {
28 2
        $this->setHistory($history);
29
30 2
        return $this->renderSparse([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->renderSpar...$this->renderLinks())); (string) is incompatible with the return type declared by the abstract method hiqdev\chkipper\history\AbstractRenderer::render of type hiqdev\chkipper\history\text.

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...
31 2
            $this->renderHeaders(),
32 2
            $this->renderTags(),
33 2
            $this->renderLinks(),
34 2
        ]);
35
    }
36
37 2
    public function renderHeaders()
38
    {
39 2
        return $this->renderText($this->getHistory()->getHeaders());
40
    }
41
42 2
    public function renderTags()
43
    {
44 2
        return $this->renderObjects('renderTag', $this->getHistory()->getTags(), true);
45
    }
46
47 2
    public function renderLinks()
48
    {
49 2
        return $this->renderObjects('renderLink', $this->getHistory()->getLinks());
50
    }
51
52 2
    public function renderObjects($method, $objects, $sparse = false)
53
    {
54 2
        $res = [];
55 2
        foreach ($objects as $key => $value) {
56 2
            $res[$key] = call_user_func([$this, $method], $value, $key);
57 2
        }
58
59 2
        return $this->renderText($res, $sparse);
60
    }
61
62 2
    public function renderSparse(array $lines)
63
    {
64 2
        $res = rtrim(implode("\n", $lines));
65
66 2
        return $res ? $res . "\n" : '';
67
    }
68
69 2
    public function renderText(array $lines, $sparse = false)
70
    {
71 2
        if (!$sparse) {
72 2
            foreach ($lines as &$line) {
73 2
                $line = rtrim($line);
74 2
            }
75 2
            $lines = array_filter($lines);
76 2
        }
77
78 2
        return $this->renderSparse($lines);
79
    }
80
81 2
    public function renderLink($href, $link)
82
    {
83 2
        return "[$link]: $href";
84
    }
85
86
    public function renderHeader($header)
87
    {
88
        return $header;
89
    }
90
91 2
    public function renderTag(Tag $tag)
92
    {
93 2
        return $this->renderSparse([
94 2
            $this->renderTagHead($tag),
95 2
            $this->renderObjects('renderNote', $tag->getNotes()),
96 2
        ]);
97
    }
98
99 2
    public function renderTagHead(Tag $tag)
100
    {
101 2
        $res = '## [' . $tag->getName() . ']';
102 2
        if ($tag->getDate()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tag->getDate() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
103 2
            $res .= ' - ' . $tag->getDate();
104 2
        }
105
106 2
        return $res . "\n";
107
    }
108
109 2
    public function renderNote(Note $note)
110
    {
111 2
        return $this->renderText([
112 2
            $this->renderNoteHead($note),
113 2
            $this->renderObjects('renderCommit', $note->getCommits()),
114 2
        ]);
115
    }
116
117 2
    public function renderNoteHead(Note $note)
118
    {
119 2
        return $note->getNote() ? '- ' . $note->getNote() : '';
120
    }
121
122 2
    public function renderCommit(Commit $commit)
123
    {
124 2
        return $this->renderText([
125 2
            $this->renderCommitHead($commit),
126 2
            $this->renderText($commit->getComments()),
127 2
        ]);
128
    }
129
130 2
    public function renderCommitHead(Commit $commit)
131
    {
132 2
        return $this->indent . '- [' . $commit->getHash() . '] ' . $commit->getLabel();
133
    }
134
}
135