InputField   F
last analyzed

Complexity

Total Complexity 66

Size/Duplication

Total Lines 274
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 13

Importance

Changes 0
Metric Value
wmc 66
lcom 0
cbo 13
dl 0
loc 274
rs 3.1913
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
D getDefaultInputFieldType() 0 83 30
D getWidgetClassNameFromFieldType() 0 48 16
C getIsIconSupportedFieldType() 0 21 14
A getIsEditorFromFieldType() 0 11 4
A getIsWidgetFromFieldType() 0 8 2

How to fix   Complexity   

Complex Class

Complex classes like InputField often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use InputField, and based on these observations, apply Extract Interface, too.

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\forms;
16
17
use fangface\Tools;
18
use fangface\widgets\BootstrapColorPicker;
19
use fangface\widgets\BootstrapSelect;
20
use fangface\widgets\BootstrapSelectSplitter;
21
use fangface\widgets\CKEditor;
22
use fangface\widgets\DatePicker;
23
use fangface\widgets\DateTimePicker;
24
use fangface\widgets\MiniColors;
25
use fangface\widgets\MultiSelect;
26
use fangface\widgets\Select2;
27
use fangface\widgets\TimePicker;
28
use yii\helpers\ArrayHelper;
29
30
31
class InputField
32
{
33
34
    // input type constants
35
    const INPUT_TEXT                    = 'textInput';
36
    const INPUT_STATIC                  = 'staticInput';
37
    const INPUT_READONLY                = 'textInputReadOnly';
38
    const INPUT_TEXTAREA                = 'textarea';
39
40
    const INPUT_PASSWORD                = 'passwordInput';
41
    const INPUT_PASSWORD_STRENGTH       = 'passwordStrength';
42
43
    const INPUT_DROPDOWN_LIST           = 'dropDownList';
44
    const INPUT_LIST_BOX                = 'listBox';
45
    const INPUT_SELECT2                 = 'select2';
46
    const INPUT_SELECT2_MULTI           = 'select2Multi';
47
    const INPUT_SELECT2_TAGS            = 'select2Tags'; //wlchere todo
48
    const INPUT_SELECT_PICKER           = 'bsSelectPicker';
49
    const INPUT_SELECT_PICKER_MULTI     = 'bsSelectPickerMulti';
50
    const INPUT_SELECT_SPLITTER         = 'bsSelectSplitter';
51
52
    const INPUT_CHECKBOX                = 'checkbox';
53
    const INPUT_CHECKBOX_BASIC          = 'checkboxBasic';
54
    const INPUT_CHECKBOX_SWITCH         = 'checkboxSwitch';
55
    const INPUT_CHECKBOX_ICHECK         = 'checkboxIcheck';
56
    const INPUT_CHECKBOX_LIST           = 'checkboxList';
57
    const INPUT_CHECKBOX_LIST_ICHECK    = 'checkboxListIcheck';
58
59
    const INPUT_RADIO                   = 'radio';
60
    const INPUT_RADIO_LIST              = 'radioList';
61
    const INPUT_RADIO_LIST_ICHECK       = 'radioListIcheck';
62
63
    const INPUT_MULTISELECT             = 'multiSelect';
64
    const INPUT_FILE                    = 'fileInput'; //wlchere todo
65
    const INPUT_HTML5                   = 'input'; //wlchere todo
66
    const INPUT_WIDGET                  = 'widget'; //wlchere todo
67
    const INPUT_HIDDEN                  = 'hidden'; //wlchere todo
68
    const INPUT_COLOR                   = 'bsColorPicker';
69
    const INPUT_MINI_COLORS             = 'miniColors';
70
    const INPUT_RAW                     = 'raw';
71
72
    const INPUT_INTEGER                 = 'textInputInteger';
73
    const INPUT_DECIMAL                 = 'textInputDecimal';
74
    const INPUT_DATE                    = 'datePicker';
75
    const INPUT_DATETIME                = 'dateTimePicker';
76
    const INPUT_TIME                    = 'timePicker';
77
    const INPUT_YEAR                    = 'textInputYear';
78
79
    const INPUT_EDITOR_CK               = 'editor_CK';
80
    const INPUT_EDITOR_BS_WYSIHTML5     = 'editor_BSW5'; //wlchere todo
81
    const INPUT_EDITOR_BS_SUMMERNOTE    = 'editor_BSSN'; //wlchere todo
82
83
    // input size constants
84
    const INPUT_SIZE_NONE = '';
85
    const INPUT_SIZE_AUTO = 'auto';
86
    const INPUT_SIZE_MINI = 'mini';
87
    const INPUT_SIZE_XSMALL = 'xsmall';
88
    const INPUT_SIZE_SMALL = 'small';
89
    const INPUT_SIZE_MEDIUM = 'medium';
90
    const INPUT_SIZE_LARGE = 'large';
91
    const INPUT_SIZE_XLARGE = 'xlarge';
92
    const INPUT_SIZE_MAX = 'max';
93
94
    // tooltip icon position
95
    const ICON_POSITION_LEFT = 'left';
96
    const ICON_POSITION_RIGHT = 'right';
97
98
    /**
99
     * Return default input field type based on column schema for attribute
100
     * @param string $attributeName
101
     * @param \yii\db\ColumnSchema $columnSchema
102
     * @param array|null $config attributeConfig (active element)
103
     * @return string
104
     */
105
    public static function getDefaultInputFieldType($attributeName, $columnSchema, $config = null)
106
    {
107
        if (is_array($config) && $config) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $config of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
108
            $type = ArrayHelper::getValue($config, 'type', '');
109
            if ($type != '') {
110
                return $type;
111
            }
112
        }
113
114
        switch ($attributeName) {
115
            case 'id':
116
                $type = self::INPUT_STATIC;
117
                break;
118
            case 'created_at':
119
            case 'createdAt':
120
            case 'modified_at':
121
            case 'modifiedAt':
122
                $type = self::INPUT_STATIC;
123
                break;
124
            default:
125
                switch ($columnSchema->type) {
126
                    case 'string':
127
                    // case 'char':
128
                    // case 'varchar':
129
                        if (is_array($columnSchema->enumValues) && $columnSchema->enumValues) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $columnSchema->enumValues of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
130
                            $type = self::INPUT_DROPDOWN_LIST;
131
                        } else {
132
                            $type = self::INPUT_TEXT;
133
                        }
134
                        break;
135
                    case 'text':
136
                    //case 'tinytext':
137
                    //case 'mediumtext':
138
                    //case 'longtext':
139
                    case 'binary':
140
                    //case 'varbinary':
141
                    //case 'blob':
142
                    //case 'tinyblob':
143
                    //case 'mediumblob':
144
                    //case 'longblob':
145
                        $type = self::INPUT_TEXTAREA;
146
                        break;
147
                    case 'int':
148
                    case 'integer':
149
                    case 'tinyint':
150
                    case 'smallint':
151
                    case 'mediumint':
152
                    case 'bigint':
153
                        if ($columnSchema->size == 1) {
154
                            $type = self::INPUT_CHECKBOX;
155
                        } else {
156
                            $type = self::INPUT_INTEGER;
157
                        }
158
                        break;
159
                    case 'float':
160
                    case 'real':
161
                    case 'double':
162
                    case 'decimal':
163
                    case 'numeric':
164
                        $type = self::INPUT_DECIMAL;
165
                        break;
166
                    case 'date':
167
                        if ($columnSchema->size == 4) {
168
                            $type = self::INPUT_YEAR;
169
                        } else {
170
                            $type = self::INPUT_DATE;
171
                        }
172
                        break;
173
                    case 'datetime':
174
                        $type = self::INPUT_DATETIME;
175
                        break;
176
                    case 'time':
177
                        $type = self::INPUT_TIME;
178
                        break;
179
                    default:
180
                        Tools::debug($columnSchema, __CLASS__);
181
                        $type = self::INPUT_TEXT;
182
                        break;
183
                }
184
                break;
185
        }
