Completed
Push — master ( 341a7d...fd15d4 )
by WEBEWEB
02:17
created

AbstractComponentTwigExtension::bootstrapButton()   D

Complexity

Conditions 10
Paths 512

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 4.1777
c 0
b 0
f 0
cc 10
nc 512
nop 8

How to fix   Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\BootstrapBundle\Twig\Extension\Component;
13
14
use WBW\Bundle\BootstrapBundle\Twig\Extension\AbstractBootstrapTwigExtension;
15
use WBW\Bundle\BootstrapBundle\Twig\Extension\BootstrapRendererTwigExtension;
16
use WBW\Library\Core\Utility\Argument\StringUtility;
17
18
/**
19
 * Abstract component Twig extension.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Component
23
 * @abstract
24
 */
25
abstract class AbstractComponentTwigExtension extends AbstractBootstrapTwigExtension {
26
27
    /**
28
     * Constructor.
29
     */
30
    protected function __construct() {
31
        parent::__construct();
32
    }
33
34
    /**
35
     * Displays a Bootstrap alert.
36
     *
37
     * @param string $content The alert content.
38
     * @param boolean $dismissible Dismissible alert ?
39
     * @param string $class The alert class.
40
     * @return string Returns the Bootstrap alert.
41
     */
42
    protected function bootstrapAlert($content, $dismissible, $class) {
43
44
        // Initialize the templates.
45
        $template = "<div %attributes%>%innerHTML%</div>";
46
        $button   = "<button class=\"close\" type=\"button\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>";
47
48
        // Initialize the attributes.
49
        $attributes = [];
50
51
        $attributes["class"]   = ["alert", $class];
52
        $attributes["class"][] = true === $dismissible ? "alert-dismissible" : null;
53
        $attributes["role"]    = ["alert"];
54
55
        // Initialize the parameters.
56
        $innerHTML = (true === $dismissible ? $button : "") . (null !== $content ? $content : "");
57
58
        // Return the HTML.
59
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
60
    }
61
62
    /**
63
     * Displays a Bootstrap badge.
64
     *
65
     * @param string $content The badge content.
66
     * @return string Returns the Bootstrap badge.
67
     */
68
    protected function bootstrapBadge($content) {
69
70
        // Initialize the template.
71
        $template = "<span %attributes%>%innerHTML%</span>";
72
73
        // Initialize the attributes.
74
        $attributes = [];
75
76
        $attributes["class"] = ["badge"];
77
78
        // Initialize the parameters.
79
        $innerHTML = null !== $content ? $content : "";
80
81
        // Return the HTML.
82
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
83
    }
84
85
    /**
86
     * Displays a Bootstrap button.
87
     *
88
     * @param string $content The button content.
89
     * @param string $title The button title.
90
     * @param string $size The button size.
91
     * @param boolean $block Block button ?
92
     * @param booelan $active Active button ?
93
     * @param booelan $disable Disable button ?
94
     * @param string $class The button class.
95
     * @param string $icon The button icon.
96
     * @return string Returns the Bootstrap button.
97
     */
98
    protected function bootstrapButton($content, $title, $size, $block, $active, $disable, $class, $icon) {
99
100
        // Initialize the template.
101
        $template = "<button %attributes%>%glyphicon%%innerHTML%</button>";
102
103
        // Initialize the attributes.
104
        $attributes = [];
105
106
        $attributes["class"]          = ["btn", $class];
107
        $attributes["class"][]        = true === $block ? "btn-block" : null;
108
        $attributes["class"][]        = true === in_array($size, ["lg", "sm", "xs"]) ? "btn-" . $size : null;
109
        $attributes["class"][]        = true === $active ? "active" : null;
110
        $attributes["title"]          = $title;
111
        $attributes["type"]           = "button";
112
        $attributes["data-toggle"]    = null !== $title ? "tooltip" : null;
113
        $attributes["data-placement"] = null !== $title ? "top" : null;
114
        $attributes["disabled"]       = true === $disable ? "disabled" : null;
115
116
        // Handle the parameters.
117
        $glyphicon = null !== $icon ? BootstrapRendererTwigExtension::renderIcon($icon) : "";
118
        $innerHTML = null !== $content ? ("" !== $glyphicon ? " " . $content : $content) : "";
119
120
        // Return the HTML.
121
        return StringUtility::replace($template, ["%attributes%", "%glyphicon%", "%innerHTML%"], [StringUtility::parseArray($attributes), $glyphicon, $innerHTML]);
122
    }
123
124
    /**
125
     * Displays a Bootstrap glyphicon.
126
     *
127
     * @param string $name The glyphicon name.
128
     * @return string Returns the Bootstrap glyphicon.
129
     */
130
    protected function bootstrapGlyphicon($name, $style) {
131
132
        // Initialize the template.
133
        $template = "<span %attributes%></span>";
134
135
        // Initialize the attributes.
136
        $attributes = [];
137
138
        $attributes["class"]       = ["glyphicon"];
139
        $attributes["class"][]     = null !== $name ? "glyphicon-" . $name : null;
140
        $attributes["aria-hidden"] = "true";
141
        $attributes["style"]       = $style;
142
143
        // Return the HTML.
144
        return StringUtility::replace($template, ["%attributes%"], [StringUtility::parseArray($attributes)]);
145
    }
146
147
    /**
148
     * Displays a Bootstrap label.
149
     *
150
     * @param string $content The label content.
151
     * @param string $class The label class.
152
     * @return string Returns the Bootstrap label.
153
     */
154
    protected function bootstrapLabel($content, $class) {
155
156
        // Initialize the template.
157
        $template = "<span %attributes%>%innerHTML%</span>";
158
159
        // Initialize the attributes.
160
        $attributes = [];
161
162
        $attributes["class"] = ["label", $class];
163
164
        // Initialize the parameters.
165
        $innerHTML = null !== $content ? $content : "";
166
167
        // Return the HTML.
168
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
169
    }
170
171
    /**
172
     * Displays a Bootstrap progress bar.
173
     *
174
     * @param string $content The progress bar content.
175
     * @param integer $value The progress bar value.
176
     * @param integer $min The progress bar min.
177
     * @param integer $max The progress bar max.
178
     * @param boolean $striped Progress bar striped ?
179
     * @param boolean $animated Progress bar animated ?
180
     * @param string $class The progress bar class.
181
     * @return string Returns the Bootstrap progress bar.
182
     */
183
    protected function bootstrapProgressBar($content, $value, $min, $max, $striped, $animated, $class = null) {
184
185
        // Initialize the template.
186
        $template = "<div class=\"progress\"><div %attributes%>%innerHTML%</div></div>";
187
        $span     = "<span class=\"sr-only\">%value%%</span>";
188
189
        // Initialize the attributes.
190
        $attributes = [];
191
192
        $attributes["class"]         = ["progress-bar", $class];
193
        $attributes["class"][]       = true === $striped ? "progress-bar-striped" : null;
194
        $attributes["class"][]       = true === $animated ? "active" : null;
195
        $attributes["style"]         = "width: " . $value . "%;";
196
        $attributes["role"]          = "progressbar";
197
        $attributes["aria-valuenow"] = $value;
198
        $attributes["aria-valuemin"] = $min;
199
        $attributes["aria-valuemax"] = $max . "%";
200
201
        // Initialize the parameters.
202
        $innerHTML = null !== $content ? $content : StringUtility::replace($span, ["%value%"], [$value]);
203
204
        // Return the HTML.
205
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
206
    }
207
208
}
209