Completed
Push — master ( 69f712...a96098 )
by Andrii
02:39
created

MarkdownRenderer   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.61%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 21
c 3
b 0
f 1
lcom 1
cbo 4
dl 0
loc 116
ccs 57
cts 59
cp 0.9661
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 10 1
A renderHeaders() 0 4 1
A renderTags() 0 4 1
A renderLinks() 0 4 1
A renderObjects() 0 9 2
A renderSparse() 0 6 2
A renderText() 0 11 3
A renderLink() 0 4 1
A renderHeader() 0 4 1
A renderTag() 0 7 1
A renderTagHead() 0 9 2
A renderNote() 0 7 1
A renderNoteHead() 0 4 2
A renderCommit() 0 7 1
A renderCommitHead() 0 4 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
    /**
24
     * {@inheritdoc}
25
     */
26 2
    public function render(History $history)
27
    {
28 2
        $this->setHistory($history);
29
30 2
        return $this->renderSparse([
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