Completed
Push — master ( 4c6f3f...cbda9e )
by Costin
01:12
created

FormElement   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 222
Duplicated Lines 9.91 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 45
lcom 1
cbo 2
dl 22
loc 222
rs 8.8
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setForm() 0 4 1
A setModel() 0 11 5
A setCommonAttributes() 0 16 1
A setSpecificAttributes() 0 4 1
A setName() 0 6 3
B setLabel() 4 21 6
A setRequired() 3 6 3
A setDisabled() 3 6 3
A setReadonly() 3 6 3
A setValue() 0 14 5
A setAutocomplete() 9 9 2
A setDesc() 0 6 2
A setHelp() 0 6 2
A setClass() 0 15 3
A setDefaultClass() 0 5 1
A setCustomAttributes() 0 6 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like FormElement 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 FormElement, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace SoareCostin\BladeFormComponents;
4
5
use Illuminate\Support\ViewErrorBag;
6
use SoareCostin\BladeFormComponents\Traits\GluesAttributes;
7
8
abstract class FormElement
9
{
10
    use GluesAttributes;
11
12
    /** @var array */
13
    protected $params;
14
15
    /** @var Form */
16
    protected $form;
17
18
    /** @var object */
19
    public $model = null;
20
21
    /** @var string */
22
    public $id;
23
24
    /** @var string */
25
    public $name;
26
27
    /** @var mixed */
28
    public $value;
29
30
    /** @var string */
31
    public $label;
32
33
    /** @var array */
34
    public $class = [];
35
36
    /** @var array */
37
    public $labelClass = [];
38
39
    /** @var bool */
40
    public $required = false;
41
42
    /** @var bool */
43
    public $disabled = false;
44
45
    /** @var bool */
46
    public $readonly = false;
47
48
    /** @var bool */
49
    public $autocomplete;
50
51
    /** @var array */
52
    public $attributesList = [];
53
54
    /** @var string */
55
    public $desc;
56
57
    /** @var string */
58
    public $help;
59
    
60
    /** @var array */
61
    public $customAttributes;
62
63
    public function __construct(array $params)
64
    {
65
        $this->params = $params;
66
67
        $this->setForm();
68
        $this->setModel();
69
        $this->setCommonAttributes();
70
        $this->setSpecificAttributes();
71
    }
72
73
    protected function setForm()
74
    {
75
        $this->form = app(Form::class);
76
    }
77
78
    protected function setModel()
79
    {
80
        if (isset($this->params['model']) && ! empty($this->params['model'])) {
81
            $this->model = $this->params['model'];
82
        }
83
84
        // Check to see if the model was added on the form opening tag
85
        if (is_null($this->model) && ! is_null($this->form->model)) {
86
            $this->model = $this->form->model;
87
        }
88
    }
89
90
    protected function setCommonAttributes()
91
    {
92
        $this->setName();
93
        $this->setLabel();
94
        $this->setValue();
95
        $this->setClass();
96
        $this->setRequired();
97
        $this->setDisabled();
98
        $this->setReadonly();
99
        $this->setAutocomplete();
100
        $this->setDesc();
101
        $this->setHelp();
102
103
        // Additional, custom attributes set by the user (eg: data, v-model)
104
        $this->setCustomAttributes();
105
    }
106
107
    protected function setSpecificAttributes()
108
    {
109
        // ...
110
    }
111
112
    protected function setName()
113
    {
114
        if (isset($this->params['name']) && ! empty($this->params['name'])) {
115
            $this->name = $this->id = $this->params['name'];
116
        }
117
    }
118
119
    protected function setLabel()
120
    {
121
        // First, check if we receive an explicit label
122
        if (isset($this->params['label']) && ! empty($this->params['label'])) {
123
            $this->label = $this->params['label'];
124
125
            return;
126
        }
127
128
        // Check if we receive a label that is false, so we don't display it
129 View Code Duplication
        if (isset($this->params['label']) && $this->params['label'] === false) {
130
            $this->label = false;
0 ignored issues
show
Documentation Bug introduced by
The property $label was declared of type string, but false is of type false. 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...
131
132
            return;
133
        }
134
135
        // Fallback: construct the label from the name
136
        if (isset($this->name)) {
137
            $this->label = ucwords(str_replace('_', ' ', $this->name));
138
        }
139
    }
140
141
    protected function setRequired()
142
    {
143 View Code Duplication
        if (isset($this->params['required']) && $this->params['required'] == true) {
144
            $this->required = true;
145
        }
146
    }
147
148
    protected function setDisabled()
149
    {
150 View Code Duplication
        if (isset($this->params['disabled']) && $this->params['disabled'] == true) {
151
            $this->disabled = true;
152
        }
153
    }
154
155
    protected function setReadonly()
156
    {
157 View Code Duplication
        if (isset($this->params['readonly']) && $this->params['readonly'] == true) {
158
            $this->readonly = true;
159
        }
160
    }
161
162
    protected function setValue()
163
    {
164
        if (is_null($this->name)) {
165
            return;
166
        }
167
168
        if (! is_null($this->value)) {
169
            $computedValue = $this->value;
170
        } elseif (! is_null($this->model) && isset($this->model->{str_replace('[]', '', $this->name)})) {
171
            $computedValue = $this->model->{str_replace('[]', '', $this->name)};
172
        }
173
174
        $this->value = old($this->name, $computedValue ?? '');
175
    }
176
177 View Code Duplication
    protected function setAutocomplete()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
178
    {
179
        // Set default autocomplete option (true/false) from cofing file
180
        $this->autocomplete = config('blade-form-components.autocomplete');
181
182
        if (isset($this->params['autocomplete'])) {
183
            $this->autocomplete = $this->params['autocomplete'];
184
        }
185
    }
186
187
    protected function setDesc()
188
    {
189
        if (isset($this->params['desc'])) {
190
            $this->desc = $this->params['desc'];
191
        }
192
    }
193
194
    protected function setHelp()
195
    {
196
        if (isset($this->params['help'])) {
197
            $this->help = $this->params['help'];
198
        }
199
    }
200
201
    protected function setClass()
202
    {
203
        $this->setDefaultClass();
204
205
        // Attach the error class if an error is displayed against this field
206
        $errors = session()->get('errors', app(ViewErrorBag::class));
207
        if ($errors->has($this->name)) {
208
            $this->class[] = config('blade-form-components.styles.field.error');
209
        }
210
211
        // Attach other user-defined classes
212
        if (isset($this->params['class'])) {
213
            $this->class[] = $this->params['class'];
214
        }
215
    }
216
217
    protected function setDefaultClass()
218
    {
219
        // Default class applied to all form elements (Eg 'form-control' for Bootstrap)
220
        $this->class[] = config('blade-form-components.styles.field.input');
221
    }
222
223
    protected function setCustomAttributes()
224
    {
225
        if (isset($this->params['attributes']['input']) && !empty($this->params['attributes']['input'])) {
226
            $this->customAttributes = $this->params['attributes']['input'];
227
        }
228
    }
229
}
230