Completed
Push — master ( dc889e...13f9c8 )
by Andrii
02:52
created

Renderer::renderNoteHead()   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 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
 * Renderer class.
16
 *
17
 * @author Andrii Vasyliev <[email protected]>
18
 */
19
class Renderer
20
{
21
    public $indent = '    ';
22
23 1
    public function setHistory($value)
24
    {
25 1
        $this->_history = $value;
0 ignored issues
show
Bug introduced by
The property _history does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

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