Passed
Push — master ( 700b0f...aafb0a )
by Björn
18:25 queued 10s
created

Button::__invoke()   F

Complexity

Conditions 21
Paths 6144

Size

Total Lines 69

Duplication

Lines 13
Ratio 18.84 %

Importance

Changes 0
Metric Value
dl 13
loc 69
rs 0
c 0
b 0
f 0
cc 21
nc 6144
nop 1

How to fix   Long Method    Complexity   

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:

1
<?php
2
/**
3
 * BB's Zend Framework 2 Components
4
 * 
5
 * UI Components
6
 *
7
 * @package     [MyApplication]
8
 * @subpackage  BB's Zend Framework 2 Components
9
 * @subpackage  UI Components
10
 * @author      Björn Bartels <[email protected]>
11
 * @link        https://gitlab.bjoernbartels.earth/groups/zf2
12
 * @license     http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
13
 * @copyright   copyright (c) 2016 Björn Bartels <[email protected]>
14
 */
15
16
namespace UIComponents\View\Helper\Components;
17
18
/**
19
 *
20
 * render nothing
21
 *
22
 */
23
class Button extends Element
24
{
25
    protected $tagname = 'a';
26
    
27
    protected $classnames = 'button btn';
28
    
29
    protected $drop = false;
30
    
31
    protected $block = false;
32
    
33
    protected $active = false;
34
    
35
    protected $_tags = array('a','input','button');
36
    
37
    protected $_sizes = array('xs','sm','lg');
38
    protected $_foundations_sizes = array(
39
        'xs' => 'tiny', 'xs' => 'small', 'lg' => 'large',
40
    );
41
42
    protected $_types = array('default','primary','success','info','warning','alert','danger','link');
43
    
44
    /**
45
     * View helper entry point:
46
     * Retrieves helper and optionally sets component options to operate on
47
     *
48
     * @param  array|StdClass $options [optional] component options to operate on
49
     * @return self
50
     */
51
    public function __invoke($options = array())
52
    {
53
        parent::__invoke($options);
54
        
55
        $component = clone $this;
56
        
57
        $component->setHeader('')->setFooter('');
58
        
59
        // tag options
60
        if ( isset($options["tagname"]) && in_array(strtolower($options["tagname"]), $component->getTags()) ) {
61
            $component->setTagname(strtolower($options["tagname"]));
62
        } else {
63
            $component->setTagname('a');
64
        }
65
        if ( isset($options["attributes"]) && is_array($options["attributes"]) ) {
66
            $component->setAttributes($options["attributes"]);
67
        }
68
        
69
        // bootstrap options
70 View Code Duplication
        if ( isset($options["size"]) && in_array(strtolower($options["size"]), $component->getSizes()) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
            $component->addClass('btn-'.strtolower($options["size"]).' '.strtolower($this->_foundations_sizes["size"]));
72
        }
73 View Code Duplication
        if ( isset($options["type"]) && in_array(strtolower($options["type"]), $component->getTypes()) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
            $component->addClass('btn-'.strtolower($options["type"]).' '.strtolower($options["type"]));
75
        } else {
76
            $component->addClass('btn-default');
77
        }
78
        if ( isset($options["drop"]) ) {
79
            $component->setDrop($options["drop"]);
80
        }
81
        if ( isset($options["block"]) ) {
82
            $component->setBlock($options["block"]);
83
        }
84
        if ( isset($options["active"]) ) {
85
            $component->setActive($options["active"]);
86
        }
87
88
        if ($component->getBlock()) $component->addClass('btn-block expanded');
89
        if ($component->getActive()) $component->addClass('active');
90
        
91
        if ($component->getTagname() == 'a') {
92
            $component->setAttributes(array_merge_recursive($component->getAttributes(), array(
93
                'role' => 'button'
94
            )));
95
            if ( isset($options["disabled"]) ) {
96
                $component->addClass('disabled');
97
            }
98
        } else {
99
            $component->setAttributes(array_merge_recursive($component->getAttributes(), array(
100
                'type' => 'button'
101
            )));
102
            if ( isset($options["disabled"]) ) {
103
                $component->setAttributes(array_merge_recursive($component->getAttributes(), array(
104
                    'disabled' => 'disabled'
105
                )));
106
            }
107
        }
108
        
109
        // tag content
110 View Code Duplication
        if ( isset($options["label"]) && !empty($options["label"]) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
            $component->setContent($options["label"]);
112
        } else if ( isset($options["content"]) && !empty($options["content"]) ) {
113
            $component->setContent($options["content"]);
114
        }
115
        
116
        //$component = clone $this;
117
        return $component;
118
        // return $this;
119
    }
120
    
121
    //
122
    // private getters/setters
123
    //
124
    
125
    /**
126
     * @return the $_sizes
127
     */
128
    public function getSizes() {
129
        return $this->_sizes;
130
    }
131
132
    /**
133
     * @param multitype:string  $_sizes
0 ignored issues
show
Documentation introduced by
The doc-type multitype:string could not be parsed: Unknown type name "multitype:string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
134
     */
135
    public function setSizes($_sizes) {
136
        $this->_sizes = $_sizes;
137
        return $this;
138
    }
139
    
140
    /**
141
     * @return the $_types
142
     */
143
    public function getTypes() {
144
        return $this->_types;
145
    }
146
147
    /**
148
     * @param multitype:string  $_types
0 ignored issues
show
Documentation introduced by
The doc-type multitype:string could not be parsed: Unknown type name "multitype:string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
149
     */
150
    public function setTypes($_types) {
151
        $this->_types = $_types;
152
        return $this;
153
    }
154
    
155
    /**
156
     * @return the $_tags
157
     */
158
    public function getTags() {
159
        return $this->_tags;
160
    }
161
162
    /**
163
     * @param multitype:string  $_tags
0 ignored issues
show
Documentation introduced by
The doc-type multitype:string could not be parsed: Unknown type name "multitype:string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
164
     */
165
    public function setTags($_tags) {
166
        $this->_tags = $_tags;
167
        return $this;
168
    }
169
170
    
171
    //
172
    // option getters/setters
173
    //
174
    
175
    
176
    /**
177
     * @return the $drop
178
     */
179
    public function getDrop() {
180
        return $this->drop;
181
    }
182
183
    /**
184
     * @param string $drop
185
     */
186
    public function setDrop($drop) {
187
        if ( in_array(strtolower($drop), array('up','down'))) {
188
            $this->drop = $drop;
0 ignored issues
show
Documentation Bug introduced by
The property $drop was declared of type boolean, but $drop is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
189
        }
190
        return $this;
191
    }
192
193
    /**
194
     * @return the $block
195
     */
196
    public function getBlock() {
197
        return $this->block;
198
    }
199
    
200
    /**
201
     * @param boolean $block
202
     */
203
    public function setBlock($block) {
204
        $this->block = !!$block;
205
        return $this;
206
    }
207
208
    /**
209
     * @return the $active
210
     */
211
    public function getActive() {
212
        return $this->active;
213
    }
214
    
215
    /**
216
     * @param boolean $active
217
     */
218
    public function setActive($active) {
219
        $this->active = !!$active;
220
        return $this;
221
    }
222
    
223
    
224
225
    
226
    
227
}