Completed
Push — master ( 808823...246322 )
by Andrii
02:33
created

MarkdownRenderer::renderHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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