Completed
Push — master ( e61f48...23ee6c )
by Andrii
43:08 queued 14:00
created

ReadmeController::renderSections()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 1
crap 3
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\controllers;
12
13
use hidev\helpers\Helper;
14
use Yii;
15
16
/**
17
 * Goal for README.file generation.
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class ReadmeController extends \hidev\controllers\TemplateController
21
{
22
    /**
23
     * @var \Twig_Environment
24
     */
25
    protected $_twig;
26
27
    /**
28
     * @var array list of sections to render
29
     */
30
    protected $_sections;
31
32
    /**
33
     * Get charset.
34
     * @return string
35
     */
36
    public function getCharset()
37
    {
38
        return (isset(Yii::$app->charset) ? Yii::$app->charset : null) ?: mb_internal_encoding();
39
    }
40
41 4
    public function renderH($title, $prefix)
42
    {
43 4
        return $prefix . ' ' . $title . "\n";
44
    }
45
46 3
    public function renderH1($title)
47
    {
48 3
        return $this->renderH($title, '#');
49
    }
50
51 1
    public function renderH2($title)
52
    {
53 1
        return $this->renderH($title, '##');
54
    }
55
56 1
    public function renderText($text)
57
    {
58 1
        $text = trim($text);
59
60 1
        return $text ? "\n$text\n" : '';
61
    }
62
63 1
    public function renderBold($text)
64
    {
65 1
        $text = trim($text);
66
67 1
        return $this->renderText('**' . $text . '**');
68
    }
69
70 2
    public function renderSection($section, $default = null)
71
    {
72 2
        $file = 'readme/' . str_replace(' ', '', $section);
73 2
        $path = Yii::getAlias("@root/docs/$file.md");
74 2
        $text = file_exists($path) ? file_get_contents($path) : $this->getSection($file, $default);
75 2
        $text = trim($text);
76
77 2
        return $text ? "\n## $section\n\n$text\n" : '';
78
    }
79
80 2
    public function getSection($file, $default = null)
81
    {
82 2
        $view = Yii::$app->getView();
83 2
        $tpl = Helper::file2template($file);
84
        try {
85 2
            $res = $view->render($tpl, ['config' => $this->takeConfig()]);
86 2
        } catch (\Exception $e) {
87 2
            $res = '';
88
        }
89
90 2
        return $res ?: $default;
91
    }
92
93
    /**
94
     * Set sections list.
95
     * @param array $value
96
     */
97
    public function setSections($value)
98
    {
99
        $this->_sections = (array) $value;
100
    }
101
102
    /**
103
     * Returns sections list. Returns default list if not set.
104
     * @return array
105
     */
106 2
    public function getSections()
107
    {
108 2
        if (empty($this->_sections)) {
109 2
            $this->_sections = ['Requirements', 'Installation', 'Idea', 'Configuration', 'Basic Usage', 'Usage', 'Support', 'License', 'Acknowledgements', 'Acknowledgments'];
110
        }
111
112 2
        return $this->_sections;
113
    }
114
115
    /**
116
     * Render all configured sections.
117
     * @return string
118
     */
119 2
    public function renderSections($sections = null)
120
    {
121 2
        if ($sections === null) {
122 2
            $sections = $this->getSections();
123
        }
124 2
        $res = '';
125 2
        foreach ($sections as $section) {
126 2
            $res .= $this->renderSection($section);
127
        }
128
129 2
        return $res;
130
    }
131
132
    /**
133
     * Render all configured badges.
134
     * @return string
135
     */
136 2
    public function renderBadges()
137
    {
138 2
        $badges = $this->badges;
0 ignored issues
show
Documentation introduced by
The property badges does not exist on object<hidev\readme\controllers\ReadmeController>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
139 2
        if (!$badges) {
140
            return '';
141
        }
142 2
        $pm = $this->takeGoal('package')->getPackageManager();
143 2
        if (!$pm || !$pm->getConfiguration()->getRequire()) {
144 2
            unset($badges['versioneye.dependencies']);
145
        }
146 2
        $res = '';
147 2
        foreach ($badges as $badge => $tpl) {
148 2
            if (!$tpl) {
149 2
                $tpl = $this->markdownBadges[$badge];
0 ignored issues
show
Documentation introduced by
The property markdownBadges does not exist on object<hidev\readme\controllers\ReadmeController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
150
            }
151 2
            if ($tpl === 'disabled') {
152
                continue;
153
            }
154 2
            $res .= $this->renderBadge($tpl) . "\n";
155
        }
156
157 2
        return $res ? "\n$res" : '';
158
    }
159
160
    /**
161
     * Render badge by given template.
162
     * @param string $template string to render
163
     * @return string
164
     */
165 2
    public function renderBadge($template)
166
    {
167 2
        return $this->getTwig()->render($template, ['config' => $this->takeConfig()]);
168
    }
169
170
    /**
171
     * Twig getter.
172
     * @return \Twig_Environment
173
     */
174 2
    public function getTwig()
175
    {
176 2
        if ($this->_twig === null) {
177 2
            $this->_twig = new \Twig_Environment(new \Twig_Loader_String());
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Loader_String has been deprecated with message: since 1.18.1 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
178
        }
179
180 2
        return $this->_twig;
181
    }
182
}
183