Completed
Push — master ( 59f3c3...4dab06 )
by Andrii
02:30
created

MarkdownRenderer::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
crap 1
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 2
    public function render(History $history)
24
    {
25 2
        $this->setHistory($history);
26
27 2
        return $this->renderSparse([
28 2
            $this->renderHeaders(),
29 2
            $this->renderTags(),
30 2
            $this->renderLinks(),
31 2
        ]);
32
    }
33
34 2
    public function renderHeaders()
35
    {
36 2
        return $this->renderText($this->getHistory()->getHeaders());
37
    }
38
39 2
    public function renderTags()
40
    {
41 2
        return $this->renderObjects('renderTag', $this->getHistory()->getTags(), true);
42
    }
43
44 2
    public function renderLinks()
45
    {
46 2
        return $this->renderObjects('renderLink', $this->getHistory()->getLinks());
47
    }
48
49 2
    public function renderObjects($method, $objects, $sparse = false)
50
    {
51 2
        $res = [];
52 2
        foreach ($objects as $key => $value) {
53 2
            $res[$key] = call_user_func([$this, $method], $value, $key);
54 2
        }
55
56 2
        return $this->renderText($res, $sparse);
57
    }
58
59 2
    public function renderSparse(array $lines)
60
    {
61 2
        $res = rtrim(implode("\n", $lines));
62
63 2
        return $res ? $res . "\n" : '';
64
    }
65
66 2
    public function renderText(array $lines, $sparse = false)
67
    {
68 2
        if (!$sparse) {
69 2
            foreach ($lines as &$line) {
70 2
                $line = rtrim($line);
71 2
            }
72 2
            $lines = array_filter($lines);
73 2
        }
74
75 2
        return $this->renderSparse($lines);
76
    }
77
78 1
    public function renderLink($href, $link)
79
    {
80 1
        return "[$link]: $href";
81
    }
82
83
    public function renderHeader($header)
84
    {
85
        return $header;
86
    }
87
88 2
    public function renderTag(Tag $tag)
89
    {
90 2
        return $this->renderSparse([
91 2
            $this->renderTagHead($tag),
92 2
            $this->renderObjects('renderNote', $tag->getNotes()),
93 2
        ]);
94
    }
95
96 2
    public function renderTagHead(Tag $tag)
97
    {
98 2
        $res = '## [' . $tag->getName() . ']';
99 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...
100 2
            $res .= ' - ' . $tag->getDate();
101 2
        }
102
103 2
        return $res . "\n";
104
    }
105
106 2
    public function renderNote(Note $note)
107
    {
108 2
        return $this->renderText([
109 2
            $this->renderNoteHead($note),
110 2
            $this->renderObjects('renderCommit', $note->getCommits()),
111 2
        ]);
112
    }
113
114 2
    public function renderNoteHead(Note $note)
115
    {
116 2
        return $note->getNote() ? '- ' . $note->getNote() : '';
117
    }
118
119 2
    public function renderCommit(Commit $commit)
120
    {
121 2
        return $this->renderText([
122 2
            $this->renderCommitHead($commit),
123 2
            $this->renderText($commit->getComments()),
124 2
        ]);
125
    }
126
127 2
    public function renderCommitHead(Commit $commit)
128
    {
129 2
        return $this->indent . '- [' . $commit->getHash() . '] ' . $commit->getLabel();
130
    }
131
}
132