Completed
Push — master ( 97a22a...c2091e )
by Andrii
04:23
created

ReadmeGoal::renderBadges()   C

Complexity

Conditions 7
Paths 21

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 5 Features 0
Metric Value
dl 0
loc 22
c 7
b 5
f 0
rs 6.9811
cc 7
eloc 14
nc 21
nop 0
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2015, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\goals;
13
14
use hidev\helpers\Helper;
15
use Yii;
16
17
/**
18
 * Goal for README.
19
 */
20
class ReadmeGoal extends TemplateGoal
21
{
22
    public function getTemplate()
23
    {
24
        return 'README';
25
    }
26
27
    public function renderH($title, $char)
28
    {
29
        $res = $title . "\n";
30
        $res .= str_repeat($char, mb_strlen($title, Yii::$app->charset));
31
32
        return $res . "\n";
33
    }
34
35
    public function renderH1($title)
36
    {
37
        return $this->renderH($title, '=');
38
    }
39
40
    public function renderH2($title)
41
    {
42
        return $this->renderH($title, '-');
43
    }
44
45
    public function renderText($text)
46
    {
47
        $text = trim($text);
48
49
        return $text ? "\n$text\n" : '';
50
    }
51
52
    public function renderBold($text)
53
    {
54
        $text = trim($text);
55
56
        return $this->renderText('**' . $text . '**');
57
    }
58
59
    public function renderSection($section, $default = null)
60
    {
61
        $file = 'readme/' . str_replace(' ', '', $section);
62
        $path = Yii::getAlias("@source/docs/$file.md");
63
        $text = file_exists($path) ? file_get_contents($path) : $this->getSection($file, $default);
64
        $text = trim($text);
65
66
        return $text ? "\n## $section\n\n$text\n" : '';
67
    }
68
69
    public function getSection($file, $default = null)
70
    {
71
        $view = Yii::$app->getView();
72
        $tpl = Helper::file2template($file);
73
74
        return $view->existsTemplate($tpl) ? $view->render($tpl, ['config' => $this->config]) : $default;
0 ignored issues
show
Documentation introduced by
The property config does not exist on object<hidev\goals\ReadmeGoal>. 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...
75
    }
76
77
    public function getSections()
78
    {
79
        return $this->getItem('sections') ?: ['Requirements', 'Installation', 'Configuration', 'Basic Usage', 'Usage', 'Support', 'License', 'Acknowledgments'];
80
    }
81
82
    public function renderSections($sections = null)
83
    {
84
        if ($sections === null) {
85
            $sections = $this->sections;
0 ignored issues
show
Documentation introduced by
The property sections does not exist on object<hidev\goals\ReadmeGoal>. 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...
86
        }
87
        $res = '';
88
        foreach ($sections as $section) {
89
            $res .= $this->renderSection($section);
90
        }
91
92
        return $res;
93
    }
94
95
    public $known_badges = [
96
        'github.version'          => '[![GitHub version](https://badge.fury.io/gh/{{ config.vendor.name }}%2F{{ config.package.name }}.svg)](https://badge.fury.io/gh/{{ config.vendor.name }}%2F{{ config.package.name }})',
97
        'packagist.stable'        => '[![Latest Stable Version](https://poser.pugx.org/{{ config.composer.fullName }}/v/stable)](//packagist.org/packages/{{ config.composer.fullName }})',
98
        'packagist.unstable'      => '[![Latest Unstable Version](https://poser.pugx.org/{{ config.composer.fullName }}/v/unstable)](//packagist.org/packages/{{ config.composer.fullName }})',
99
        'packagist.license'       => '[![License](https://poser.pugx.org/{{ config.composer.fullName }}/v/license)](//packagist.org/packages/{{ config.composer.fullName }})',
100
        'packagist.downloads'     => '[![Total Downloads](https://poser.pugx.org/{{ config.composer.fullName }}/downloads)](//packagist.org/packages/{{ config.composer.fullName }})',
101
        'versioneye.dependencies' => '[![Dependency Status](https://www.versioneye.com/php/{{ config.vendor.name }}:{{ config.package.name }}/dev-master/badge.svg)](https://www.versioneye.com/php/{{ config.vendor.name }}:{{ config.package.name }}/dev-master)',
102
        'travisci.build'          => '[![Build Status](https://img.shields.io/travis/{{ config.package.fullName }}.svg)](http://travis-ci.org/{{ config.package.fullName }})',
103
    ];
104
105
    public function renderBadges()
106
    {
107
        $badges = $this->badges;
0 ignored issues
show
Bug introduced by
The property badges does not seem to exist. Did you mean known_badges?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
108
        if (!$badges) {
109
            return '';
110
        }
111
        if (!$this->package->getPackageManager()->getConfigFile()->getRequire()) {
0 ignored issues
show
Documentation introduced by
The property package does not exist on object<hidev\goals\ReadmeGoal>. 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...
112
            unset($badges['versioneye.dependencies']);
113
        }
114
        $res = '';
115
        foreach ($badges as $badge => $tpl) {
116
            if (!$tpl) {
117
                $tpl = $this->known_badges[$badge];
118
            }
119
            if ($tpl === 'disabled') {
120
                continue;
121
            }
122
            $res .= $this->renderBadge($tpl) . "\n";
123
        }
124
125
        return $res ? "\n$res" : '';
126
    }
127
128
    public function renderBadge($tpl)
129
    {
130
        return $this->getTwig()->render($tpl, ['config' => $this->getConfig()]);
131
    }
132
133
    protected $_twig;
134
135
    public function getTwig()
136
    {
137
        if ($this->_twig === null) {
138
            $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...
139
        }
140
141
        return $this->_twig;
142
    }
143
}
144