186
        return $type;
187
    }
188
189
    /**
190
     * Get default widget class based on input field type
191
     * @param string $type
192
     * @return string
193
     */
194
    public static function getWidgetClassNameFromFieldType($type)
195
    {
196
        switch ($type) {
197
            case self::INPUT_COLOR:
198
                $widgetClass = BootstrapColorPicker::className();
199
                break;
200
            case self::INPUT_MINI_COLORS:
201
                $widgetClass = MiniColors::className();
202
                break;
203
            case self::INPUT_DATE:
204
                $widgetClass = DatePicker::className();
205
                break;
206
            case self::INPUT_DATETIME:
207
                $widgetClass = DateTimePicker::className();
208
                break;
209
            case self::INPUT_MULTISELECT:
210
                $widgetClass = MultiSelect::className();
211
                break;
212
            case self::INPUT_SELECT2_MULTI:
213
            case self::INPUT_SELECT2_TAGS:
214
            case self::INPUT_SELECT2:
215
                $widgetClass = Select2::className();
216
                break;
217
            case self::INPUT_SELECT_PICKER:
218
            case self::INPUT_SELECT_PICKER_MULTI:
219
                $widgetClass = BootstrapSelect::className();
220
                break;
221
            case self::INPUT_SELECT_SPLITTER:
222
                $widgetClass = BootstrapSelectSplitter::className();
223
                break;
224
            case self::INPUT_TIME:
225
                $widgetClass = TimePicker::className();
226
                break;
227
            case self::INPUT_EDITOR_CK:
228
                $widgetClass = CKEditor::className();
229
                break;
230
            case self::INPUT_EDITOR_BS_WYSIHTML5:
231
                $widgetClass = BootstrapWysihtml5::className();
232
                break;
233
            case self::INPUT_EDITOR_BS_SUMMERNOTE:
234
                $widgetClass = BootstrapSummernote::className();
235
                break;
236
            default:
237
                $widgetClass = '';
238
                break;
239
        }
240
        return $widgetClass;
241
    }
