Completed
Push — master ( 7b4929...cefe8d )
by Andrii
01:50
created

MarkdownRenderer::renderCommitHead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Changelog keeper
4
 *
5
 * @link      https://github.com/hiqdev/chkipper
6
 * @package   chkipper
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\chkipper\history;
12
13
/**
14
 * Markdown history renderer.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class MarkdownRenderer extends AbstractRenderer
19
{
20
    public $indent = '    ';
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function render(History $history)
26
    {
27
        $this->setHistory($history);
28
29
        return $this->renderSparse([
30
            $this->renderHeaders(),
31
            $this->renderTags(),
32
            $this->renderLinks(),
33
        ]);
34
    }
35
36
    public function renderHeaders()
37
    {
38
        return $this->renderText($this->getHistory()->getHeaders());
39
    }
40
41
    public function renderTags()
42
    {
43
        return $this->renderObjects('renderTag', $this->getHistory()->getTags(), true);
44
    }
45
46
    public function renderLinks($links = null)
47
    {
48
        if ($links === null) {
49
            $links = $this->getHistory()->getLinks();
50
        }
51
52
        return $this->renderObjects('renderLink', $links);
53
    }
54
55
    public function renderObjects($method, $objects, $sparse = false)
56
    {
57
        $res = [];
58
        foreach ($objects as $key => $value) {
59
            $res[$key] = call_user_func([$this, $method], $value, $key);
60
        }
61
62
        return $this->renderText($res, $sparse);
63
    }
64
65
    public function renderSparse(array $lines)
66
    {
67
        $res = rtrim(implode("\n", $lines));
68
69
        return $res ? $res . "\n" : '';
70
    }
71
72
    public function renderText(array $lines, $sparse = false)
73
    {
74
        if (!$sparse) {
75
            foreach ($lines as &$line) {
76
                $line = rtrim($line);
77
            }
78
            $lines = array_filter($lines);
79
        }
80
81
        return $this->renderSparse($lines);
82
    }
83
84
    public function renderLink($href, $link)
85
    {
86
        return "[$link]: $href";
87
    }
88
89
    public function renderHeader($header)
90
    {
91
        return $header;
92
    }
93
94
    public function renderTag(Tag $tag)
95
    {
96
        return $this->renderSparse([
97
            $this->renderTagHead($tag),
98
            $this->renderObjects('renderNote', $tag->getNotes()),
99
        ]);
100
    }
101
102
    public function renderTagHead(Tag $tag)
103
    {
104
        $res = '## [' . $tag->getName() . ']';
105
        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...
106
            $res .= ' - ' . $tag->getDate();
107
        }
108
109
        return $res . "\n";
110
    }
111
112
    public function renderNote(Note $note)
113
    {
114
        return $this->renderText([
115
            $this->renderNoteHead($note),
116
            $this->renderObjects('renderCommit', $note->getCommits()),
117
        ]);
118
    }
119
120
    public function renderNoteHead(Note $note)
121
    {
122
        return $note->getNote() ? '- ' . $note->getNote() : '';
123
    }
124
125
    public function renderCommit(Commit $commit)
126
    {
127
        return $this->renderText([
128
            $this->renderCommitHead($commit),
129
            $this->renderText($commit->getComments()),
130
        ]);
131
    }
132
133
    public function renderCommitHead(Commit $commit)
134
    {
135
        return $this->indent . '- [' . $commit->getHash() . '] ' . $commit->getLabel();
136
    }
137
}
138