Passed
Branch ops-updates (277b44)
by Björn
05:09
created

Button::__invoke()   F

Complexity

Conditions 21
Paths 6144

Size

Total Lines 67
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 39
c 0
b 0
f 0
nc 6144
nop 1
dl 0
loc 67
rs 0

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
0 ignored issues
show
Bug introduced by
The type UIComponents\View\Helper\Components\StdClass was not found. Did you mean StdClass? If so, make sure to prefix the type with \.
Loading history...
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()) ) {
0 ignored issues
show
Bug introduced by
$component->getTags() of type UIComponents\View\Helper\Components\the is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        if ( isset($options["tagname"]) && in_array(strtolower($options["tagname"]), /** @scrutinizer ignore-type */ $component->getTags()) ) {
Loading history...
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
        if ( isset($options["size"]) && in_array(strtolower($options["size"]), $component->getSizes()) ) {
71
            $component->addClass('btn-'.strtolower($options["size"]).' '.strtolower($this->_foundations_sizes["size"]));
72
        }
73
        if ( isset($options["type"]) && in_array(strtolower($options["type"]), $component->getTypes()) ) {
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
        if ( isset($options["label"]) && !empty($options["label"]) ) {
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
0 ignored issues
show
Bug introduced by
The type UIComponents\View\Helper\Components\the was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
127
     */
128
    public function getSizes() {
129
        return $this->_sizes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_sizes returns the type array which is incompatible with the documented return type UIComponents\View\Helper\Components\the.
Loading history...
130
    }
131
132
    /**
133
     * @param multitype:string  $_sizes
0 ignored issues
show
Bug introduced by
The type UIComponents\View\Helper\Components\multitype was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_types returns the type array which is incompatible with the documented return type UIComponents\View\Helper\Components\the.
Loading history...
145
    }
146
147
    /**
148
     * @param multitype:string  $_types
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_tags returns the type array which is incompatible with the documented return type UIComponents\View\Helper\Components\the.
Loading history...
160
    }
161
162
    /**
163
     * @param multitype:string  $_tags
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->drop returns the type boolean which is incompatible with the documented return type UIComponents\View\Helper\Components\the.
Loading history...
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;
189
        }
190
        return $this;
191
    }
192
193
    /**
194
     * @return the $block
195
     */
196
    public function getBlock() {
197
        return $this->block;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->block returns the type boolean which is incompatible with the documented return type UIComponents\View\Helper\Components\the.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->active returns the type boolean which is incompatible with the documented return type UIComponents\View\Helper\Components\the.
Loading history...
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
}