Completed
Push — master ( b3b6d3...2114de )
by Andrii
08:18
created

ReadmeController::renderBold()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * README plugin for HiDev
5
 *
6
 * @link      https://github.com/hiqdev/hidev-readme
7
 * @package   hidev-readme
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\readme\controllers;
13
14
use hidev\helpers\Helper;
15
use Yii;
16
17
/**
18
 * Goal for README.file generation.
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class ReadmeController extends \hidev\controllers\TemplateController
22
{
23
    /**
24
     * @var \Twig_Environment
25
     */
26
    protected $_twig;
27
28
    /**
29
     * Get charset.
30
     * @return string
31
     */
32 4
    public function getCharset()
33
    {
34 4
        return (isset(Yii::$app->charset) ? Yii::$app->charset : null) ?: mb_internal_encoding();
35
    }
36
37 4
    public function renderH($title, $char)
38
    {
39 4
        $res = $title . "\n";
40 4
        $res .= str_repeat($char, mb_strlen($title, $this->getCharset()));
41
42 4
        return $res . "\n";
43
    }
44
45 3
    public function renderH1($title)
46
    {
47 3
        return $this->renderH($title, '=');
48
    }
49
50 1
    public function renderH2($title)
51
    {
52 1
        return $this->renderH($title, '-');
53
    }
54
55 1
    public function renderText($text)
56
    {
57 1
        $text = trim($text);
58
59 1
        return $text ? "\n$text\n" : '';
60
    }
61
62 1
    public function renderBold($text)
63
    {
64 1
        $text = trim($text);
65
66 1
        return $this->renderText('**' . $text . '**');
67
    }
68
69 2
    public function renderSection($section, $default = null)
70
    {
71 2
        $file = 'readme/' . str_replace(' ', '', $section);
72 2
        $path = Yii::getAlias("@root/docs/$file.md");
73 2
        $text = file_exists($path) ? file_get_contents($path) : $this->getSection($file, $default);
74 2
        $text = trim($text);
75
76 2
        return $text ? "\n## $section\n\n$text\n" : '';
77
    }
78
79 2
    public function getSection($file, $default = null)
80
    {
81 2
        $view = Yii::$app->getView();
82 2
        $tpl = Helper::file2template($file);
83
        try {
84 2
            $res = $view->render($tpl, ['config' => $this->takeConfig()]);
85 2
        } catch (\Exception $e) {
86 2
            $res = '';
87
        }
88
89 2
        return $res ?: $default;
90
    }
91
92 2
    public function getSections()
93
    {
94 2
        return $this->sections ?: ['Requirements', 'Installation', 'Idea', 'Configuration', 'Basic Usage', 'Usage', 'Support', 'License', 'Acknowledgements', 'Acknowledgments'];
0 ignored issues
show
Documentation introduced by
The property sections 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...
95
    }
96
97
    /**
98
     * Render all configured sections.
99
     * @return string
100
     */
101 2
    public function renderSections($sections = null)
102
    {
103 2
        if ($sections === null) {
104 2
            $sections = $this->getSections();
105 2
        }
106 2
        $res = '';
107 2
        foreach ($sections as $section) {
108 2
            $res .= $this->renderSection($section);
109 2
        }
110
111 2
        return $res;
112
    }
113
114
    /**
115
     * Render all configured badges.
116
     * @return string
117
     */
118 2
    public function renderBadges()
119 1
    {
120 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...
121 2
        if (!$badges) {
122 1
            return '';
123
        }
124 1
        $pm = $this->takeGoal('package')->getPackageManager();
125 1
        if (!$pm || !$pm->getConfiguration()->getRequire()) {
126 1
            unset($badges['versioneye.dependencies']);
127 1
        }
128 1
        $res = '';
129 1
        foreach ($badges as $badge => $tpl) {
130 1
            if (!$tpl) {
131 1
                $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...
132 1
            }
133 1
            if ($tpl === 'disabled') {
134
                continue;
135
            }
136 1
            $res .= $this->renderBadge($tpl) . "\n";
137 1
        }
138
139 1
        return $res ? "\n$res" : '';
140
    }
141
142
    /**
143
     * Render badge by given template.
144
     * @param string $template string to render.
145
     * @return string
146
     */
147 1
    public function renderBadge($template)
148
    {
149 1
        return $this->getTwig()->render($template, ['config' => $this->takeConfig()]);
150
    }
151
152
    /**
153
     * Twig getter.
154
     * @return \Twig_Environment
155
     */
156 1
    public function getTwig()
157
    {
158 1
        if ($this->_twig === null) {
159 1
            $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...
160 1
        }
161
162 1
        return $this->_twig;
163
    }
164
}
165