Completed
Push — master ( ebafc7...6d87de )
by Andrii
11:59
created

Readme   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 7

Test Coverage

Coverage 8.96%

Importance

Changes 0
Metric Value
wmc 33
lcom 3
cbo 7
dl 0
loc 180
ccs 6
cts 67
cp 0.0896
rs 9.76
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 4 1
A getTemplate() 0 4 1
A getCharset() 0 4 3
A renderH() 0 4 1
A renderH1() 0 4 1
A renderH2() 0 4 1
A renderH3() 0 4 1
A renderText() 0 6 2
A renderBold() 0 6 1
A renderSection() 0 9 3
A getSection() 0 11 3
A setSections() 0 4 1
A getSections() 0 8 2
A renderSections() 0 12 3
B renderBadges() 0 20 6
A renderBadge() 0 6 1
A getTwig() 0 8 2
1
<?php
2
/**
3
 * README plugin for HiDev
4
 *
5
 * @link      https://github.com/hiqdev/hidev-readme
6
 * @package   hidev-readme
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\readme\components;
12
13
use hidev\helpers\FileHelper;
14
use hidev\helpers\Helper;
15
use Yii;
16
17
/**
18
 * README generation component.
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class Readme extends \hidev\base\Component
22
{
23
    /**
24
     * @var \Twig_Environment
25
     */
26
    protected $_twig;
27
28
    /**
29
     * @var array list of sections to render
30
     */
31
    protected $_sections;
32
33
    public $knownBadges = [];
34
35
    public $badges;
36
37
    public function save($path, $type)
38
    {
39
        FileHelper::write($path, $this->render($this->getTemplate($type)));
40
    }
41
42
    public function getTemplate($type)
43
    {
44
        return 'readme-' . $type;
45
    }
46
47
    /**
48
     * Get charset.
49
     * @return string
50
     */
51
    public function getCharset()
52
    {
53
        return (isset(Yii::$app->charset) ? Yii::$app->charset : null) ?: mb_internal_encoding();
54
    }
55
56 2
    public function renderH($title, $prefix)
57
    {
58 2
        return $prefix . ' ' . $title . "\n";
59
    }
60
61 1
    public function renderH1($title)
62
    {
63 1
        return $this->renderH($title, '#');
64
    }
65
66 1
    public function renderH2($title)
67
    {
68 1
        return $this->renderH($title, '##');
69
    }
70
71
    public function renderH3($title)
72
    {
73
        return $this->renderH($title, '###');
74
    }
75
76
    public function renderText($text)
77
    {
78
        $text = trim($text);
79
80
        return $text ? "\n$text\n" : '';
81
    }
82
83
    public function renderBold($text)
84
    {
85
        $text = trim($text);
86
87
        return $this->renderText('**' . $text . '**');
88
    }
89
90
    public function renderSection($section, $default = null)
91
    {
92
        $file = 'readme/' . str_replace(' ', '', $section);
93
        $path = Yii::getAlias("@root/docs/$file.md");
94
        $text = file_exists($path) ? file_get_contents($path) : $this->getSection($file, $default);
95
        $text = trim($text);
96
97
        return $text ? "\n## $section\n\n$text\n" : '';
98
    }
99
100
    public function getSection($file, $default = null)
101
    {
102
        $tpl = '@hidev/views/' . Helper::file2template($file);
103
        try {
104
            $res = $this->render($tpl);
105
        } catch (\Exception $e) {
106
            $res = '';
107
        }
108
109
        return $res ?: $default;
110
    }
111
112
    /**
113
     * Set sections list.
114
     * @param array $value
115
     */
116
    public function setSections($value)
117
    {
118
        $this->_sections = (array) $value;
119
    }
120
121
    /**
122
     * Returns sections list. Returns default list if not set.
123
     * @return array
124
     */
125
    public function getSections()
126
    {
127
        if (empty($this->_sections)) {
128
            $this->_sections = ['Requirements', 'Installation', 'Idea', 'Configuration', 'Basic Usage', 'Usage', 'Support', 'License', 'Acknowledgements', 'Acknowledgments'];
129
        }
130
131
        return array_unique($this->_sections);
132
    }
133
134
    /**
135
     * Render all configured sections.
136
     * @return string
137
     */
138
    public function renderSections($sections = null)
139
    {
140
        if ($sections === null) {
141
            $sections = $this->getSections();
142
        }
143
        $res = '';
144
        foreach ($sections as $section) {
145
            $res .= $this->renderSection($section);
146
        }
147
148
        return $res;
149
    }
150
151
    /**
152
     * Render all configured badges.
153
     * @return string
154
     */
155
    public function renderBadges()
156
    {
157
        $badges = $this->badges;
158
        if (!$badges) {
159
            return '';
160
        }
161
        $pm = $this->take('package')->getPackageManager();
0 ignored issues
show
Unused Code introduced by
$pm is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
162
        $res = '';
163
        foreach ($badges as $badge => $tpl) {
164
            if (!$tpl) {
165
                $tpl = $this->knownBadges[$badge];
166
            }
167
            if ($tpl === 'disabled') {
168
                continue;
169
            }
170
            $res .= $this->renderBadge($tpl) . "\n";
171
        }
172
173
        return $res ? "\n$res" : '';
174
    }
175
176
    /**
177
     * Render badge by given template.
178
     * @param string $template string to render
179
     * @return string
180
     */
181
    public function renderBadge($template)
182
    {
183
        $tpl = $this->getTwig()->createTemplate($template);
184
185
        return $tpl->render(['app' => Yii::$app]);
186
    }
187
188
    /**
189
     * Twig getter.
190
     * @return \Twig_Environment
191
     */
192
    public function getTwig()
193
    {
194
        if ($this->_twig === null) {
195
            $this->_twig = new \Twig_Environment(new \Twig_Loader_Array());
196
        }
197
198
        return $this->_twig;
199
    }
200
}
201