Completed
Push — master ( a0e7e0...d1cff9 )
by Costin
01:48 queued 12s
created

FormElement   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 201
Duplicated Lines 21.39 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 2
dl 43
loc 201
rs 9.2
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setForm() 0 4 1
A setModel() 0 11 5
A setCommonAttributes() 0 13 1
A setSpecificAttributes() 0 4 1
A setName() 3 6 3
B setLabel() 8 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() 13 13 2

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
    public function __construct(array $params)
61
    {
62
        $this->params = $params;
63
64
        $this->setForm();
65
        $this->setModel();
66
        $this->setCommonAttributes();
67
        $this->setSpecificAttributes();
68
    }
69
70
    protected function setForm()
71
    {
72
        $this->form = app(Form::class);
73
    }
74
75
    protected function setModel()
76
    {
77
        if (isset($this->params['model']) && ! empty($this->params['model'])) {
78
            $this->model = $this->params['model'];
79
        }
80
81
        // Check to see if the model was added on the form opening tag
82
        if (is_null($this->model) && ! is_null($this->form->model)) {
83
            $this->model = $this->form->model;
84
        }
85
    }
86
87
    protected function setCommonAttributes()
88
    {
89
        $this->setName();
90
        $this->setLabel();
91
        $this->setValue();
92
        $this->setClass();
93
        $this->setRequired();
94
        $this->setDisabled();
95
        $this->setReadonly();
96
        $this->setAutocomplete();
97
        $this->setDesc();
98
        $this->setHelp();
99
    }
100
101
    protected function setSpecificAttributes()
102
    {
103
        // ...
104
    }
105
106
    protected function setName()
107
    {
108 View Code Duplication
        if (isset($this->params['name']) && ! empty($this->params['name'])) {
1 ignored issue
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...
109
            $this->name = $this->id = $this->params['name'];
110
        }
111
    }
112
113
    protected function setLabel()
114
    {
115
        // First, check if we receive an explicit label
116 View Code Duplication
        if (isset($this->params['label']) && ! empty($this->params['label'])) {
1 ignored issue
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...
117
            $this->label = $this->params['label'];
118
119
            return;
120
        }
121
122
        // Check if we receive a label that is false, so we don't display it
123 View Code Duplication
        if (isset($this->params['label']) && $this->params['label'] === false) {
1 ignored issue
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...
124
            $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...
125
126
            return;
127
        }
128
129
        // Fallback: construct the label from the name
130
        if (isset($this->name)) {
131
            $this->label = ucwords(str_replace('_', ' ', $this->name));
132
        }
133
    }
134
135
    protected function setRequired()
136
    {
137 View Code Duplication
        if (isset($this->params['required']) && $this->params['required'] == true) {
1 ignored issue
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...
138
            $this->required = true;
139
        }
140
    }
141
142
    protected function setDisabled()
143
    {
144 View Code Duplication
        if (isset($this->params['disabled']) && $this->params['disabled'] == true) {
1 ignored issue
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...
145
            $this->disabled = true;
146
        }
147
    }
148
149
    protected function setReadonly()
150
    {
151 View Code Duplication
        if (isset($this->params['readonly']) && $this->params['readonly'] == true) {
1 ignored issue
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...
152
            $this->readonly = true;
153
        }
154
    }
155
156
    protected function setValue()
157
    {
158
        if (is_null($this->name)) {
159
            return;
160
        }
161
162
        if (! is_null($this->value)) {
163
            $computedValue = $this->value;
164
        } elseif (! is_null($this->model) && isset($this->model->{str_replace('[]', '', $this->name)})) {
165
            $computedValue = $this->model->{str_replace('[]', '', $this->name)};
166
        }
167
168
        $this->value = old($this->name, $computedValue ?? '');
169
    }
170
171 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...
172
    {
173
        // Set default autocomplete option (true/false) from cofing file
174
        $this->autocomplete = config('blade-form-components.autocomplete');
175
176
        if (isset($this->params['autocomplete'])) {
177
            $this->autocomplete = $this->params['autocomplete'];
178
        }
179
    }
180
181
    protected function setDesc()
182
    {
183
        if (isset($this->params['desc'])) {
184
            $this->desc = $this->params['desc'];
185
        }
186
    }
187
188
    protected function setHelp()
189
    {
190
        if (isset($this->params['help'])) {
191
            $this->help = $this->params['help'];
192
        }
193
    }
194
195 View Code Duplication
    protected function setClass()
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...
196
    {
197
        // Default class applied to all form elements (Eg 'form-control' for Bootstrap)
198
        $this->class[] = config('blade-form-components.styles.field.input');
199
200
        // Attach the error class if an error is displayed against this field
201
        $errors = session()->get('errors', app(ViewErrorBag::class));
202
        if ($errors->has($this->name)) {
203
            $this->class[] = config('blade-form-components.styles.field.error');
204
        }
205
206
        // Attach other user-defined classes
207
    }
208
}
209