Completed
Branch master (a1edd4)
by
unknown
54:09
created

ButtonDropdown   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 122
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 20 2
B renderButton() 0 37 5
A renderDropdown() 0 8 1
1
<?php
2
/**
3
 * This file is part of the fangface/yii2-concord package
4
 *
5
 * For the full copyright and license information, please view
6
 * the file LICENSE.md that was distributed with this source code.
7
 *
8
 * @package fangface/yii2-concord
9
 * @author Fangface <[email protected]>
10
 * @copyright Copyright (c) 2014 Fangface <[email protected]>
11
 * @license https://github.com/fangface/yii2-concord/blob/master/LICENSE.md MIT License
12
 *
13
 */
14
15
namespace fangface\widgets;
16
17
use fangface\helpers\Html;
18
use yii\helpers\ArrayHelper;
19
use yii\bootstrap\Button;
20
use yii\bootstrap\Dropdown;
21
use yii\bootstrap\Widget;
22
23
24
/**
25
 * Date Picker widget
26
 */
27
class ButtonDropdown extends Widget
28
{
29
    /**
30
     * @var string the button label
31
     */
32
    public $label = 'Button';
33
    /**
34
     * @var array the HTML attributes for the container tag. The following special options are recognized:
35
     *
36
     * - tag: string, defaults to "div", the name of the container tag.
37
     *
38
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
39
     * @since 2.0.1
40
     */
41
    public $containerOptions = [];
42
    /**
43
     * @var array the HTML attributes of the button.
44
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
45
     */
46
    public $options = [];
47
    /**
48
     * @var array the configuration array for [[Dropdown]].
49
     */
50
    public $dropdown = [];
51
    /**
52
     * @var boolean whether to display a group of split-styled button group.
53
     */
54
    public $split = false;
55
    /**
56
     * @var string the tag to use to render the button
57
     */
58
    public $tagName = 'button';
59
    /**
60
     * @var boolean whether the label should be HTML-encoded.
61
     */
62
    public $encodeLabel = true;
63
    /**
64
     * @var boolean include container wrap (default btn-group)
65
     */
66
    public $includeContainer = true;
67
68
69
    /**
70
     * Renders the widget.
71
     */
72
    public function run()
73
    {
74
        $this->registerPlugin('button');
75
        if ($this->includeContainer) {
76
            Html::addCssClass($this->containerOptions, 'btn-group');
77
            $options = $this->containerOptions;
78
            $tag = ArrayHelper::remove($options, 'tag', 'div');
79
            return implode("\n", [
80
                Html::beginTag($tag, $options),
81
                $this->renderButton(),
82
                $this->renderDropdown(),
83
                Html::endTag($tag)
84
            ]);
85
        } else {
86
            return implode("\n", [
87
                $this->renderButton(),
88
                $this->renderDropdown(),
89
            ]);
90
        }
91
    }
92
93
    /**
94
     * Generates the button dropdown.
95
     * @return string the rendering result.
96
     */
97
    protected function renderButton()
98
    {
99
        Html::addCssClass($this->options, 'btn');
100
        $label = $this->label;
101
        if ($this->encodeLabel) {
102
            $label = Html::encode($label);
103
        }
104
        if ($this->split) {
105
            $options = $this->options;
106
            $this->options['data-toggle'] = 'dropdown';
107
            Html::addCssClass($this->options, ['toggle' => 'dropdown-toggle']);
0 ignored issues
show
Documentation introduced by
array('toggle' => 'dropdown-toggle') is of type array<string,string,{"toggle":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
            unset($this->options['id']);
109
            $splitButton = Button::widget([
110
                'label' => '<span class="caret"></span>',
111
                'encodeLabel' => false,
112
                'options' => $this->options,
113
                'view' => $this->getView(),
114
            ]);
115
        } else {
116
            $label .= ' <span class="caret"></span>';
117
            $options = $this->options;
118
            if (!isset($options['href']) && $this->tagName === 'a') {
119
                $options['href'] = '#';
120
            }
121
            Html::addCssClass($options, ['toggle' => 'dropdown-toggle']);
0 ignored issues
show
Documentation introduced by
array('toggle' => 'dropdown-toggle') is of type array<string,string,{"toggle":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
122
            $options['data-toggle'] = 'dropdown';
123
            $splitButton = '';
124
        }
125
126
        return Button::widget([
127
            'tagName' => $this->tagName,
128
            'label' => $label,
129
            'options' => $options,
130
            'encodeLabel' => false,
131
            'view' => $this->getView(),
132
        ]) . "\n" . $splitButton;
133
    }
134
135
    /**
136
     * Generates the dropdown menu.
137
     * @return string the rendering result.
138
     */
139
    protected function renderDropdown()
140
    {
141
        $config = $this->dropdown;
142
        $config['clientOptions'] = false;
143
        $config['view'] = $this->getView();
144
145
        return Dropdown::widget($config);
146
    }
147
148
}
149