242
243
    /**
244
     * Check if field input type supports icon prefix
245
     *
246
     * @param string $type
247
     * @return boolean
248
     */
249
    public static function getIsIconSupportedFieldType($type)
250
    {
251
        switch ($type) {
252
            case self::INPUT_TEXT:
253
            case self::INPUT_TEXTAREA:
254
            case self::INPUT_PASSWORD:
255
            case self::INPUT_DROPDOWN_LIST:
256
            case self::INPUT_LIST_BOX:
257
            case self::INPUT_YEAR:
258
            case self::INPUT_INTEGER:
259
            case self::INPUT_DECIMAL:
260
            case self::INPUT_DATE:
261
            case self::INPUT_DATETIME:
262
            case self::INPUT_TIME:
263
            case self::INPUT_YEAR:
264
            case self::INPUT_READONLY:
265
                return true;
266
            default:
267
        }
268
        return false;
269
    }
270
271
    /**
272
     * Return true if input field type is an editor area
273
     *
274
     * @param string $type
275
     * @return boolean
276
     */
277
    public static function getIsEditorFromFieldType($type)
278
    {
279
        switch ($type) {
280
            case InputField::INPUT_EDITOR_CK:
281
            case InputField::INPUT_EDITOR_BS_WYSIHTML5:
282
            case InputField::INPUT_EDITOR_BS_SUMMERNOTE:
283
                return true;
284
            default:
285
                return false;
286
        }
287
    }
288
289
    /**
290
     * Check if input type is a widget
291
     *
292
     * @param string $type
293
     * @return boolean
294
     */
295
    public static function getIsWidgetFromFieldType($type)
296
    {
297
        $class = self::getWidgetClassNameFromFieldType($type);
298
        if ($class != '') {
299
            return true;
300
        }
301
        return false;
302
    }
303
304
}
305